- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在扫描蓝牙设备。为此,我正在使用 CBCentralManager
,就像这样
- (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 应用程序在其他设备上模拟 Beacon。控制台输出如下所示:
2017-07-26 14:43:56.711830 ExampleApplication[445:55698] peripheral:
6A3E0CB4-74BB-4472-98AA-0C9DEB1D4551, rssi: -28
到目前为止一切都很好。
现在,我还想在应用程序处于后台时扫描蓝牙设备。为此,我将此类属性添加到 Info.plist
<key>UIBackgroundModes</key>
<string>bluetooth-central</string>
此外,我了解到当将 nil
作为 serviceUUIDs
传递时,无法在后台扫描蓝牙设备,因此我使用之前发现的外围设备的 UUID 并传递它到 CBCentralManager
CBUUID *uuid = [CBUUID UUIDWithString:@"6A3E0CB4-74BB-4472-98AA-0C9DEB1D4551"];
[self.centralManager scanForPeripheralsWithServices:@[uuid] options:options];
还有一个问题——didDiscoverPeripheral
再也不会被调用,即使应用程序在前台也是如此。是什么原因?我使用之前发现的设备的 UUID 没有问题,但事实并非如此。有什么想法吗?
最佳答案
-(void)centralManagerDidUpdateState:(CBCentralManager *)central {
if (central.state != CBCentralManagerStatePoweredOn) {
return;
}
if (central.state == CBCentralManagerStatePoweredOn) {
[_centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@"0x180A"]] options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @YES}];
NSLog(@"Start scanning");
}
}
你可以这样检查。它可以帮助您找出问题所在
编辑:
好吧,如果你已经尝试过上面的代码。
随着 scanForPeripheralsWithServices 被弃用,理想的方法是在 swift 中使用新代码。
使用:像这样的东西来检查func scanForPeripherals(withServices serviceUUIDs: [CBUUID]?, 选项:[字符串:任何]? = 无)
否则,您的硬件现在可能正确或正在正确广播。
关于ios - CoreBluetooth - didDiscoverPeripheral 从未调用过特定的 serviceUUID,但适用于 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45327714/
我正在尝试使用 swift 列出当前通过蓝牙连接到我的 iPhone 的设备。我想要任何设备(BLE、经典蓝牙……) 我在 Xcode 7.3 和 swift 2.2 中使用 CoreBluetoot
我正在扫描蓝牙设备。为此,我正在使用 CBCentralManager,就像这样 - (void)startScanning { [self.centralManager scanForPer
我是一名优秀的程序员,十分优秀!