iOS CBPeripheral连接问题

时间:2015-12-16 10:57:06

标签: ios cbperipheral bluetooth-lowenergy

我叫方法

void)connectPeripheral:(CBPeripheral *)peripheral options:(nullable NSDictionary<NSString *, id> *)options;

连接我的设备,我的设备在范围内,但我的应用程序仍然无法连接我的设备成功,当我关闭系统BT然后打开BT时,我的应用程序将连接我的设备成功,我不#39;知道为什么吗?

如果我没有关闭BT然后打开,我可以拨打第一个电话方法

(void)cancelPeripheralConnection:(CBPeripheral *)peripheral;

然后致电

void)connectPeripheral:(CBPeripheral *)peripheral options:(nullable NSDictionary<NSString *, id> *)options;

它会有意义吗?

1 个答案:

答案 0 :(得分:1)

要检查天气 您的设备是否已连接 ,您可以使用以下两种委托方法: 目标C:

1)-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral

2)-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error

@property (nonatomic, strong) CBPeripheral *heartRatePeripheral;
#define heartRateServiceCBUUID [CBUUID UUIDWithString:@"0x180D"]

-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral 
  *)peripheral
{ 
  [self.Customdelegate HRMoniterStatus:peripheral Status:@"Connected"];
  [_heartRatePeripheral discoverServices:@[heartRateServiceCBUUID]];
}

CustomDelegate:

-(void)HRMoniterStatus:(CBPeripheral *)HRMoniter Status:(NSString 
*)connectionStatus
{
  NSLog(@"HRMoniter connection status ::- %@",connectionStatus);
}

快捷键:

1)func centralManager(_中央:CBCentralManager,didConnect外围设备:CBPeripheral)

2)func centralManager(_ Central:CBCentralManager,didDisconnectPeripheral外设:CBPeripheral,错误:错误?)

let heartRateServiceCBUUID = CBUUID(string: "0x180D")

let heartRatePeripheral : CBPeripheral

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)
{
    Customdelegate.hrMoniterStatus(peripheral, status: "Connected")
    heartRatePeripheral.discoverServices([heartRateServiceCBUUID])
}

自定义委托:

func hrMoniterStatus(_ HRMoniter: CBPeripheral?, status connectionStatus: String?) 
{
   print("HRMoniter connection status ::- \(connectionStatus ?? "")")
}