2017-07-26 35 views
2

我正在扫描蓝牙设备。对于我使用CBCentralManager,像这样CoreBluetooth - didDiscoverPeripheral从未呼吁具体的serviceUUID,但为零作品

- (void)startScanning 
{ 
    [self.centralManager scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }]; 
} 

- (void)centralManager:(CBCentralManager *)central 
didDiscoverPeripheral:(CBPeripheral *)peripheral 
    advertisementData:(NSDictionary *)advertisementData 
        RSSI:(NSNumber *)RSSI 
{ 
    NSLog(@"\n%@, rssi: %@", peripheral.identifier.UUIDString, RSSI); 
    // do stuff 
} 

我使用Estimote应用来模拟其他设备的信标。控制台输出看起来像这样:

2017年7月26日14:43:56.711830 ExampleApplication [445:55698]周:
6A3E0CB4-74BB-4472-98AA-0C9DEB1D4551,RSSI:-28

目前一切都很好。

现在,我还想在应用程序处于后台时扫描蓝牙设备。对于这一点,我添加了这样的属性Info.plist

<key>UIBackgroundModes</key> 
<string>bluetooth-central</string> 

而且,我读过,这是不可能的传球nilserviceUUIDs当扫描在后台的蓝牙设备,所以我采取了以前发现的外围设备的UUID,并将其传递到CBCentralManager

CBUUID *uuid = [CBUUID UUIDWithString:@"6A3E0CB4-74BB-4472-98AA-0C9DEB1D4551"]; 
[self.centralManager scanForPeripheralsWithServices:@[uuid] options:options]; 

还有的问题 - didDiscoverPeripheral永远不会再调用,即使当应用程序在前台。是什么原因?我使用之前发现的设备的UUID没有问题,但并非如此。有任何想法吗?

+0

您需要扫描由外设通告的服务的uuid,而不是外设的uuid。外围设备的uuid可以改变,不同的iOS设备会在同一个外围设备上看到不同的uuid,但服务外设是一致的。 – Paulw11

+0

问题是,对于我收到的任何外设,“services”属性为空 –

+0

@mag_zbc如果您的外设没有放入其具有某项服务的广告(通过提供服务UUID),则无法专门扫描因为如果它不通告它,你需要连接然后扫描它的服务。 – Larme

回答

0
-(void)centralManagerDidUpdateState:(CBCentralManager *)central { 

if (central.state != CBCentralManagerStatePoweredOn) { 
    return; 
} 
if (central.state == CBCentralManagerStatePoweredOn) { 
    [_centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@"0x180A"]] options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @YES}]; 
    NSLog(@"Start scanning"); 
} 
} 

你可以这样查看。它可以帮助你找出什么问题

编辑:

好吧,如果你有上面的代码尝试。

然后,理想的方式是使用swift中的新代码,因为scanForPeripheralsWithServices已弃用。

用途:是这样的退房 FUNC scanForPeripherals(withServices serviceUUIDs:[CBUUID]? 选项:[字符串:任何] =无)

否则它可能是一个可能性,你的硬件现在正确或广播正确。

+0

设备信息\t org.bluetooth.service.device_information \t 0x180A – cole

+0

我已经核对过了。这并不能解释为什么当''nil'作为'serviceUUIDs'传递时,我可以很好地发现外设 –