react-native-ble-manager icon indicating copy to clipboard operation
react-native-ble-manager copied to clipboard

[Android] Peripheral not found when retrieving Services

Open BaloMueller opened this issue 5 years ago • 10 comments

Version

Tell us which versions you are using:

  • react-native-ble-manager v6.6.8
  • react-native v59.8
  • iOS/Android Android v.25

Expected behaviour

Should return peripheralInfo, as it always did, and still does on iOS.

Actual behaviour

Returns "Peripheral not found"

Steps to reproduce

  1. Connect to device
  2. Call retrieveServices

Stack trace and console log

I debugged already the Android code, but can't find the reason, why it is not working. The peripherals Map<> is always empty. When a peripheral gets discovered, it is added to the peripherals Map. But when retrieveServices() gets invoked, the Map is always empty. Can someone reproduce the issue?

BaloMueller avatar Jul 12 '19 23:07 BaloMueller

Maybe is something related to this commit . Do you use the forceLegacy flag?

marcosinigaglia avatar Jul 13 '19 11:07 marcosinigaglia

I agree, it is probably related to the commit. Are you able to reproduce? No I don't use the forceLegacy flag.

BaloMueller avatar Jul 13 '19 11:07 BaloMueller

No, I tested on different Android with no problem.

marcosinigaglia avatar Jul 13 '19 11:07 marcosinigaglia

forceLegacy doesn't fix the problem.

BaloMueller avatar Jul 14 '19 16:07 BaloMueller

Unfortunately I'm not experienced enough with the Android Platform to track the root cause down. I downgraded to Version 6.6.5, which solved the problem for me.

BaloMueller avatar Jul 16 '19 22:07 BaloMueller

I'm having the same issue on a Galaxy Note 9 running Android 9. My previous app version was using React Native 0.56 and react-native-ble-manager 6.6.2 and it was working. Since I updated to React Native 0.60 it's not working anymore even with 6.6.2 or 6.6.8. I'm going to do some checks hope to help.

iltumio avatar Aug 21 '19 16:08 iltumio

@BaloMueller I found the root cause for my case.
I was calling startScan at every scan stop event if no devices was already connected. The problem was that the setState I was calling right after the connection was not fast enough to prevent the startScan to be called again. As I can see from the native code the "peripherals" map will be empty whenever a new scan starts

iltumio avatar Aug 22 '19 07:08 iltumio

I think is better to use the BleManagerDiscoverPeripheral event to save the peripherals in your app.

marcosinigaglia avatar Aug 23 '19 08:08 marcosinigaglia

I think is better to use the BleManagerDiscoverPeripheral event to save the peripherals in your app.

I'm using it. But I'm scanning and searching for a specific device. If I don't find it I need to rescan.

iltumio avatar Aug 23 '19 09:08 iltumio

@BaloMueller I found the root cause for my case. I was calling startScan at every scan stop event if no devices was already connected. The problem was that the setState I was calling right after the connection was not fast enough to prevent the startScan to be called again. As I can see from the native code the "peripherals" map will be empty whenever a new scan starts

Thank you so much for this, this just really helped us out. This should be in the documentation of the scan or retrieveServices function, as many people are likely performing continuously looping scans which will cause problems here.

NeilMcGuiness avatar Jun 29 '20 13:06 NeilMcGuiness