Сервер BLE работает на Linux-плате с модулем BT, сервер разработан с использованием Python-dbus, а часть характеристик показана ниже. >
Код: Выделить всё
class DapUsrInfoCharacteristic(bluetooth_gatt.Characteristic):
bonded = False
def __init__(self, bus, index, service):
bluetooth_gatt.Characteristic.__init__(
self, bus, index,
bluetooth_constants.DAP_CHR_UUID,
['read', 'write', 'notify'],
service)
# ['encrypt-authenticated-read', 'encrypt-authenticated-write',
# 'notify'],
# service)
def ReadValue(self, options):
# Replace this with the actual value to be read
show_log(LOG_LEVEL.INFO, "Reading value from client")
return dbus.ByteArray([0x01])
def WriteValue(self, value, options):
# self.do_notify(value)
self.callback(CallbackType.BLE_RECVD_MSG, value)
# self.parse_msg_from_client(value)
def StartNotify(self):
show_log(LOG_LEVEL.INFO, "starting notifications")
self.notifying = True
def StopNotify(self):
show_log(LOG_LEVEL.INFO, "stopping notifications")
self.notifying = False
def register_callback(self, callback):
self.callback = callback
def do_notify(self, value):
self.StartNotify()
if not self.notifying:
show_log(LOG_LEVEL.INFO, "Not notifying")
return
show_log(LOG_LEVEL.INFO, f"Sending message to client: {value}")
self.PropertiesChanged(bluetooth_constants.GATT_CHARACTERISTIC_INTERFACE, {'Value': value}, [])
show_log(LOG_LEVEL.INFO, f"Message sent to client: {value}")
def parse_msg_from_client(self, value):
msg_type = int(value[0])
if msg_type == BLE_MSG_TYPE.BONDED.value:
self.bonded = bool(value[1])
show_log(LOG_LEVEL.INFO, f"Bond status: {self.bonded}")
self.callback(CallbackType.BLE_BONDED, None)
else:
show_log(LOG_LEVEL.INFO, f"Unknown message type: {value}")
Код: Выделить всё
/* Gatt call back */
private BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
...
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
Log.d(TAG, "Descriptor write status: " + status);
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.d(TAG, "Descriptor written");
mainHandler.post(() -> eventSink.success("Descriptor written"));
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Log.d(TAG, "------------------");
byte[] value = characteristic.getValue();
Log.d(TAG, "Characteristic changed: " + Arrays.toString(value));
mainHandler.post(() -> eventSink.success("Characteristic changed: " + Arrays.toString(value) + "\n"));
}
};
/*Enable characteristic notification*/
boolean isEnableNotification = bluetoothGatt.setCharacteristicNotification(targetCharacteristic, true);
if (!isEnableNotification) {
Log.e(TAG, "Failed to enable notification");
return;
}
Log.d(TAG, "Characteristic notification enabled");
List descriptors = targetCharacteristic.getDescriptors();
Log.d(TAG,"Total descriptors: " + descriptors.size());
for (BluetoothGattDescriptor descriptor : descriptors) {
Log.d(TAG, "Descriptor: " + descriptor.getUuid().toString());
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
boolean isWriteDesp = bluetoothGatt.writeDescriptor(descriptor);
Log.d(TAG, "Descriptor write status: " + isWriteDesp);
}
Log.d(TAG, "Finished writing descriptors");
Можете ли вы дать какой-нибудь совет? Спасибо большое!
Подробнее здесь: https://stackoverflow.com/questions/790 ... ticchanged