最近做一个硬件设备,写IOS相应的数据连接/分析代码时;发现一个问题,如果是开机,每次都能连接上。连接断开后,发现再也扫描不到了。通过第三方工具LightBlue,发现信号是-127。
此时进入设置查看蓝牙设备,发现该设备处于连接状态。所以发现不了该设备。
此问题解决方案:搜索前,先通过
retrieveConnectedPeripheralsWithServices
搜索已连接设备,是否存在当前应用需要的设备。存在时,不进行扫描,直接连接设备。
不存在时,进行扫描。
搜索示例代码:
-(int) Search_RetrieveConnPeri
{
NSArray *uuidArray = [NSArray arrayWithObjects:[CBUUID UUIDWithString:DEVICE_INFO_SERVICE],[CBUUID UUIDWithString:DEVICE_BATTER_SERVICE], [CBUUID UUIDWithString:kConnectedServiceUUID1], [CBUUID UUIDWithString:OAT695_SERVICE],nil];
NSArray* allConnected=[centralManager retrieveConnectedPeripheralsWithServices:uuidArray];
if(allConnected.count>0)
{
for(int i=0;i<allConnected.count;i++)
{
CBPeripheral *per = allConnected[i];
NSLog(@"retrie conn names: %@",per.name);
if([per.name hasPrefix:KNAME_DEVICE]){
[self Connect_Peri:per];
return 1;
}
}
//CBPeripheral *per = allConnected[0];
}
return 0;
}