我發現我在iOS7中實現BLE協議的啓動階段非常慢。在我的應用程序中,啓動順序佔整個執行時間的約68%。有沒有辦法更快地發現BLE外設服務?
我該怎麼做才能讓它更快?
我已經計時了,這裏是我得到的。
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上進行了測試。
我很好奇你花了多長時間來發現外設。我的猜測是發現外圍設備的時間,他們的服務與他們的硬件實施有關。 – reTs
根據我的經驗,以較高速率宣傳自己的外設將花費較短的時間來發現服務。也許巧合,因爲我只有幾個外圍設備可以玩。另外我發現一個外設服務的時間最長爲0.5秒,這對我來說是可以接受的。 – reTs
我再次測試了兩個完全不同的(硬件方面)外設,結果是相同的(給出或花費50毫秒)。我不確定如何在發現外設之前測量時間?發現外圍設備和發現服務之間的差異時間仍然非常重要,因爲它會讓應用程序感覺比它更慢。 – MdaG