gpt4 book ai didi

ios - CoreBluetooth - didDiscoverPeripheral 从未调用过特定的 serviceUUID,但适用于 nil

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:08:48 26 4
gpt4 key购买 nike

我正在扫描蓝牙设备。为此,我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com