Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreBluetooth:Disconnect peripheral Connection from application

My application related to bluetooth communication with the peripheral device.Every functionality is working fine right from discovering to connecting .While coming to disconnecting the peripheral from the application i have written code like this

  -(void) disconnect
 {
 if (_selectedPeripheral != nil &&
    _selectedPeripheral.state != CBPeripheralStateDisconnected)
  {
    NSLog(@"Peripheral disconnecting");
    [_centralManager cancelPeripheralConnection:_selectedPeripheral];
    _selectedPeripheral = nil;
  }
 } 

When i click button this above method is calling and app showing that peripheral is disconnected and when i came out of the application and look into settings /bluetooth/ .Peripheral is showing connected.How to stop connection the peripheral in the device level i.e in the settings .Please help me with the proper solution.

like image 980
dev Avatar asked Dec 19 '22 04:12

dev


1 Answers

You are unable to guarantee a system level disconnect from the peripheral.

This is a link directly from the CBCentralManager documentation:

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.

In my experience the physical link is disconnected quickly if you are the only application using the peripheral, but if you potentially are not as Apple clearly states there is a potential for other applications to be maintaining a persisted connection which would cause the physical link to not disconnect even though it is stating to you it has.

like image 157
Robert Haworth Avatar answered Dec 21 '22 22:12

Robert Haworth