发现discoverServices后,iOS BLE外围设备会立即断开连接

在为iOS开发BLE应用程序时,在调用discoverServices之后,我会立即断开连接。 我正在testing4个精确的BLE设备(OEM),并且我一直在两个完全相同的设备上获得此断开连接。 每次。 我知道设备是好的,因为我也在Android上构build了相同的应用程序,并且使用相同的设备,所有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实例(你正在工作)。 例如在你的视图控制器中,你需要有一个属性

@属性(强,非primefaces)CBPeripheral * activePeripheral;

分配find的外设activePeripheral,之后做你的工作人员(连接/发现/等…)


我觉得使用像CoreBluetooth这样的Framework并不是取得好成绩的好方法,你需要高水平的,基于块的。 这里是我刚刚为你提供的一个图书馆https://github.com/DavidSahakyan/LGBluetooth

这将使蓝牙生活更容易。

我能够得到CoreBluetooth这个工作,使用一个NSMutableArray来保存对象。 这个诀窍:

 @property (strong, nonatomic) NSMutableArray *discoveredPeripherals; //then in your didDiscoverPeripheral delegate: if ( ![discoveredPeripherals containsObject peripheral]{ [discoveredPeripherals addObject peripheral]; //this right here retains the obj }