发送代码至BLE HM10-如何?

时间:2019-04-09 14:29:04

标签: java android

我正在创建一个应用程序,它可以将字符串值21发送到BLE HM10

服务声明:

BluetoothGattService service = bluetoothGatt.getService(SERVICE_UUID);

BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(
                CHARACTERISTIC_UUID,
                BluetoothGattCharacteristic.PROPERTY_WRITE,
                BluetoothGattCharacteristic.PERMISSION_WRITE);

service.addCharacteristic(characteristic);
bluetoothGattServer.addService(service);

服务使用情况:

String message = "21";
byte[] messageBytes = new byte[0];
try {
    messageBytes = message.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
    Log.e(TAG, "Failed to convert message string to byte array");
    characteristic.setValue(messageBytes);
    boolean success = bluetoothGatt.writeCharacteristic(characteristic);
    Log.d(TAG, "Success message: " + success);
}

characteristic.setValue(messageBytes);
boolean success = bluetoothGatt.writeCharacteristic(characteristic);
Log.d("Send Success Message: ", success + " ");

0 个答案:

没有答案