dotintent / react-native-ble-plx

React Native BLE library
Apache License 2.0
3.07k stars 512 forks source link

[Android] Connect and discover services - BleError: Device 00:16:A4:4C:B9:6A was disconnected #734

Closed Sumukha1791 closed 4 years ago

Sumukha1791 commented 4 years ago

Prerequisites

Please answer the following questions for yourself before submitting an issue.

Expected Behavior

Should be able to connect and discover services

Current Behavior

Receiving a disconnection error with status code 61 when trying to connect and discover services for a BLE device using an Android phone. The way I have designed my code is that when I receive an error in connection request from react-native-ble-plx library I again try to connect to the BLE device in a loop for 5 attempts, if all the 5 attempts result in error on 6th attempt I pass refershGatt param with value onconnected to the connect call. This does clear the cache and I am able to connect to the device but the service descriptions are empty. The device I try to connect uses Bluetooth dongle so the dongle might be paired to the Android phone in a previous attempt so when I manually unpair/forget the Bluetooth dongle from the paired devices list it does successfully discover the services. I am expecting to connect to the BLE device without manually unpairing the BLE dongle from the paired devices list in the android phone.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. Connect to a device using Bluetooth dongle
    bleManager.connectToDevice(deviceId)
  2. Then discover the services and characteristics
    bleManager.discoverAllServicesAndCharacteristicsForDevice(deviceId)
  3. Disconnect from device
    bleManager.cancelDeviceConnection(deviceId)
  4. Connect and try to discover services to a different device using the same Bluetooth dongle but pass refreshgatt parameter with the connect request.
    bleManager.connectToDevice(deviceId, { refreshGatt: 'OnConnected' })

Context

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

jnilesh commented 3 years ago

while trying to connect to the device, I too get the same error multipleTimes

nolife08021 commented 2 years ago

@Sumukha1791 @jnilesh hi did u guys found any solution for this? I'm facing the same issue.

Hassaan68 commented 2 years ago

@Sumukha1791 did you find any solution for this issue?

Tadejo9 commented 2 years ago

@Hassaan68 what versions of react native, library and os are you using?

Tadejo9 commented 2 years ago

I'm quoting this from official documentation of this library.

To connect to a device one needs to first scan it.

And this is the url of the "Connect" page of documentation https://github.com/dotintent/react-native-ble-plx/wiki/Device-Connecting

Hassaan68 commented 2 years ago

@Tadejo9 . react native version 0.66.4. Device is iphone 12 (ios 15.4.1)

Tadejo9 commented 2 years ago

@Hassaan68 have you tried scanning for devices?

Hassaan68 commented 2 years ago

@Tadejo9 the error occurs when update the device firmware, remove device from bluetooth pairing and then tries to reconnect to it. It just does not reconnect in first try and after the error is shown when I try to connect it then it works fine as before.

Hassaan68 commented 2 years ago

@Hassaan68 have you tried scanning for devices?

yes . I do scan devices every time before reconnecting

Tadejo9 commented 2 years ago

The error above is an Android error. Can you paste yours below?

Hassaan68 commented 2 years ago

@Tadejo9 the error is same as @Sumukha1791. error code 201.

Tadejo9 commented 2 years ago

@Tadejo9 the error occurs when update the device firmware, remove device from bluetooth pairing and then tries to reconnect to it. It just does not reconnect in first try and after the error is shown when I try to connect it then it works fine as before.

Sorry i did not see this comment before, i guess i must have lost signal. Try handling the error. If this error occurs "restart" the pairing process. There are many ways of doing that.

Hassaan68 commented 2 years ago

@Tadejo9 I have tried the suggested solution and called the connect method again when this error occurs. Is there any way to clear out any cache in this library. I think it is the cache issue. because library tries to connect to the last connected device

Tadejo9 commented 2 years ago

@Hassaan68 where do you define deviceId that you want to connect to? I've never had a problem where library tried to connect to last connected device even though different id was passed.

Hassaan68 commented 2 years ago

@Tadejo9 the device Id remains same because only firmware version is updated and Id is not changed. the device was disconnected before firmware update by using the manager.cancelDeviceConnection(deviceId) method.

Hassaan68 commented 2 years ago

@Tadejo9 . one more update. If I disconnect the device and remove from bluetooth pairing and then reconnects the device without firmware update then it works fine. It is worth noticing that firmware update changes the few characteristics of the device. It can be case that library is not aware of these changes and tries to reconnect to device by using the previous characteristics ?