Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 4.3 BLE how to write Characteristic

Followed sample

I know:

  • How to read Characteristic value.

But I don't know:

  • How to write data to the firmware.

I tried several times, but it did not work.

This is the coding:

if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
                System.out.println("read!!!!!!");
                // 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);
            }
            if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                System.out.println("notify!!!!!!");
                mNotifyCharacteristic = characteristic;
                mBluetoothLeService.setCharacteristicNotification(
                        characteristic, true);
            }
            if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
                if (SampleGattAttributes.AppConfigToBongCharacteristicUUID
                        .equals(characteristic.getUuid())) {
                    System.out.println("write!!!!!!");
                    mBluetoothLeService.writeCharacteristic(characteristic);
                }
            }



public void writeCharacteristic(BluetoothGattCharacteristic characteristic) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    if (UUID_SEND_CONFIG_TO_BONG.equals(characteristic.getUuid())) {

        Calendar date = Calendar.getInstance();
        StringBuilder data = new StringBuilder();
        String data_date_y = String.format("%4s", date.get(Calendar.YEAR))
                .replace(' ', '0');
        String data_date_m = String.format("%2s", date.get(Calendar.MONTH))
                .replace(' ', '0');
        String data_date_d = String.format("%2s", date.get(Calendar.DATE))
                .replace(' ', '0');
        String data_date_h = String.format("%2s", date.get(Calendar.HOUR))
                .replace(' ', '0');
        String data_date_min = String.format("%2s",
                date.get(Calendar.MINUTE)).replace(' ', '0');

        data.append("10FFFF");
        data.append(data_date_y);
        data.append(data_date_m);
        data.append(data_date_d);
        data.append(data_date_h);
        data.append(data_date_min);
        System.out.println(data);
        byte[] dataByte = data.toString().getBytes();

        characteristic.setValue(dataByte);
        mBluetoothGatt.writeCharacteristic(characteristic);
    }

}

//onCharacteristicWrite() Not being called

@Override
    public void onCharacteristicWrite(BluetoothGatt gatt,
            BluetoothGattCharacteristic characteristic, int status) {
        System.out.println("writeCharacteristic111");
        System.out.println("status:" + status);
        System.out.println(BluetoothGatt.GATT_SUCCESS);
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }
    }
like image 615
steven Avatar asked Oct 13 '13 13:10

steven


People also ask

How do you write Bluetooth characteristics?

write( c , data ) writes the specified data to a characteristic of a Bluetooth® Low Energy peripheral device. The Attributes property of the input characteristic object c must be "Write" and/or "WriteWithoutResponse" . write( c , data , type ) specifies whether the device expects a response back using type .

What are ble settings?

In contrast to classic Bluetooth, BLE is designed for significantly lower power consumption. This allows apps to communicate with BLE devices that have stricter power requirements, such as proximity sensors, heart rate monitors, and fitness devices.

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.


2 Answers

I think jkraybill has got the answer for you.

I was using google's bluetooth le sample code, which checks properties bit with '|' operation as following:

if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
    ...
}

But when I looked into BluetoothGatt code itself, I found it's using '&' operation instead.

public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) {
    if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0
        && (characteristic.getProperties() &
            BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) return false;

    ...
}

So obviously the sample code was incorrect, if you check further on its definition on Google develop doc: https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic.html

like image 156
Jiawei Dai Avatar answered Oct 14 '22 23:10

Jiawei Dai


You must specify the value that you would like to write. you can find what you need in this example

like image 2
Fakher Avatar answered Oct 14 '22 22:10

Fakher