4
votes

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?

1
I know I might not grasp your complete idea from the description, but looks like the Android device advertising itself is unnecessary. If the idea is that the Evalboard is the GATT server, then it should advertise itself. If the Android device is supposed to connect to the server as a GATT client, then it should be scanning for devices. Once the Android device discovers the GATT server device, it can attempt connecting to the GATT server. Now your client is advertising itself and your server is scanning for clients, which seems to be completely backwards to how it's supposed to be done. - Markus Kauppinen
That is exactly the point of this test, we want to inverse the topology in order to figure out if it could be more power efficient and more reliable. - ChiefMiami
i am also trying the achieve the same thing. Is this possible to send small bytes between two android devices using ble in this 1 device work as a gatt and other work as a peripheral device ? - Rajesh Koshti

1 Answers

0
votes

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