2
我正在研究一個與BLE相關的小項目。我有一個要求,我需要在手動關閉後在後臺重新連接設備,然後從iPhone->設置 - >藍牙關閉藍牙。關閉並重新打開後,如何在後臺重新連接BLE設備?
我正在研究一個與BLE相關的小項目。我有一個要求,我需要在手動關閉後在後臺重新連接設備,然後從iPhone->設置 - >藍牙關閉藍牙。關閉並重新打開後,如何在後臺重新連接BLE設備?
只需存儲外圍設備標識符或(UUID爲< iOS 7),檢索外圍設備,並在中央管理器更新狀態爲啓動時調用連接。
對於iOS 7:
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
if(central.state == CBCentralManagerStatePoweredOn)
{
NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:savedUUID];//where savedUUID is the string version of the NSUUID you've saved somewhere
NSArray *peripherals = [_cbCentralManager retrievePeripheralsWithIdentifiers:@[uuid]];
for(CBPeripheral *periph in peripherals)
{
[_cbCentralManager connectPeripheral:periph options:nil];
}
}
}
對於iOS 6:
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
if(central.state == CBCentralManagerStatePoweredOn)
{
CFUUIDRef uuid;//the cfuuidref you've previously saved
[central retrievePeripherals:@[(id)uuid]];//now wait for the delegate callback below
}
}
- (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals
{
for(CBPeripheral *periph in peripherals)
{
[_centralManager connectPeripheral:periph options:nil];
}
}
注:這些只是代碼段。您還應該監視CBCentralManagerStatePoweredOff
(其中包括)並取消所有當前的外圍設備連接。