Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Device as BLE peripheral and GATT client

I am trying to build an Android application which acts as a BLE peripheral and also as GATT client.

These are the steps I am trying to achieve:

  1. The Android device starts advertising itself. A second device starts scanning and as soon as it detects the android device it initiates a connection.

  2. The Android devices acts as a GATT client and tries to connect to the server device.

The first step seems to work fine, I am able to advertise and the other device (a Bluegiga Evalboard in my case) is able to see the Android device and can establish a connection.

But how am I notified on the Android device that a connection could be established in order to call the function device.connectGatt(context, false, gattCb) to connect to the GATT server?

I also tried it by obtaining the remote device after starting the advertisement.

BluetoothDevice device = bleAdapter.getRemoteDevice(remoteAddress); device.connectGatt(context, false, gattCb);

The only thing that happens, the onConnectionStateChange(BluetoothGatt gatt, int status, int newState) function gets called called from the BluetoothGattCallback just to inform me that the device has disconnected.

What am I missing?

like image 427
ChiefMiami Avatar asked Nov 09 '22 09:11

ChiefMiami


1 Answers

I would suggest to try using, after the advertising period, the getBondedDevices method from your BluetoothAdapter instead of the getRemoteDevice one as you did.

like image 198
Roméo Leylekian Avatar answered Nov 15 '22 04:11

Roméo Leylekian