-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disconnecting from a peripheral then reconnecting it again results in a false connection callback #189
Comments
I think this scenario should work perfectly fine as I am also doing this in my own apps. I have a suspicion you are calling connect again before the peripheral is fully disconnected because that would explain what you are describing. Can you share a full log that shows this issue? |
I really appreciate the quick reply. I believe you are correct. For a work around, I put in a delay after user disconnects before allowing connection again. This temporarily solves the issue. But the delay I chose is arbitrary and I don't trust it. I am using the library, and not the Blessed source code, so I don't think I have access to the peripheral disconnect callback (or I am not understanding how to do this). I did check the state of the peripheral in the central callback, and that did say it was disconnected (which must be incorrect). Attached is the log. As you can see, a reconnection is initially is indicated in the BluetoothHandler callback (line 445). At line 475, BluetoothPeripheral says it is disconnected and the Gatt is closed. Then sometime later, the auto connect occurs. This is not desirable. Like I said, I believe you are correct. It seems like the peripheral isn't fully connected. I am, however, relying on the BluetoothCentralManagerCallback in BluetoothHandler: Is it possible that I need to somehow gain access to the BluetoothPeripheral disconnect callback and rely only on that? |
OK, I see one thing that I was not implementing. I added onConnectionUpdated() to BluetoothPeripheralCallback peripheralCallback. It looks like your Blessed Example wasn't using that either. Things work better now, but I am getting that callback twice for some reason when connecting. Still looking into it.... I don't think this is an issue with your code, and I appreciate your replies. |
One thing I notice is that the BluetoothPeripheralCallback onConnectionUpdated() triggers when there is a connection, but does not trigger on a disconnect. Am I to assume that I need to use BluetoothPeripheralCallback onConnectionUpdated() to determine a successful connection and BluetoothCentralManagerCallback onDisconnectedPeripheral() to determine a successful disconnection? And... on when connecting, I am getting multiple BluetoothPeripheralCallback onConnectionUpdated() callbacks. Why would this be? |
Connection to a peripheral works well unless it is disconnected then reconnected again. My application allows the user to disconnect from a peripheral then choose from other peripherals seen in the scan. When connecting again to the peripheral previously connected to, this results in the callback BluetoothCentralManagerCallback onConnectedPeripheral saying it was successful. However, this is followed shortly by the callback onDisconnectedPeripheral, saying it disconnected. The peripheral itself doesn't see a connection at this time.
In my debug log, I see that BluetoothPeripheral: reports that it was disconnected 'on request'. This was not requested.
See the log below showing that it connected to the peripheral named 'DDA-D7AF', and shortly after it disconnected.
I/BluetoothHandler: connected to 'DDA-D7AF'
D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=5 device=DCBDCC_F
I/BluetoothPeripheral: disconnected 'DDA-D7AF' on request
I/BluetoothHandler: disconnected 'DDA-D7AF' with status SUCCESS
Note that upon the BluetoothHandler disconnect callback, the autoConnectPeripheral() is sent 5 seconds later, which does finally successfully connect to the peripheral correctly.
If I disconnect then reconnect to a DIFFERENT peripheral and not the one previously connected to, this false connection callback does not occur.
I am disconnecting by using cancel connection:
central.cancelConnection(peripheral);
I have also tried calling the peripheral cancel directly:
peripheral.cancelConnection();
Any ideas on this would be appreciated.
The text was updated successfully, but these errors were encountered: