0

I am making an Android app for a school project, I just need to be able to communicate with a device using BLE.

I am at the point where I am connected to the GATT server of my device, but i can't find any services as onServicesDiscovered callback is never called. I have followed the instructions of android - BLE gatt.discoverServices() returns true but Callback never fires - Stack Overflow and android - onServicesDiscovered never called while connecting to GATT Server - Stack Overflow but it doesn't makes it work...

I also replied to one of these posts, here is my message :

I am currently getting the same issue, without beeing able to correct it with a delay. I even called gatt.discoverServices() using a button to ensure the connection to be done before searching for services but doesn't work either.
When I press this button, I get this gatt.discoverServices() log.
Have you got any idea on how to deal with it ?
Edit: the following two permissions are in my manifest but I can't manage to let the user enable them, they seem to be not granted at start. BUT, all the methods that seemed to need these permissions at this point are working fine (bluetoothAdapter.startDiscovery() or device.getName() for instance).

Manifest.permissions.BLUETOOTH_CONNECT
Manifest.permissions.BLUETOOTH_SCAN

Any idea on how to deal with it ?

Volt
  • 11
  • 3
  • What is the Android version of the device you use for testing? The two Bluetooth permission you have mentioned are Android 12+ only. – Robert Apr 18 '23 at 21:19
  • My phone is on android 8. I removed these permissions and the program "works" as mentionned before (connecting to GATT is ok, but discovering devices gets me the same logs as [gatt.discoverServices() log](https://i.stack.imgur.com/XflMu.png)). I found an exemple program [(See BluetoothLeGatt project)](https://github.com/VoltXd/connectivity-samples) which works well and seems to work approximatly like [my project](https://github.com/VoltXd/MyFirstApp). Any idea ? – Volt Apr 19 '23 at 09:19
  • The permissions you use are exclusive to Android 12+ devices. So on Android below 12 you have to query different permissions: https://stackoverflow.com/q/67722950/150978 – Robert Apr 19 '23 at 09:31

1 Answers1

1

More debug allow me to see that onServicesDiscovered() was called, the real issue was with the BroadcastReceiver. Before calling the function registerReceiver, I forgot to add the action ACTION_GATT_SERVICES_DISCOVERED in the IntentFilter. Adding this action fixed the problem.

Volt
  • 11
  • 3