Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BLE with Android 5.0 : How to get a device to act as Central AND Server?

I'm using two Android 5.0 devices to communicate through Bluetooth Low Energy and I wan't :

  • Device 1 to act as Central and Server.

  • Device 2 to act as Peripheral and Client.


This is the behavior I'd like to achieve :

1) Device 2 starts advertising (peripheral role).

2) Device 1 starts scanning (central role), and gets the advertising device (BluetoothDevice object) through the ScanCallback's onScanResult method.

3) I now want the advertising device (Device 2) to be notified that it has been scanned and be able to get the BluetoothDevice associated with Device 1.

4) Device 1 has an instance of BluetoothGattServer. Device 2 would now call connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback) on Device 1 to get an instance of BluetoothGatt.

5) In the end, Device 1 is Server and Device 2 is Client.


So far I've found that in step 2, once Device 1 holds the BluetoothDevice for Device 2, it can only connect as client like in step 4 using connectGatt.

I might be able to use the BluetoothGattServer defined in Device 1, and call : gattServer.connect(BluetoothDevice device, boolean autoConnect) with device being Device 2.

But how will Device 2 be notified it's been connected to ?

And how will I get an instance of BluetoothGatt in Device 2 if I can't call connectGatt(Context, boolean, BluetoothGattCallback) on a BluetoothDevice?

Thank you in advance for your help !

Some documentation :

BluetoothGattServer

BluetoothDevice

like image 609
Thomas W Avatar asked May 24 '15 17:05

Thomas W


People also ask

What is a BLE Central?

Central - the BLE device which initiates an outgoing connection request to an advertising peripheral device. Peripheral - the BLE device which accepts an incoming connection request after advertising.

Can a phone be a BLE peripheral?

When using BLE, an Android device can act as a peripheral device, a central device, or both. Peripheral mode lets devices send advertisement packets.

How do Android apps communicate with custom BLE?

Create a new project Open Android Studio and you should be greeted with the following screen. Select Import an Android code sample. On the next screen select the sample Bluetooth Le Gatt under Connectivity. This project will set us up with a framework to build off of for our application.

What is MTU BLE Android?

The ATT Maximum Transmission Unit (MTU) is the maximum length of an ATT packet. Per the Bluetooth Core Specification, the maximum length of an attribute can be 512 bytes. In reality, it can be slightly larger than this to accommodate the ATT header. On Android, the maximum MTU can be 517 bytes.


1 Answers

1) Device 2 starts advertising (peripheral role).

Peripheral role will advertise, make sure to add CONNECTABLE

     AdvertiseSettings.Builder settingBuilder = new AdvertiseSettings.Builder();
     settingBuilder.setConnectable(true);

And start advertisement accordingly.

2) Device 1 starts scanning (central role), and gets the advertising device (BluetoothDevice object) through the ScanCallback's onScanResult method.

Perfect, now call connectGatt on this device(peripheral), make sure you stops the advertisement after you gets required device, otherwise you will end up sending multiple connect commands.

3) I now want the advertising device (Device 2) to be notified that it has been scanned and be able to get the BluetoothDevice associated with Device 1.

When you calls connectGatt from Central/client role, your peripheral will get a notification in its BluetoothGattServerCallback'onConnectionStateChange.

there you will know that connection has been made. though you have to register gatt Service with characteristics at peripheral side.

4) Device 1 has an instance of BluetoothGattServer. Device 2 would now call connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback) on Device 1 to get an instance of BluetoothGatt.

Wrong, Device 1 will initiate connection as I have stated in point 3. both device's onConnectionStateChange will be called to know that connection has been made.

5) In the end, Device 1 is Server and Device 2 is Client.

Wrong, Device 2 is peripheral(Server), Device 1 is Monitor(Client)

like image 171
AAnkit Avatar answered Sep 20 '22 15:09

AAnkit