【问题标题】:How to get list of available Bluetooth devices?如何获取可用蓝牙设备的列表?
【发布时间】:2012-04-28 00:41:42
【问题描述】:

我目前正在创建一个可以使用蓝牙设备的 iPhone 应用程序(Xcode 4.3.1,IOS 5)!此应用程序的主要目标是室内导航(建筑物内的 GPS 并不十分准确)。

我在这里看到的唯一解决方案(将我的应用保留在 AppStore 上)是尝试扫描可用的蓝牙设备!

我尝试使用 CoreBluetooth 框架,但我没有得到可用设备的列表! 可能我没有正确使用这些功能

#import <UIKit/UIKit.h>
#import <CoreBluetooth/CoreBluetooth.h>

@interface AboutBhyperView : UIViewController <CBPeripheralDelegate, CBCentralManagerDelegate>
{
    CBCentralManager *mgr;
}
@property (readwrite, nonatomic) CBCentralManager *mgr;
@end



- (void)viewDidLoad
{
    [super viewDidLoad];

    mgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}


- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {


    NSLog([NSString stringWithFormat:@"%@",[advertisementData description]]);
}

-(void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals{
    NSLog(@"This is it!");
}


- (void)centralManagerDidUpdateState:(CBCentralManager *)central{ 
    NSString *messtoshow;

    switch (central.state) {
        case CBCentralManagerStateUnknown:
        {
            messtoshow=[NSString stringWithFormat:@"State unknown, update imminent."];
            break;
        }
        case CBCentralManagerStateResetting:
        {
            messtoshow=[NSString stringWithFormat:@"The connection with the system service was momentarily lost, update imminent."];
            break;
        }
        case CBCentralManagerStateUnsupported:
        {
            messtoshow=[NSString stringWithFormat:@"The platform doesn't support Bluetooth Low Energy"];
            break;
        }
        case CBCentralManagerStateUnauthorized:
        {
            messtoshow=[NSString stringWithFormat:@"The app is not authorized to use Bluetooth Low Energy"];
            break;
        }
        case CBCentralManagerStatePoweredOff:
        {
            messtoshow=[NSString stringWithFormat:@"Bluetooth is currently powered off."];
            break;
        }
        case CBCentralManagerStatePoweredOn:
        {
            messtoshow=[NSString stringWithFormat:@"Bluetooth is currently powered on and available to use."];
            [mgr scanForPeripheralsWithServices:nil options:nil];
            //[mgr retrieveConnectedPeripherals];

//--- it works, I Do get in this area!

            break;
        }   

    }
    NSLog(messtoshow); 
} 

这行我不清楚,如何传递正确的参数?

[mgr scanForPeripheralsWithServices:nil options:nil];

我查看了苹果参考.. 我仍然不明白 CBUUID 是什么??? 每个设备都有一些蓝牙ID?我在哪里可以找到它?

- (void)scanForPeripheralsWithServices:(NSArray *)serviceUUIDs options:(NSDictionary *)options;

参数 serviceUUIDs - 应用感兴趣的 CBUUID 数组。 options - 自定义扫描的字典,请参阅 CBCentralManagerScanOptionAllowDuplicatesKey。

还有没有其他方法可以在IOS上使用蓝牙?我的意思是,不使用 BLE 4.0 的旧框架!

任何建议将不胜感激!

谢谢!

【问题讨论】:

  • 请注意,当您将 nil 传递给 scanForPeripheralsWithServices: 时,您在后台时不会收到任何结果。
  • @mz87 你找到找到BT设备的方法了吗?我也遇到了同样的问题。
  • 我认为 Apple 禁止这件事。我们只能获取具有特定 CBUUID 的设备列表。因此,如果您想列出所有设备(与本机的蓝牙设置相同),那么这是不可能的。如果我错了,请纠正我。
  • @Mrug 6 年后......你错了吗? Apple 是否会阻止您在未指定 UUID 的情况下获取所有连接的设备?我可以找到解决方案。

标签: ios bluetooth core-bluetooth


【解决方案1】:

This guide 看起来很有希望用于蓝牙 3.0。请记住,CoreBluetooth 框架仅适用于低功耗蓝牙 (4.0)。在bluetooth.com's dev-pages,你可以看到一些全局定义服务的例子,正如关阳提到的,你可以看到心率服务是0x180D。单位的 UUID 由制造商定义。

这里有一个代码 sn-p 可能会帮助您。

// Initialize a private variable with the heart rate service UUID    
CBUUID *heartRate = [CBUUID UUIDWithString:@"180D"];

// Create a dictionary for passing down to the scan with service method
NSDictionary *scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];

// Tell the central manager (cm) to scan for the heart rate service
[cm scanForPeripheralsWithServices:[NSArray arrayWithObject:heartRate] options:scanOptions]

【讨论】:

  • 请注意,“蓝牙低功耗,以前称为 Wibree 是蓝牙 v4.0 的子集”en.wikipedia.org/wiki/Bluetooth#Bluetooth_v4.0 因此,如果设备支持 BT 4.0,并不意味着它支持英国电信。
  • @MaratAl 我认为你的理解正好相反;如果设备支持 LE,并不意味着它也应该支持标准 BR/EDR。如果设备上打了蓝牙Logo,按照mu的理解应该是支持LE的。将其命名为“蓝牙智能”可能会使它仅兼容低能耗。
【解决方案2】:

在蓝牙中,有“服务”。一个设备发布1..N服务,每个服务都有1..M特征。每个服务都有一个唯一标识符,称为 UUID。

例如,提供“心率”服务的心率蓝牙传感器发布了具有 UUID 0x180D 的服务。

(更多服务here)

因此,当您执行搜索时,您必须提供 UUID 作为条件,告诉您要搜索的服务。

【讨论】:

    【解决方案3】:

    保持冷静,使用https://github.com/l0gg3r/LGBluetooth

    所有你需要做的

    [[LGCentralManager sharedInstance] scanForPeripheralsByInterval:4
                                                             completion:^(NSArray *peripherals) {
         }];
    

    【讨论】:

    • 不要兴奋同样的结果。如果我去设置/蓝牙,我可以找到一些设备,但是当我使用 LGBluetooth 时,我什么也看不到。
    • @Shady,如果您使用 LGBluetooth(即配对)连接到设备一次,它将显示在“设置”屏幕中,与 CoreBluetooth 的结果相同。即 LGBluetooth 简单地将 CoreBluetooth 包装为基于块(它不添加/删除/更改 BLE 功能,它只是包装)
    • 我不想配对或连接,我只想在“设置”显示时显示所有可用设备。我无法使用 CoreBluetooth 或 LGBluetooth 做到这一点。没有调用“centralManger: didDiscoverPeripheral:..”!
    • @Shady,那个功能应该支持外围硬件,CoreBluetooth和LGBluetooth不会帮你解决这类问题)
    • 保持冷静和死链接:(
    【解决方案4】:

    在扫描设备时尝试提供这个

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
    
     [self.centralManager scanForPeripheralsWithServices:nil options:options];
    

    您将能够通过didDiscoverPeripheral委托方法获取设备列表

    【讨论】:

      【解决方案5】:

      你应该看看one of the examples。这是相关的行:

      [manager scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"180D"]] options:nil];
      

      (我知道这是一个 Mac OS X 示例,但 iOS CoreBluetooth API 非常相似。)

      CBUUID 标识您感兴趣的服务,而不是设备。标准服务有一个 16 位 UUID,在这种情况下,0x180d 用于心率监测器,或者 0x180a 用于设备信息,而专有服务有一个 128 位 UUID(16 字节)。

      大部分设备都实现了设备信息服务,所以如果你只是在寻找任何设备,你可以试试[CBUUID UUIDWithString:@"180A"]。

      【讨论】:

      • 扫描总是给我未知的状态
      猜你喜欢
      • 2014-05-05
      • 2012-06-03
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      相关资源
      最近更新 更多