-(void) disconnect { if (_selectedPeripheral != nil && _selectedPeripheral.state != CBperipheralstatedisconnected) { NSLog(@"Peripheral disconnecting"); [_centralManager cancelPeripheralConnection:_selectedPeripheral]; _selectedPeripheral = nil; } }
当我单击按钮时,上面的方法是调用和应用程序显示外围设备已断开连接,当我从应用程序中出来并查看设置/蓝牙/ .Peripheral正在显示已连接.如何停止连接外围设备级别,即在设置.请帮我解决问题.
解决方法
这是直接来自CBCentralManager文档的链接:
cancelPeripheralConnection:
discussion
This method is nonblocking,and any CBPeripheral class
commands that are still pending to peripheral may or may not complete.
Because other apps may still have a connection to the peripheral,
canceling a local connection does not guarantee that the underlying
physical link is immediately disconnected. From the app’s perspective,
however,the peripheral is considered disconnected,and the central
manager object calls the centralManager:diddisconnectPeripheral:error:
method of its delegate object.
根据我的经验,如果您是使用外围设备的唯一应用程序,物理链接会很快断开连接,但如果您可能不像Apple明确指出的那样,其他应用程序可能会维持持久连接,这会导致物理链接不能尽管它已经向你说明它已经断开了.