Working with BLE Android 4.3 how to write characteristics?

后端 未结 4 2138
清歌不尽
清歌不尽 2020-12-14 09:16

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, b

4条回答
  •  温柔的废话
    2020-12-14 09:33

    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!

提交回复
热议问题