我正在创建一个应用程序,它可以将字符串值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 + " ");