【问题标题】:iOS CBPeripheral connect QuestionsiOS CBPeripheral 连接问题
【发布时间】:2015-12-16 10:57:06
【问题描述】:

我调用方法

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

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

如果我不关闭BT再打开,我可以调用第一个调用方法

(void)cancelPeripheralConnection:(CBPeripheral *)peripheral;

然后调用

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

这有意义吗?

【问题讨论】:

  • 更好的调用:-connectPeripheral:options:-cancelPeripheralConnection:

标签: ios cbperipheral bluetooth-lowenergy


【解决方案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]];
}

自定义委托:

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

斯威夫特:

1) func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)

2) func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?)

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 ?? "")")
}

【讨论】:

    猜你喜欢
    • 2013-01-30
    • 2022-08-02
    • 2013-03-28
    • 2014-04-22
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 1970-01-01
    相关资源
    最近更新 更多