我发现我的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 {
...
}
编辑
我了解到安卓上类似的应用程序执行速度比这快十倍(使安卓应用感觉更加迅捷 -> 更好的用户体验),因此我很好奇是我的实现、BLE层还是硬件成为了瓶颈。它在iPhone 4S上进行了测试。