![](/img/trans.png)
[英]CBPeripheral connection keep disconnecting after calling discoverServices
[英]iOS BLE peripherals keep disconnecting immediately after discoverServices is called
在開發適用於iOS的BLE應用程序時,在調用discoverServices之后,我會立即斷開連接。 我正在使用4個完全一樣的BLE設備(OEM)進行測試,並且在完全相同的兩個設備上不斷斷開連接。 每次。 我知道設備還可以,因為我還在Android上構建了相同的應用程序,並且使用相同的設備,所有4個設備始終保持連接。 這使用的是Titanium,但是這里的所有內容都在iOS中實現。 以下是相關的iOS代碼:
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
TiLogMessage(@"[INFO] ===== %@ - centralManagerDidUpdateState: entry",self);
NSString *state = nil;
switch (central.state) {
case CBCentralManagerStatePoweredOn:
state = @"CentralManagerStatePoweredOn";
break;
case CBCentralManagerStateUnknown:
state = @"CentralManagerStateUnknown";
break;
case CBCentralManagerStateResetting:
state = @"CentralManagerStateResetting";
break;
case CBCentralManagerStateUnsupported:
state = @"CentralManagerStateUnsupported";
break;
case CBCentralManagerStateUnauthorized:
state = @"CentralManagerStateUnauthorized";
break;
case CBCentralManagerStatePoweredOff:
state = @"CentralManagerStatePoweredOff";
break;
default:
TiLogMessage(@"[INFO] ===== %@ - centralManagerDidUpdateState default -> break",self);
break;
}
TiLogMessage(@"[INFO] ===== %@ - centralManagerDidUpdateState state changed to: %@",self, state);
NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys:state, @"state", nil];
if ([self _hasListeners:@"centralManagerStateChange"]) {
[self fireEvent:@"centralManagerStateChange" withObject: event];
}
TiLogMessage(@"[INFO] ===== %@ - centralManagerDidUpdateState: exit",self);
}
- (void) connectPeripheral:(id)args
{
TiLogMessage(@"[INFO] ===== %@ : connectPeripheral - entry",self);
NSString* uuid = [[[args objectAtIndex:0] objectForKey:@"peripheral"] objectForKey: @"UUID"];
for (CBPeripheral *peripheral in self.discoveredPeripherals){
if ([[peripheral.identifier UUIDString] isEqualToString:uuid]){
TiLogMessage(@"[INFO] ===== %@ : connectPeripheral - device is %@",self, peripheral);
TiLogMessage(@"[INFO] ===== %@ : connectPeripheral - attempting to connect to %@",self, peripheral.name);
if (self.connectedPeripherals){
TiLogMessage(@"[INFO] ===== %@ : connectedPeripheral - connectedPeripherals is ok...",self);
if (![self.connectedPeripherals containsObject:peripheral]){
TiLogMessage(@"[INFO] ===== %@ : connectPeripheral - device is not connected. Connecting.",self);
if (centralManager.state == CBCentralManagerStatePoweredOn){
[centralManager connectPeripheral:peripheral options:nil];
}
}
else{
TiLogMessage(@"[INFO] ===== %@ : connectPeripheral - device is already connected. %@",self,peripheral);
}
}
}
}
TiLogMessage(@"[INFO] ===== %@ : connectPeripheral - exit",self);
}
- (void) centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
TiLogMessage(@"[INFO] ===== %@ : didConnectPeripheral - entry",self);
if ( [manuallyDisconnectedPeripherals containsObject:[peripheral.identifier UUIDString]] ){
[manuallyDisconnectedPeripherals removeObject:[peripheral.identifier UUIDString]];
}
if (![self.connectedPeripherals containsObject:peripheral]){
[peripheral setDelegate:self];
[self.connectedPeripherals addObject: peripheral];
NSNumber *RSSI = 0;
if (peripheral.RSSI != nil){
RSSI = peripheral.RSSI;
}
NSDictionary *peripheralObj = [NSDictionary dictionaryWithObjectsAndKeys: [peripheral.identifier UUIDString], @"UUID",
peripheral.name, @"name",[NSNumber numberWithInteger:1], @"isConnected", RSSI, @"RSSI", nil];
NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys: peripheralObj, @"peripheral",
[peripheral.identifier UUIDString], @"UUID",nil];
if ([self _hasListeners:@"didConnectPeripheral"]) {
[self fireEvent:@"didConnectPeripheral" withObject: event];
}
}
TiLogMessage(@"[INFO] ===== %@ : didConnectPeripheral - exit",self);
}
- (void) discoverServices:(id)args
{
TiLogMessage(@"[INFO] ===== %@ : discoverServices - entry",self);
ENSURE_SINGLE_ARG(args,NSDictionary);
NSString *peripheralUUID = [[args objectForKey:@"peripheral"] objectForKey:@"UUID"];
TiLogMessage(@"[INFO] ===== %@ : discoverServices - for %@",self,peripheralUUID);
if (self.connectedPeripherals){
for (CBPeripheral *peripheral in self.connectedPeripherals){
if ([[peripheral.identifier UUIDString] isEqualToString:peripheralUUID]){
TiLogMessage(@"[INFO] ===== %@ : discoverServices - , attempting to discover services for %@",self,peripheral);
[peripheral discoverServices: [BLEServicesCBUUIDs count] > 0 ? BLEServicesCBUUIDs : nil];
}
}
}
TiLogMessage(@"[INFO] ===== %@ : discoverServices - exit",self);
}
您需要嚴格保持CBPeripheral實例(您正在使用的實例)。 例如,在您的視圖控制器中,您需要具有一個屬性
@屬性(強,非原子)CBPeripheral * activePeripheral;
將找到的外圍設備分配給activePeripheral,之后再進行工作人員(連接/發現/等...)
我認為像CoreBluetooth這樣使用Framework並不是取得良好結果的好方法,您需要基於塊的高級功能。 這是我剛剛為您提交的一個庫https://github.com/LGBluetooth/LGBluetooth
它將使藍牙的生活更加輕松。
我可以使用NSMutableArray來保存對象,並與CoreBluetooth一起使用。 這達到了目的:
@property (strong, nonatomic) NSMutableArray *discoveredPeripherals;
//then in your didDiscoverPeripheral delegate:
if ( ![discoveredPeripherals containsObject peripheral]{
[discoveredPeripherals addObject peripheral]; //this right here retains the obj
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.