Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

writeCharacteristic and response #28

Open
sante85 opened this issue Dec 28, 2023 · 0 comments
Open

writeCharacteristic and response #28

sante85 opened this issue Dec 28, 2023 · 0 comments

Comments

@sante85
Copy link

sante85 commented Dec 28, 2023

@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);

        }

    }

``

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant