gpt4 book ai didi

iOS CBPeripheral 连接问题

转载 作者:行者123 更新时间:2023-11-29 01:23:32 25 4
gpt4 key购买 nike

我调用方法

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

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

如果不关闭BT再开启,是否可以调用first call方法

(void)cancelPeripheralConnection:(CBPeripheral *)peripheral;

然后调用

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

这有意义吗?

最佳答案

要检查天气您的设备是否已连接,您可以使用这两个委托(delegate)方法: objective-c :

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

2) -(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)外设错误:(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]];
}

自定义委托(delegate):

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

swift :

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

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

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

let heartRatePeripheral : CBPeripheral

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

自定义委托(delegate):

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

关于iOS CBPeripheral 连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34310166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com