私はBLEに関連する小さなプロジェクトに取り組んでいます。iPhone->設定->BluetoothからBluetoothを手動でオフにしてからオフにした後、バックグラウンドでデバイスを再接続する必要があるという要件があります。
1957 次
1 に答える
2
周辺機器の識別子または (iOS 7 未満の場合は UUID) を保存し、周辺機器を取得して、centralManager が状態を電源オンに更新したときに接続を呼び出します。
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
また、その更新を取得したら、(特に)監視し、現在のすべての周辺機器接続をキャンセルする必要があります。
于 2013-09-30T17:45:55.007 に答える