I am building an Android authenticator for FIDO2. I got stuck with read/write characteristics. I am working on Mac - Chrome 75. Chrome can detect my Android BLE authenticator. After detecting my BLE Authenticator, onCharacteristicReadRequest() is called from authenticator side. Inside the onCharacteristicReadRequest() I am using the code written below but there is no response from client after that.
I have tried U2F with version 0b01000000. It's working fine. When I moved FIDO2 version 0b100000, I am facing this issue. I am advertising fido service and device info service from authenticator. Both services has been added with Thread.sleep(1000) interval. I couldn't add both services sequentially. When I add both services sequentially, I am getting ArrayIndexOutofBoundException.
I don't know if both issues are interlinked. Please correct me if I am doing anything wrong.
{
...
}else if (characteristic.getUuid().equals(FidoUUIDConstants.FIDO_SERVICE_REVISION_BITFIELD)) {
status = BluetoothGatt.GATT_SUCCESS;
ByteBuffer bb = ByteBuffer.allocate(1);
bb.order(ByteOrder.BIG_ENDIAN);
bb.put((byte) (1 << 5));
bytes = bb.array();
}
mGattServer.sendResponse(device, requestId, status, 0, bytes);
Client should read/write characteristics after fidoServiceBitFieldversion is expected.
I agreed with #Bao's concern. According to CTAP specification, you should define descriptor corresponding to each characteristic with READ/WRITE permission. Note that the UUIDs for each descriptor are needed a valid UUID 128-bit format. All descriptors have both permissions READ and WRITE. For example:
UUID CONTROL_POINT_DESCRIPTOR_UUID = UUID.fromString("00002901-0000-1000-8000-00805f9b34fb");
BluetoothGattDescriptor controlPointDescriptor = new BluetoothGattDescriptor(
CONTROL_POINT_DESCRIPTOR_UUID,
BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE
);
I couldn't add both services sequentially
I think you could add the device info service like the following:
gattServer = bleManager.openGattServer(this, new BluetoothGattServerCallback() {
#Override
public void onServiceAdded(int status, BluetoothGattService service) {
if (service.getUuid().equals(FidoUUIDConstants.FIDO2_GATT_SERVICE)) {
gattServer.addService(deviceInfoService);
}
}
});
gattServer.addService(fido2GattService)
For the characteristic fidoServiceRevisionBitfield, I just simply followed this statement a device that only supports FIDO2 Rev 1 will only have a fidoServiceRevisionBitfield characteristic of length 1 with value 0x20. at index 8.3.5.1. FIDO Service of CTAP document. Hence, my implementation was:
if(characteristic.getUuid().equals(FIDO2GattService.SERVICE_REVISION_BITFIELD)) {
status = BluetoothGatt.GATT_SUCCESS;
bytes = new byte[] {0x20}
}
gattServer.sendResponse(device, requestId, status, 0, bytes);
You should override all the methods of BluetoothGattServerCallback
I think you are missing onDescriptorReadRequest, onDescriptorWriteRequest implements.
#Override
public void onDescriptorReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattDescriptor descriptor) {
if (descriptor.getCharacteristic().getUuid().equals(FIDO2GattService.CONTROL_POINT_UUID) &&
descriptor.getUuid().equals(FIDO2GattService.CONTROL_POINT_DESCRIPTOR_UUID)) {
gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, new byte[] {0x31, 0x2e, 0x32});
} else {
gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, new byte[] {0x00, 0x00});
}
}
#Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
}
Related
I'm developing a project that consists of a BLE GATT server ran on Android phone (using BluetoothGattServer Java class) and a BLE client on an IoT board. The concept of using the phone as the server is to somehow protect the IoT board from attacking clients. When my Android application wants to talk to the external device it starts advertising a special set of data and if the external device recognizes the advertisment it connects to the Android BLE GATT server. Then the external device reads the presented services and chareacteristics and registers for notifications on some of the chars.
By far it all happens well.
After that the external device tries to write authentication data to one of the chars. If everything was clean and perfect, the process goes well. But if for a reason the last connection broke in the middle of some operation and was not properly closed, the external device cannot read/write the characteristic. If I restart the phone or clear Bluetooth cache (Settings > Apps > Bluetooth > Clear Data) all the operations proceed fine, but I cannot force users to do this in normal operation and I haven't found how and if I could clear this cache programmatically from inside the app.
I read over the web for GATT client cache undocumented "Refresh" method, but in GATT server there is no such.
I read about and tried the "Service changed" characteristic (0x2A05) but it doesn't help me much.
Having doubts about which of the devices is causing the problem I have tried with another phone as a client. I ran "BLE Scanner" App on it and tried to connect to the server phone - the problem persists. I can connect, all the characteristics are discovered, but when I try to read/write some char the connection brakes after a 30 sec timeout - exactly the same behavior like in the original situation. The conclusion is I have a problem with Android BluetoothGattServer.
During the development the problem occurs mostly when connection is broken by some error in communication. In real life usage after I have all errors fixed that will not happen, but having in mind tha it is wireless radio connection, it can be disconnected by literally everything and I shall have a reliable mechanism to reconnect.
I open the server with this code:
private void startServer() {
BluetoothAdapter bluetoothAdapter = mBluetoothManager.getAdapter();
mBluetoothLeAdvertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
if (mBluetoothLeAdvertiser == null) {
Log.w("BLE", "Failed to create advertiser");
return;
}
AdvertiseSettings settings = new AdvertiseSettings.Builder()
.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
.setConnectable(true)
.setTimeout(0)
.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
.build();
byte bData[] = new byte[24];
bData = ...... // some proprietary advertising data
AdvertiseData data = new AdvertiseData.Builder()
.setIncludeDeviceName(false)
.setIncludeTxPowerLevel(false)
.addManufacturerData(iManufID, bData)
.build();
mBluetoothLeAdvertiser
.startAdvertising(settings, data, mAdvertiseCallback);
mBluetoothGattServer = mBluetoothManager.openGattServer(ctxActivity, mGattServerCallback);
mBluetoothGattServer.clearServices();
mBluetoothGattServer.addService(BLEProfile.createBLEService());
/* Static method, which builds Service->Chars->Descriptors.
I assign the Client Config descriptor (0x2902) to each characteristic. */
}
For stopping the sertver I use this code:
private void stopServer() {
if (mBluetoothGattServer == null) return;
mBluetoothGattServer.clearServices();
mBluetoothGattServer.close();
if (mBluetoothLeAdvertiser == null) return;
mBluetoothLeAdvertiser.stopAdvertising(mAdvertiseCallback);
}
I stop and start the server each time a connection was broken.
Does anyone have an idea what am I doing wrong?
Also it is important to mention that in real life IoT devices will be many in a room, phones may be many in a room. One phone should be able to be connected by any of the IoT devices it requests sequentially and one IoT device should be able to connect to more than one phone sequentially. The advertising data of the phone's GATT Server defines which of the IoT devices is requested to connect and it will change each time the phone requests connection with a different device.
Update:
Here is the code for the Server Callback:
private BluetoothGattServerCallback mGattServerCallback = new BluetoothGattServerCallback() {
#Override
public void onMtuChanged(BluetoothDevice device, int mtu) {
super.onMtuChanged(device, mtu);
Log.i("BLE", "MTU changed: "+mtu);
}
#Override
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
if(BLEProfile.XXXXXX.equals(characteristic.getUuid()))
{
// ... some data checks ...
mBluetoothGattServer.sendResponse(device,
requestId,
iValid,
0,
bEncrypted);
characteristic.setValue(bEncrypted);
mBluetoothGattServer.notifyCharacteristicChanged(device, characteristic, true);
} else if (...) /* similar operations for all other characteristics */
{
...
}
else
{
Log.i("BLE", "Write not mine characteristic");
mBluetoothGattServer.sendResponse(device,
requestId,
BluetoothGatt.GATT_FAILURE,
0,
null);
}
}
#Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.i("BLE", "BluetoothDevice ... CONNECTED: " + device);
if(device != null)
{
BluetoothGattService mServ = mBluetoothGattServer.getService(BLEProfile.GATT_SERVICE);
if(mServ != null)
{
BluetoothGattCharacteristic mChar = mServ.getCharacteristic(BLEProfile.SERVICE_CHANGED);
if(mChar != null)
mBluetoothGattServer.notifyCharacteristicChanged(device, mChar, false);
}
}
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
mRegisteredDevices.remove(device);
stopAdvertising();
startAdvertising();
}
}
#Override
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset,
BluetoothGattCharacteristic characteristic) {
/* Not used currently. Just some testing code below. */
if(BLEProfile.XXXXXX.equals(characteristic.getUuid()))
{
mBluetoothGattServer.sendResponse(device,
requestId,
BluetoothGatt.GATT_SUCCESS,
0,
new byte[] {0x01, 0x02, 0x03, 0x04, 0x05});
} else if(...) /* similar operations for all other characteristics */
{
...
}
else
{
Log.i("BLE", "Read not mine characteristic");
mBluetoothGattServer.sendResponse(device,
requestId,
BluetoothGatt.GATT_FAILURE,
0,
null);
}
}
#Override
public void onDescriptorReadRequest(BluetoothDevice device, int requestId, int offset,
BluetoothGattDescriptor descriptor) {
if (BLEProfile.CLIENT_CONFIG.equals(descriptor.getUuid())) {
Log.d("BLE", "Config descriptor read");
byte[] returnValue;
if (mRegisteredDevices.contains(device)) {
returnValue = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
} else {
returnValue = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
}
/* Not sure why I am responding with GATT_FAILURE here insted of GAT_SUCCESS !?!? May be some copy/paste mistake. */
mBluetoothGattServer.sendResponse(device,
requestId,
BluetoothGatt.GATT_FAILURE,
0,
returnValue);
} else {
Log.w("BLE", "Unknown descriptor read request");
mBluetoothGattServer.sendResponse(device,
requestId,
BluetoothGatt.GATT_FAILURE,
0,
null);
}
}
#Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId,
BluetoothGattDescriptor descriptor,
boolean preparedWrite, boolean responseNeeded,
int offset, byte[] value) {
if (BLEProfile.CLIENT_CONFIG.equals(descriptor.getUuid())) {
if (Arrays.equals(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE, value)) {
Log.i("BLE", "Subscribe device to notifications: " + device);
mRegisteredDevices.add(device);
} else if (Arrays.equals(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE, value)) {
Log.i("BLE", "Unsubscribe device from notifications: " + device);
mRegisteredDevices.remove(device);
}
if (responseNeeded) {
mBluetoothGattServer.sendResponse(device,
requestId,
BluetoothGatt.GATT_SUCCESS,
0,
null);
}
} else {
Log.w("BLE", "Unknown descriptor write request");
if (responseNeeded) {
mBluetoothGattServer.sendResponse(device,
requestId,
BluetoothGatt.GATT_FAILURE,
0,
null);
}
}
}
};
I am trying to interface with an Red Bear Labs nRF8001 Arduino Shield via Android Studio. I am succesfully able to send a command via Android and recieve it on my Arduino.
However I wish to read the response from my Arduino, using "OnCharacteristicChange" - however research showed a descriptor is required.
This is my code sample:
#Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
if(status != BluetoothGatt.GATT_SUCCESS){
Log.i("BtServiceCallback","OnServicesDiscovered Failed!");
}
BluetoothGattService service = gatt.getService(RBLService.UUID_BLE_SHIELD_SERVICE);
BluetoothGattCharacteristic characteristic = service.getCharacteristic(RBLService.UUID_BLE_SHIELD_TX);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(RBLGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
byte[] val = readSetPoint.getBytes();
characteristic.setValue(val);
gatt.setCharacteristicNotification(characteristic,true);
gatt.writeCharacteristic(characteristic);
Log.i("Sent = ", characteristic.getStringValue(0));
}
Whenever I try to create a descriptor from the characteristic, I get the following error:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothGattDescriptor.setValue(byte[])' on a null object reference
at uk.ac.as988brighton.bluecontroller.MainActivity$1.onServicesDiscovered(MainActivity.java:160)
at android.bluetooth.BluetoothGatt$1.onSearchComplete(BluetoothGatt.java:286)
at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:103)
at android.os.Binder.execTransact(Binder.java:573)
I am using the UUIDs from RBL's Github:
public static String CLIENT_CHARACTERISTIC_CONFIG = "00002902-0000-1000-8000-00805f9b34fb";
public static String BLE_SHIELD_TX = "713d0003-503e-4c75-ba94-3148f18d941e";
public static String BLE_SHIELD_RX = "713d0002-503e-4c75-ba94-3148f18d941e";
public static String BLE_SHIELD_SERVICE = "713d0000-503e-4c75-ba94-3148f18d941e";
I am unsure as to what is causing the null descriptor.
Edit
I have used different combinations for the descriptor - such as using
BluetoothGattDescriptor characteristic.getDescriptor(RBLService.UUID_BLE_SHIELD_TX);
But still receiving the same error.
It looks like you need to make sure you're naming things the same. Both your service and characteristic uuid are declared as BLE_SHIELD_SERVICE and BLE_SHIELD_TX but you're trying to access them with UUID_BLE_SHIELD_SERVICE and UUID_BLE_SHIELD_TX.
So change
BluetoothGattService service = gatt.getService(RBLService.UUID_BLE_SHIELD_SERVICE); to
BluetoothGattService service = gatt.getService(RBLService.BLE_SHIELD_SERVICE);
and
BluetoothGattCharacteristic characteristic = service.getCharacteristic(RBLService.UUID_BLE_SHIELD_TX); to
BluetoothGattCharacteristic characteristic = service.getCharacteristic(RBLService.BLE_SHIELD_TX);
and I believe it should work. Also make sure you are broadcasting those exact service and characteristic uuid's from your peripheral.
Good luck!
If we call setCharacteristicNotification on a character, not giving Remote Notification on value Change? How to enable the remote Notification on a Central Device in Bluetooth LE ?
TO enable Remote Notification on Android,
setCharacteristicNotification(characteristic, enable) is not enough.
Need to write the descriptor for the characteristic. Peripheral has to enable characteristic notification while creating the characteristic.
Once the Notify is enabled , it will have a descriptor with handle 0x2902 . so we need to write BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE to the descriptor. First Convert 0x2902 to 128 bit UUID, it will be like this 00002902-0000-1000-8000-00805f9b34fb (Base Bluetooth UUID is 0000xxxx-0000-1000-8000-00805f9b34fb).
Code below
protected static final UUID CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
* Enable Notification for characteristic
*
* #param bluetoothGatt
* #param characteristic
* #param enable
* #return
*/
public boolean setCharacteristicNotification(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic characteristic,boolean enable) {
Logger.d("setCharacteristicNotification");
bluetoothGatt.setCharacteristicNotification(characteristic, enable);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID);
descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[]{0x00, 0x00});
return bluetoothGatt.writeDescriptor(descriptor); //descriptor write operation successfully started?
}
i also receive null value when call descrpitor.setValue, so i just turn it on when discovering service and finally it works very well:
to notify master device that some characteristic is change, call this function on your pheripheral:
private BluetoothGattServer server;
//init....
//on BluetoothGattServerCallback...
//call this after change the characteristic
server.notifyCharacteristicChanged(device, characteristic, false);
in your master device: enable setCharacteristicNotification after discover the service:
#Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
services = mGatt.getServices();
for(BluetoothGattService service : services){
if( service.getUuid().equals(SERVICE_UUID)) {
characteristicData = service.getCharacteristic(CHAR_UUID);
for (BluetoothGattDescriptor descriptor : characteristicData.getDescriptors()) {
descriptor.setValue( BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
mGatt.writeDescriptor(descriptor);
}
gatt.setCharacteristicNotification(characteristicData, true);
}
}
if (dialog.isShowing()){
mHandler.post(new Runnable() {
#Override
public void run() {
dialog.hide();
}
});
}
}
now you can check your characteristic value is change, for example onCharacteristicRead function :
#Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
Log.i("onCharacteristicRead", characteristic.toString());
byte[] value=characteristic.getValue();
String v = new String(value);
Log.i("onCharacteristicRead", "Value: " + v);
}
To enable notification you should do as following.
mBluetoothLeService.setCharacteristicNotification(mSampleCharacteristic, true);
of which definition is as following.
if (enabled) {
BluetoothGattDescriptor bluetoothGattDescriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
bluetoothGattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
mBluetoothGatt.writeDescriptor(bluetoothGattDescriptor);
} else {
BluetoothGattDescriptor bluetoothGattDescriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
bluetoothGattDescriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(bluetoothGattDescriptor);
}
Still if remote notification does not work, try to read characteristics only after enabling notifications for the same.
Reading characteristics is as
mBluetoothLeService.readCharacteristic(eachChara);
has anyone tried using HM-10 Bluetooth module?
I'm able to pair with it using an Android device and passing the pre-defined PIN. Based on the UART return, the pairing is successful (module returns OK+CONN - means a connection was established)
However, after a few seconds (2-3), the UART receives OK+LOST; means the connection was lost. Also, the LED starts blinking (normally, when a connection is active, it stays lit)
Is this normal behaviour for bluetooth in general or the HM-10 module.
This is the product's website: http://www.jnhuamao.cn/bluetooth.asp?ID=1
I'm not sure, but HM -10 don't support rfcom. It's mean that you must use GATT functionality for communication. Entity of BLE is usage of minimum data package as it possible, so BLE don't hold the connection all times and use something like statuses [attributes].
So, few code lines for example, how work with BLE:
1.
BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(DEVICE_ADDR);
That's device initiation, the same like with simple bluetooth, where DEVICE_ADDR is the MAC of your BLE(how to find this address you can find in google or stack overflow, its trivial)
2.
BluetoothGattService mBluetoothGattService;
BluetoothGatt mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
#Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
mBluetoothGatt.discoverServices();
}
}
#Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
List<BluetoothGattService> gattServices = mBluetoothGatt.getServices();
for(BluetoothGattService gattService : gattServices) {
if("0000ffe0-0000-1000-8000-00805f9b34fb".equals(gattService.getUuid().toString()))
{
mBluetoothGattService = gattService;
}
}
} else {
Log.d(TAG, "onServicesDiscovered received: " + status);
}
}
};
So, what this code mean: if u can see from this part of code, i describe how GATT service find. This service needed for "attribute" communication. gattService.getUuid() has few uuids for communication(4 in my module), some of them used for RX, some for TX etc. "0000ffe0-0000-1000-8000-00805f9b34fb" that is one of uuid that use for communication thats why i check it.
The final part of code is message sending:
BluetoothGattCharacteristic gattCharacteristic = mBluetoothGattService.getCharacteristic(UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb"));
String msg = "HELLO BLE =)";
byte b = 0x00;
byte[] temp = msg.getBytes();
byte[] tx = new byte[temp.length + 1];
tx[0] = b;
for(int i = 0; i < temp.length; i++)
tx[i+1] = temp[i];
gattCharacteristic.setValue(tx);
mBluetoothGatt.writeCharacteristic(gattCharacteristic);
After sending message contain hold on and you can send another message or can close connection.
More info, you can find on https://developer.android.com/guide/topics/connectivity/bluetooth-le.html.
PS: MAC address of your module can find with ble scanner code or with AT cmd:
on my firmware AT+ADDR or AT+LADDR
About UUIDs usage: not sure, but in my case, i find it with next AT+UUID [Get/Set system SERVER_UUID] -> Response +UUID=0xFFE0, AT+CHAR [Get/Set system CHAR_UUID] - Response +CHAR=0xFFE1. Thats why i make conclusion that UUID which i must use fe "0000[ffe0/is 0xFFE0 from AT response]-0000-1000-8000-00805f9b34fb"
I am able to discover, connect to bluetooth.
Source Code---
Connect via bluetooth to Remote Device:
//Get the device by its serial number
bdDevice = mBluetoothAdapter.getRemoteDevice(blackBox);
//for ble connection
bdDevice.connectGatt(getApplicationContext(), true, mGattCallback);
Gatt CallBack for Status:
private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
#Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
//Connection established
if (status == BluetoothGatt.GATT_SUCCESS
&& newState == BluetoothProfile.STATE_CONNECTED) {
//Discover services
gatt.discoverServices();
} else if (status == BluetoothGatt.GATT_SUCCESS
&& newState == BluetoothProfile.STATE_DISCONNECTED) {
//Handle a disconnect event
}
}
#Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
//Now we can start reading/writing characteristics
}
};
Now I want to send commands to Remote BLE device but don't know how to do that.
Once the command is sent to the BLE device, the BLE device will respond by broadcasting
data which my application can receive.
You need to break this process into a few steps, when you connect to a BLE device and discover Services:
Display available gattServices in onServicesDiscovered for your callback
To check whether you can write a characteristic or not
check for BluetoothGattCharacteristic PROPERTIES -I didn't realize that need to enable the PROPERTY_WRITE on the BLE hardware and that wasted a lot of time.
When you write a characteristic, does the hardware perform any action to explicitly indicate the operation (in my case i was lighting an led)
Suppose mWriteCharacteristic is a BluetoothGattCharacteristic
The part where to check the PROPERTY should be like:
if (((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) |
(charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) > 0) {
// writing characteristic functions
mWriteCharacteristic = characteristic;
}
And, to write your characteristic:
// "str" is the string or character you want to write
byte[] strBytes = str.getBytes();
byte[] bytes = activity.mWriteCharacteristic.getValue();
YourActivity.this.mWriteCharacteristic.setValue(bytes);
YourActivity.this.writeCharacteristic(YourActivity.this.mWriteCharacteristic);
Those are the useful parts of the code that you need to implement precisely.
Refer this github project for an implementation with just a basic demo.
A noob-friendly guide to make Android interact with a LED-lamp.
Step 1.
Get an tool to scan your BLE device. I used "Bluetooth LE Lab" for Win10, but this one will do it as well: https://play.google.com/store/apps/details?id=com.macdom.ble.blescanner
Step 2.
Analyse the behavior of the BLE device by entering data, I recommend to enter hex values.
Step 3.
Get the sample of the Android docs. https://github.com/googlesamples/android-BluetoothLeGatt
Step 4.
Modify the UUIDs you find in SampleGattAttributes
My config:
public static String CUSTOM_SERVICE = "0000ffe5-0000-1000-8000-00805f9b34fb";
public static String CLIENT_CHARACTERISTIC_CONFIG = "0000ffe9-0000-1000-8000-00805f9b34fb";
private static HashMap<String, String> attributes = new HashMap();
static {
attributes.put(CUSTOM_SERVICE, CLIENT_CHARACTERISTIC_CONFIG);
attributes.put(CLIENT_CHARACTERISTIC_CONFIG, "LED");
}
Step 5.
In BluetoothService.java modify onServicesDiscovered:
#Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
for (BluetoothGattService gattService : gatt.getServices()) {
Log.i(TAG, "onServicesDiscovered: ---------------------");
Log.i(TAG, "onServicesDiscovered: service=" + gattService.getUuid());
for (BluetoothGattCharacteristic characteristic : gattService.getCharacteristics()) {
Log.i(TAG, "onServicesDiscovered: characteristic=" + characteristic.getUuid());
if (characteristic.getUuid().toString().equals("0000ffe9-0000-1000-8000-00805f9b34fb")) {
Log.w(TAG, "onServicesDiscovered: found LED");
String originalString = "560D0F0600F0AA";
byte[] b = hexStringToByteArray(originalString);
characteristic.setValue(b); // call this BEFORE(!) you 'write' any stuff to the server
mBluetoothGatt.writeCharacteristic(characteristic);
Log.i(TAG, "onServicesDiscovered: , write bytes?! " + Utils.byteToHexStr(b));
}
}
}
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
Convert the byte-String using this function:
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
PS: The above code is far away from production, but I hope it helps those, who are new to BLE.