Skip to content

Commit

Permalink
fix(dbus): Implement notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Crespi committed Apr 9, 2021
1 parent bda7d30 commit 3f32190
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 13 deletions.
5 changes: 4 additions & 1 deletion lib/bindings/dbus/gatt/Characteristic.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ export declare class DbusGattCharacteristic extends GattCharacteristic {
readonly service: DbusGattService;
readonly path: string;
private iface;
private propsIface;
private get dbus();
constructor(service: DbusGattService, uuid: string, isRemote: boolean, properties: GattCharacteristicProperty[], secure: GattCharacteristicProperty[], path: string);
discoverDescriptors(): Promise<GattDescriptor[]>;
read(): Promise<Buffer>;
write(data: Buffer, withoutResponse: boolean): Promise<void>;
broadcast(): Promise<void>;
notify(): Promise<void>;
notify(notify: boolean): Promise<void>;
private onPropsChanged;
private getInterface;
private getPropsInterface;
addDescriptor(): Promise<GattDescriptor>;
}
//# sourceMappingURL=Characteristic.d.ts.map
2 changes: 1 addition & 1 deletion lib/bindings/dbus/gatt/Characteristic.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 28 additions & 2 deletions lib/bindings/dbus/gatt/Characteristic.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/bindings/dbus/gatt/Characteristic.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/bindings/web/Adapter.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion lib/bindings/web/Adapter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/bindings/web/Adapter.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 32 additions & 3 deletions src/bindings/dbus/gatt/Characteristic.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ClientInterface } from 'dbus-next';

import { GattCharacteristic, GattCharacteristicProperty, GattDescriptor } from '../../../models';
import { buildTypedValue, I_BLUEZ_CHARACTERISTIC } from '../misc';
import { buildTypedValue, DbusObject, I_BLUEZ_CHARACTERISTIC, I_PROPERTIES } from '../misc';

import { DbusGattService } from './Service';

Expand All @@ -11,6 +11,7 @@ export class DbusGattCharacteristic extends GattCharacteristic {
public readonly path: string;

private iface: ClientInterface;
private propsIface: ClientInterface;

private get dbus() {
return this.service.gatt.peripheral.adapter.modblue.dbus;
Expand Down Expand Up @@ -54,10 +55,29 @@ export class DbusGattCharacteristic extends GattCharacteristic {
throw new Error('Method not implemented.');
}

public async notify(): Promise<void> {
throw new Error('Method not implemented.');
public async notify(notify: boolean): Promise<void> {
const iface = await this.getInterface();
const propsIface = await this.getPropsInterface();

if (notify) {
propsIface.on('PropertiesChanged', this.onPropsChanged);
await iface.StartNotify();
} else {
propsIface.off('PropertiesChanged', this.onPropsChanged);
await iface.StopNotify();
}
}

private onPropsChanged = (iface: string, changedProps: DbusObject) => {
if (iface !== I_BLUEZ_CHARACTERISTIC) {
return;
}

if ('Value' in changedProps) {
this.emit('notification', changedProps.Value.value as Buffer);
}
};

private async getInterface() {
if (!this.iface) {
const obj = await this.dbus.getProxyObject('org.bluez', this.path);
Expand All @@ -67,6 +87,15 @@ export class DbusGattCharacteristic extends GattCharacteristic {
return this.iface;
}

private async getPropsInterface() {
if (!this.propsIface) {
const obj = await this.dbus.getProxyObject('org.bluez', this.path);
this.propsIface = obj.getInterface(I_PROPERTIES);
}

return this.propsIface;
}

public async addDescriptor(): Promise<GattDescriptor> {
throw new Error('Method not implemented.');
}
Expand Down
4 changes: 2 additions & 2 deletions src/bindings/web/Adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export class WebAdapter extends Adapter {
// NO-OP
}

public isScanning(): Promise<boolean> {
throw new Error('Method not implemented.');
public async isScanning(): Promise<boolean> {
return false;
}

public async startScanning(): Promise<void> {
Expand Down

0 comments on commit 3f32190

Please sign in to comment.