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

feat: Add methods for all BLE interactions #10

Merged
merged 4 commits into from
Mar 30, 2023

Conversation

richardhopton
Copy link

@richardhopton richardhopton commented Mar 29, 2023

This PR contains connection logic to call all relevant BLE interactions on the ESP native API.

BLE Advertisements:-

const listener = (advertisement) => { /* some logic */};
connection
  .on('message.BluetoothLEAdvertisementResponse', listener)
  .subscribeBluetoothAdvertisementService();

Note: Unsubscribing has been added to ESPHome & AIOESPHomeApi but it hasn't been released yet so will be hard to test. To unsubscribe you'd want to do this:-

connection
  .off('message.BluetoothLEAdvertisementResponse', listener)
  .unsubscribeBluetoothAdvertisementService();

In the examples I've included below, you will need an address to pass in. The address is the decimal encoded MAC address of the device but is also returned in the BluetoothLEAdvertisementResponse.

Connect to connectable BLE device:-

await connection.connectBluetoothDeviceService(address);

Disconnect from connectable BLE device:-

await connection.disconnectBluetoothDeviceService(address);

For all the following examples you MUST be connected or they will fail.

BLE list GATT services:-

const result = await connection.listBluetoothGATTServicesService(address);

In the following examples, you will need a handle that can be retrieved from the GATT services list.

BLE read GATT Characteristic:-

const result = await connection.listBluetoothGATTServicesService(address, handle);

BLE notify GATT Characteristic:-

connection
  .on('message.BluetoothGATTNotifyDataResponse', (message) => { /* do something with the message.data */ })
  .notifyBluetoothGATTCharacteristicService(address, handle);

BLE write GATT Characteristic:-

// the 4th argument on this function is whether to wait for a response. I'd recommend using true because
// If you don't wait then the message might sit in the queue until it's ready to drain (like on disconnect from esphome)
await connection.writeBluetoothGATTCharacteristicService(address, handle, someUint8Array, true);

BLE write GATT Descriptor:-

await connection.writeBluetoothGATTDescriptorService(address, handle, someUint8Array);

@@ -1218,19 +1218,19 @@ message BluetoothGATTGetServicesRequest {
}

message BluetoothGATTDescriptor {
repeated uint64 uuid = 1;
repeated uint64 uuid = 1 [jstype=JS_STRING];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is needed because esp sends 64-bit integers, but JS can only handle 53-bit precision. protocolbuffers/protobuf#3666 (comment)

lib/connection.js Outdated Show resolved Hide resolved
lib/connection.js Outdated Show resolved Hide resolved
ESPHome will always return a response even if the `response` value is set to false. It only appears to control how the characteristic is written.
@twocolors twocolors merged commit 5cb5ab7 into twocolors:main Mar 30, 2023
@twocolors
Copy link
Owner

Thanks for the help and code.

@twocolors twocolors mentioned this pull request Mar 30, 2023
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

Successfully merging this pull request may close these issues.

None yet

2 participants