mirror of
https://github.com/thegecko/web-bluetooth-dfu.git
synced 2026-03-15 21:23:17 +08:00
Add alternative optional service, control, packet & button uuids
This commit is contained in:
@@ -212,7 +212,8 @@ export class SecureDfu extends EventDispatcher {
|
||||
});
|
||||
}
|
||||
|
||||
private gattConnect(device: BluetoothDevice): Promise<Array<BluetoothRemoteGATTCharacteristic>> {
|
||||
private gattConnect(device: BluetoothDevice, serviceUUID?: number|string): Promise<Array<BluetoothRemoteGATTCharacteristic>> {
|
||||
serviceUUID = serviceUUID || SecureDfu.SERVICE_UUID;
|
||||
return Promise.resolve()
|
||||
.then(() => {
|
||||
if (device.gatt.connected) return device.gatt;
|
||||
@@ -220,7 +221,7 @@ export class SecureDfu extends EventDispatcher {
|
||||
})
|
||||
.then(server => {
|
||||
this.log("connected to gatt server");
|
||||
return server.getPrimaryService(SecureDfu.SERVICE_UUID)
|
||||
return server.getPrimaryService(serviceUUID)
|
||||
.catch(() => {
|
||||
throw new Error("Unable to find DFU service");
|
||||
});
|
||||
@@ -393,15 +394,27 @@ export class SecureDfu extends EventDispatcher {
|
||||
* Scans for a device to update
|
||||
* @param buttonLess Scans for all devices and will automatically call `setDfuMode`
|
||||
* @param filters Alternative filters to use when scanning
|
||||
* @param uuids Optional alternative uuids for service, control, packet or button
|
||||
* @returns Promise containing the device
|
||||
*/
|
||||
public requestDevice(buttonLess: boolean, filters: Array<BluetoothLEScanFilterInit>): Promise<BluetoothDevice> {
|
||||
public requestDevice(
|
||||
buttonLess: boolean,
|
||||
filters: Array<BluetoothLEScanFilterInit>,
|
||||
uuids?: {
|
||||
service?: number|string,
|
||||
button?: number|string,
|
||||
control?: number|string,
|
||||
packet?: number|string
|
||||
}
|
||||
): Promise<BluetoothDevice> {
|
||||
uuids = uuids || {};
|
||||
const serviceId = uuids.service || SecureDfu.SERVICE_UUID;
|
||||
if (!buttonLess && !filters) {
|
||||
filters = [ { services: [ SecureDfu.SERVICE_UUID ] } ];
|
||||
filters = [ { services: [ serviceId ] } ];
|
||||
}
|
||||
|
||||
const options: any = {
|
||||
optionalServices: [ SecureDfu.SERVICE_UUID ]
|
||||
optionalServices: [ serviceId ]
|
||||
};
|
||||
|
||||
if (filters) options.filters = filters;
|
||||
@@ -410,7 +423,7 @@ export class SecureDfu extends EventDispatcher {
|
||||
return this.bluetooth.requestDevice(options)
|
||||
.then(device => {
|
||||
if (buttonLess) {
|
||||
return this.setDfuMode(device);
|
||||
return this.setDfuMode(device, uuids);
|
||||
}
|
||||
return device;
|
||||
});
|
||||
@@ -419,19 +432,25 @@ export class SecureDfu extends EventDispatcher {
|
||||
/**
|
||||
* Sets the DFU mode of a device, preparing it for update
|
||||
* @param device The device to switch mode
|
||||
* @param uuids Optional alternative uuids for control, packet or button
|
||||
* @returns Promise containing the device if it is still on a valid state
|
||||
*/
|
||||
public setDfuMode(device: BluetoothDevice): Promise<BluetoothDevice> {
|
||||
return this.gattConnect(device)
|
||||
public setDfuMode(device: BluetoothDevice, uuids?: { service?: number|string, control?: number|string, packet?: number|string, button?: number|string}): Promise<BluetoothDevice> {
|
||||
uuids = uuids || {};
|
||||
const serviceId = uuids.service || SecureDfu.SERVICE_UUID;
|
||||
const controlId = uuids.control || CONTROL_UUID;
|
||||
const packetId = uuids.packet || PACKET_UUID;
|
||||
const buttonId = uuids.button || BUTTON_UUID;
|
||||
return this.gattConnect(device, serviceId)
|
||||
.then(characteristics => {
|
||||
this.log(`found ${characteristics.length} characteristic(s)`);
|
||||
|
||||
const controlChar = characteristics.find(characteristic => {
|
||||
return (characteristic.uuid === CONTROL_UUID);
|
||||
return (characteristic.uuid === controlId);
|
||||
});
|
||||
|
||||
const packetChar = characteristics.find(characteristic => {
|
||||
return (characteristic.uuid === PACKET_UUID);
|
||||
return (characteristic.uuid === packetId);
|
||||
});
|
||||
|
||||
if (controlChar && packetChar) {
|
||||
@@ -439,7 +458,7 @@ export class SecureDfu extends EventDispatcher {
|
||||
}
|
||||
|
||||
const buttonChar = characteristics.find(characteristic => {
|
||||
return (characteristic.uuid === BUTTON_UUID);
|
||||
return (characteristic.uuid === buttonId);
|
||||
});
|
||||
|
||||
if (!buttonChar) {
|
||||
|
||||
Reference in New Issue
Block a user