NordicSemiconductor / Android-BLE-Library

A library that makes working with Bluetooth LE on Android a pleasure. Seriously.
BSD 3-Clause "New" or "Revised" License
2.04k stars 419 forks source link

connect fail and issue status = -2 #418

Closed nirbhaymanoj closed 2 years ago

nirbhaymanoj commented 2 years ago

I am trying to connect to a BLE device but the thing is the device gets connected but within 1 or 2 seconds it gets disconnected showing status error of -2. Any help would be appreciated.

code snippet :

 bleManager.connect(device).
    retry(3,100).
    timeout(10000).
    useAutoConnect(false).
    done(device1 -> Log.d(">>>>>","connected to device")).
    fail((device1, status) -> Log.d(">>>>>","Connect request failed : " + status)).
    then(device1 -> Log.d(">>>>>","then clause" + device1)).
    enqueue();

LOGS:

2022-09-14 09:33:41.969 13553-13553/com.example.fnv_traceability_android D/>>>>>: inside broadcast receiver on receive
2022-09-14 09:33:41.970 13553-13553/com.example.fnv_traceability_android D/>>>>>: inside broadcast receiver connected
2022-09-14 09:33:43.662 13553-13553/com.example.fnv_traceability_android D/>>>>>: Connect request failed : -2
2022-09-14 09:33:43.662 13553-13553/com.example.fnv_traceability_android D/>>>>>: then clauseB8:D6:1A:10:C9:D2
2022-09-14 09:33:44.710 13553-13553/com.example.fnv_traceability_android D/>>>>>: inside broadcast receiver on receive
2022-09-14 09:33:44.711 13553-13553/com.example.fnv_traceability_android D/>>>>>: inside broadcast receiver disconnected
philips77 commented 2 years ago

The error -2 is defined here: https://github.com/NordicSemiconductor/Android-BLE-Library/blob/52d60a4de110b73faef05938ea31a699a9a38163/ble/src/main/java/no/nordicsemi/android/ble/callback/FailCallback.java#L32 It is thrown when this method https://github.com/NordicSemiconductor/Android-BLE-Library/blob/52d60a4de110b73faef05938ea31a699a9a38163/ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java#L1478-L1485 returns false. By design it should return false if required GATT service has not been found on the device, which may happen in the following cases:

  1. You've connected to a wrong device, or the correct firmware has not been flashed,
  2. Sometimes, if the method mentioned above checks also for properties of the characteristics, i
  3. You've flashed a new firmware on the device, but Android has cached services of the previous one, and returning the old ones from getServices(),
  4. Service Change indication has not been received after the device has changed services (when pairing is used and the device can switches modes).

Please, use nRF Connect to verify what services are on the device, refresh cache if necessary and compare it to your implementation of the BLE manager.