Working with BLE Android 4.3 how to write characteristics?

ε祈祈猫儿з 提交于 2019-11-28 20:30:43
Nam Pham

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;
}
Miguel

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.

The following code is write characteristic using string data in utf-8 format:

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

        Log.i(TAG, "characteristic " + characteristic.toString());
        try {
            Log.i(TAG, "data " + URLEncoder.encode(data, "utf-8"));

            characteristic.setValue(URLEncoder.encode(data, "utf-8"));

            // TODO
            mBluetoothGatt.writeCharacteristic(characteristic);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

Hope it helps!

image
public boolean writeCharacteristic(byte value[],int type){
    //check mBluetoothGatt is available
    if (mBluetoothGatt == null) {
        Log.e(TAG, "lost connection");
        return false;
    }
    BluetoothGattService Service = mBluetoothGatt.getService(UUID_SIMPLESERVICE);
    if (Service == null) {
        Log.e(TAG, "service not found!");
        //////////NO service found...........
         return false;
    }
    BluetoothGattCharacteristic charac1 = null;
    boolean status1 = false;

    if(type==1) {
        charac1 = Service.getCharacteristic(UUID_PORT1);
        charac1.setValue(value);
        status1 = mBluetoothGatt.writeCharacteristic(charac1);
        Log.v("________BLESERVICE____", "___WRITE CHARATERISTICS STATUS:_________"+status1);
        onReliableWriteCompleted(status1);
    }
    if (charac1 == null) {
        Log.e(TAG, "char not found!");
        return false;
    }

    Log.v("___TYPE___","______________________"+type);
    return status1;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!