Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with BLE Android 4.3 how to write characteristics?

Tags:

I am working on a BLE project (Android application) using Android 4.3 API, i have used sample BLE app it is only reading characteristics in DeviceControlActivity.activity, but i want to write characteristics and send it to BLE chip on clicking a button. How can I write data on chip cc2540 .. Basically i don't know the step by step procedure to write characteristics.

write i can only see the name and id of device with following piece of code in DeviceControlActivity

 private final ExpandableListView.OnChildClickListener servicesListClickListner =         new ExpandableListView.OnChildClickListener() {             @Override             public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,                                         int childPosition, long id) {                 if (mGattCharacteristics != null) {                     final BluetoothGattCharacteristic characteristic =                             mGattCharacteristics.get(groupPosition).get(childPosition);                     final int charaProp = characteristic.getProperties();                     if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {                         // If there is an active notification on a characteristic, clear                         // it first so it doesn't update the data field on the user interface.                         if (mNotifyCharacteristic != null) {                             mBluetoothLeService.setCharacteristicNotification(                                     mNotifyCharacteristic, false);                             mNotifyCharacteristic = null;                         }                         mBluetoothLeService.readCharacteristic(characteristic);                         showDialog("reading");                     }                      if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {                         // If there is an active notification on a characteristic, clear                         // it first so it doesn't update the data field on the user interface.                         if (mNotifyCharacteristic != null) {                             mBluetoothLeService.setCharacteristicNotification(                                     mNotifyCharacteristic, false);                             mNotifyCharacteristic = null;                         }                         mBluetoothLeService.writeCharacteristic(characteristic);                         showDialog("writing");                         //characteristic.setValue(bytes);                         //characteristic.setValue("testing");                         //characteristic.setWriteType(BluetoothGattCharacteristic.PERMISSION_WRITE);                     }                     if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {                         mNotifyCharacteristic = characteristic;                         mBluetoothLeService.setCharacteristicNotification(                                 characteristic, true);                     }                      byte[] value = {0x00, (byte) (0xB9) , 0x0D, (byte) (0x90), 0x2F};                     if(!characteristic.setValue(value))                      {                         Log.w(TAG, "Couldn't set characteristic's local value");                          //return;                     }                      characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);                     /*if(!writeCharacteristic.writeCharacteristic(characteristic))                     {                          Log.w(TAG, "Couldn't write characteristic");                     }*/                      return true;                 }                 return false;             } }; 
like image 997
Nomiluks Avatar asked Nov 18 '13 08:11

Nomiluks


People also ask

What is characteristic in BLE?

A characteristic object represents a characteristic of a Bluetooth® Low Energy peripheral device. If read or write are supported in the object Attributes property, you can read characteristic values using read or you can write characteristic values using write .

How many characteristics can a BLE service have?

A service can have one or more characteristics, and each service distinguishes itself from other services by means of a unique numeric ID called a UUID, which can be either 16-bit (for officially adopted BLE Services) or 128-bit (for custom services).


2 Answers

The following code is write characteristic using byte[] data:

    public boolean writeCharacteristic(){      //check mBluetoothGatt is available     if (mBluetoothGatt == null) {         Log.e(TAG, "lost connection");         return false;     }     BluetoothGattService Service = mBluetoothGatt.getService(your Services);     if (Service == null) {         Log.e(TAG, "service not found!");         return false;     }     BluetoothGattCharacteristic charac = Service             .getCharacteristic(your characteristic);     if (charac == null) {         Log.e(TAG, "char not found!");         return false;     }      byte[] value = new byte[1];     value[0] = (byte) (21 & 0xFF);     charac.setValue(value);     boolean status = mBluetoothGatt.writeCharacteristic(charac);     return status; } 
like image 166
Nam Pham Avatar answered Sep 20 '22 13:09

Nam Pham


Please note that the logic OR in:
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0)" in the original post should be a logic AND for the permission check to work. Same for the second charaProp comparison. Otherwise bot statements are true regardless of the actual permission flag.

like image 27
Miguel Avatar answered Sep 21 '22 13:09

Miguel