weliem / blessed-bluez

BLE library using Java and Bluez
MIT License
76 stars 23 forks source link

writeCharacteristic and response #28

Open sante85 opened 9 months ago

sante85 commented 9 months ago

@weliem I want to congratulate you on this magnificent library. could you help me convert this C code to java. I tried, but there's something missing in the writeCharateristic. The write does not reply to me with the data. Where am I doing wrong?

this is a C code. https://github.com/syssi/esphome-tianpower-bms/blob/main/components/tianpower_bms_ble/tianpower_bms_ble.cpp

this is a java translate with your lib `` private static final UUID DIS_SERVICE_UUID = UUID.fromString("0000ff00-0000-1000-8000-00805f9b34fb"); private static final UUID NOTIFY_CHARACTERISTIC_UUID = UUID.fromString("0000ff01-0000-1000-8000-00805f9b34fb"); private static final UUID CTRL_CHARACTERISTIC_UUID = UUID.fromString("0000ff02-0000-1000-8000-00805f9b34fb");

private final BluetoothPeripheralCallback peripheralCallback = new BluetoothPeripheralCallback() {
    @Override
    public void onServicesDiscovered(@NotNull final BluetoothPeripheral peripheral, @NotNull final List<BluetoothGattService> services) {        
        BluetoothGattCharacteristic ctrlChar = peripheral.getCharacteristic(DIS_SERVICE_UUID, CTRL_CHARACTERISTIC_UUID);
        if(ctrlChar != null) {
            if (ctrlChar.supportsWritingWithResponse()) {
                BluetoothBytesParser parser = new BluetoothBytesParser();
                parser.setIntValue(0x55, BluetoothBytesParser.FORMAT_UINT8);
                parser.setIntValue(0x04, BluetoothBytesParser.FORMAT_UINT8);
                parser.setIntValue(0x88, BluetoothBytesParser.FORMAT_UINT8);
                parser.setIntValue(0xAA, BluetoothBytesParser.FORMAT_UINT8);
                peripheral.writeCharacteristic(ctrlChar, parser.getValue(), WriteType.WITH_RESPONSE);
            }
        }

        BluetoothGattCharacteristic notifyChar = peripheral.getCharacteristic(DIS_SERVICE_UUID, NOTIFY_CHARACTERISTIC_UUID);
        if (notifyChar != null) {
            peripheral.setNotify(notifyChar, true);

        }

    }

``