CoreBluetooth: Disconnect peripheral connection from app

My app is connected with bluetooth communication with a peripheral. Every functionality works great, from discovery to connectivity. If we start to disconnect the peripherals from the application, I wrote a code like this

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

      

When I click the button, this method calls the call and the app showing that the peripheral is disabled and when I quit the app and look into the / bluetooth / settings. The periphery shows the connection. How to stop connecting a peripheral at the device level i.e. in settings. Please help me with the correct solution.

+3


source to share


3 answers


You cannot guarantee system-level disconnection from peripherals.

This is the link directly from the documentation CBCentralManager

:



cancelPeripheralConnection:

Discussion

This method does not block, and any CBPeripheral instruction that is still at the periphery may or may not complete. Since other applications still have peripheral connections, canceling the local connection does not guarantee that the physical link is immediately disconnected. From an application perspective, however, the peripheral is considered disconnected and the central management object calls the centralManager: didDisconnectPeripheral: error: method of its delegate object.

In my experience, physical connection gets disconnected quickly if you are the only app using a peripheral, but if you might not, as Apple clearly states, there is a possibility that other apps will maintain a persistent connection, leading to a physical link so as not to disconnect even if she tells you.

+4


source


I know this is an old thread, but I figured I would add a potential solution for others.



What you can do is issue a command that makes the peripheral reboot, rebooting the bluetooth connection. If your device has a command like this that you can execute via Bluetooth, you're in luck, otherwise you'll need access to the firmware for the peripheral to add a new command that does this. I'm not a firmware, so I can't tell you exactly what you need to do; all i know is that the device i'm working with has a command like this (this is its own command specifically for our device, not part of the bluetooth protocol), and this made it possible to ensure that disconnection is guaranteed as long as i issue it before a challenge cancelPeripheralConnection

.

+1


source


We faced the same problem, but we managed to get around this nasty bug (or API design flaw) by using

Objective C

[peripheral writeValue:x forCharacteristic:y type:CBCharacteristicWriteWithResponse];

      

Swift

peripheral.writeValue(x, for: y, type: .withResponse)

      

The weird thing is that iOS doesn't override the physical connection to the peripheral that we implemented in the peripheral in order to do the disconnect, so we send a string indicating the disconnect. We used

Objective C

[peripheral writeValue:x forCharacteristic:y type:CBCharacteristicWriteWithoutResponse];

      

Swift

peripheral.writeValue(x, for: y, type: .withResponse)

      

and the peripherals are disconnected as expected. Hope this helps anyone facing the same issue with this CoreBluetooth API flaw.

x is the specific cmd supported on your peripheral (like firmware)

y is the specific characteristic you want to send the value

0


source







All Articles