bluetooth lowenergy – Android 4.3 BTLE as server: How to start advertisements?-ThrowExceptions

Exception or error:

I am trying to implement a BTLE SERVER on the Nexus 7 with the new BTLE API in 4.3. I am running into several problems. First there are no examples with the SDK. The only example is for a client. Second, the documentation actually tells you to do the wrong thing. It states that one must use the BluetoothAdapter.getProfileProxy() with a BluetoothProfile.GATT_SERVER parameter to obtain the BluetoothGattServer object. This approach will work, but one will be unable to link one’s implementation of the BluetoothGattServerCallback to the BLE stack. (This callback is how one responds to client read and write requests among other things.) However, after stumbling on issue 58582 a developer pointed to the new BluetoothManager.openGattServer() method which takes your callback as a parameter and returns a BluetoothGattServer object. Well, one problem solved.

The next issue is more problematic. The BluetoothGattServer documentation states that one can use this class to create and advertise Bluetooth LE services and characteristics. Creating the services, etc. was not problem but they neglect to say how to start advertising. There is no method in the class itself or any other of the classes that I can find.

Does anyone know how to do this? At the moment all I can see is to use the same approach as used by the client, but that approach involves scanning (which is not advertising). All the documentation further suggests that the BluetoothAdapter.startLeScan() IS indeed JUST for scanning.

So how do I invoke advertisements once all my services, characteristics, and descriptors are in place?

How to solve:

As I understand, the Android implementation can only act as a central device, and not as a peripheral device. In Bluetooth Low Energy, only the peripheral can advertise. The central device can scan for advertisements from peripherals, and send connect requests as replies to (some kinds of) advertisements, to create a connection to the peripheral.

In BLE, there is a distinction between the concepts Central/Peripheral and Server/Client:

  • Central/Peripheral is relating to the network architecture, where the central is the hub in a star, with one or more peripherals connected to it. It will typically be a phone, tablet or computer. A peripheral device can only connect to one central at a time.

  • Server/Client (GATT server/client) is a higher level concept, related to the data that are kept in the devices and possibly communicated over the connection. Both central and peripheral devices can implement a GATT server and a GATT client, but is not required to have both.

So to answer your question: You cannot invoke advertisements. You will have to start scanning for peripheral devices to be able to make a connection to one or more of them.

Hope this helps.

###

You will need API Level 21.

import android.bluetooth.le;
...
...
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter myBluetoothAdapter = bluetoothManager.getAdapter();
BluetoothLeAdvertiser myBluetoothLeAdvertiser =  myBluetoothAdapter.getBluetoothLeAdvertiser ();
myBluetoothLeAdvertiser.startAdvertising (AdvertiseSettings settings, AdvertiseData advertiseData, AdvertiseCallback callback);

Useful link is : https://developer.android.com/about/versions/android-5.0.html

###

It seems the getProfileProxy does not respond to the GATT or GATT_SERVER request. The API suggests advertising support, but there is no code implemented behind yet. (Android Issue Tracker)

Same half-way implemented APIs were released initially when NFC was introduced and Google iterated adding more well rounded functionality with follow-up releases.

###

As I see it, BLE advertising ability (aka peripheral mode) will be added to Android with the upcoming version 4.4.3 of Kitkat.
It’s supposed to be released next week, but the changelog has already been leaked accidentially, see Google cache or here in line 2554: peripheral mode (3/4): Add peripheral mode API.

I guess we will know more soon.

Leave a Reply

Your email address will not be published. Required fields are marked *