我发现我在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上的速度要快十倍(让安卓应用程序感觉到更好的->用户体验),所以我很好奇是我的实现、BLE层还是硬件才是瓶颈。它是在iPhone 4S上测试的。
发布于 2013-10-26 11:58:20
当您加密连接时,iOS应该缓存GATT数据库。因此,第一个发现之后的后续发现调用应该立即发生。
从iOS 7开始,甚至特性值也会被缓存,这意味着您可以通过该特性的value属性读取静态值,如“设备名称”。如果要更新它们,仍然必须发出读取特征值请求。
有关缓存行为的详细信息,请查看幻灯片48中的WWDC 2013年会议703 (可能应该查看视频中相应的部分)。
就连接和发现时间而言,主要是广告间隔。有几个广告间隔是由苹果推荐的最佳表现在苹果产品蓝牙配件设计指南 (第3.5节广告间隔)。此外,当您连接时,您应该禁用扫描,因为扫描会使连接进程减慢大约55倍。
请注意,基于iOS的对每个连接事件发送的数据包数量的限制不应明显地影响发现时间(除非您有一个庞大的GATT数据库并正在查询整个事件)。根据LE协议的设计,这些限制应该只对“写无响应”和“特征值通知”可见。
发布于 2013-10-24 01:41:19
没有框架级的API来提高发现速度。BTLE与经典蓝牙(系统框架级)和Wi(天线级)共存,因此天线上的最大时间受到系统的限制。
https://stackoverflow.com/questions/19404503
复制相似问题