Working with BLE Android 4.3 how to write characteristics?

后端 未结 4 2141
清歌不尽
清歌不尽 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:40

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

提交回复
热议问题