【发布时间】:2023-03-17 11:40:01
【问题描述】:
我发现我在 iOS7 中实现的 BLE 协议在启动阶段非常缓慢。在我的应用程序中,启动顺序约占整个执行时间的 68%。
我该怎么做才能让它更快?
我已经计时了,这就是我得到的。
t dt
37.598 [BLE] Discovered peripheral at RSSI -27 with UUID:XYZ
37.599 0.001 [BLE] Connecting to peripheral
37.602 0.003 [BLE] Scanning stopped
37.685 0.083 [BLE] Peripheral connected
38.48 0.795 [BLE] Discovered service
38.599 0.119 [BLE] Discovered characteristic
如您所见,在发现服务之前存在巨大的瓶颈。
我的启动代码简化了:
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
switch (central.state) {
case CBCentralManagerStatePoweredOn:
[central scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:kServiceUuid]]
options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @YES}];
break;
case CBCentralManagerStatePoweredOff:
[central stopScan];
break;
default:
break;
}
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
if (self.discoveredPeripheral != peripheral) {
self.discoveredPeripheral = peripheral; // Save a local copy of the peripheral, so CoreBluetooth doesn't get rid of it
[central connectPeripheral:peripheral options:nil];
[central stopScan];
}
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
[peripheral discoverServices:@[[CBUUID UUIDWithString:kServiceUuid]]];
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:@[array of characteristics]
forService:service];
}
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
...
}
编辑
我了解到,Android 上的类似应用程序在这方面的速度要快十倍(让 Android 应用程序感觉更快捷 -> 更好的用户体验),所以我很好奇是我的实现、BLE 层还是硬件是瓶颈。它在 iPhone 4S 上进行了测试。
【问题讨论】:
-
我很好奇您花了多长时间发现外围设备。我的猜测是发现外设及其服务的时间与它们的硬件实现有关。
-
根据我的经验,以较高速率宣传自己的外围设备将花费更短的时间来发现服务。不过可能是巧合,因为我只有几个外围设备可以玩。此外,我发现外设服务的最长时间约为 0.5 秒,这对我来说是可以接受的。
-
我再次测试了两个完全不同(硬件方面)的外围设备,结果是相同的(给或需要 50 毫秒)。我不确定如何在发现外围设备之前测量时间?发现外围设备和发现服务之间的增量时间仍然非常重要,因为它使应用程序感觉比它可能的更迟钝。
-
您使用什么类型的设备..?我尝试使用 CiragoBLE BT8000 加密狗.. 但它没有检测到.. 请列出一些用于在 ios 中检测的蓝牙加密狗。提前致谢!
-
我没有使用加密狗,我使用的是专门为与 BLE 设备(不仅是 iOS 设备)通信而构建的单独的 BLE 外设。您是否尝试过使用 LightBlue 检测您的加密狗? itunes.apple.com/gb/app/lightblue-bluetooth-low-energy/…
标签: objective-c ios7 bluetooth-lowenergy