Skip to content

Commit

Permalink
fix(bluetooth-low-energy): end app discovery on disconnect
Browse files Browse the repository at this point in the history
If the device disconnected there is no point in waiting, as there would
be no response and it will always run into a timeout.
  • Loading branch information
mKeRix committed Jan 10, 2021
1 parent 3b05297 commit ac2c13d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Expand Up @@ -1230,6 +1230,8 @@ describe('BluetoothLowEnergyService', () => {
manufacturerData: APPLE_MANUFACTURER_DATA,
},
discoverServicesAsync: jest.fn().mockResolvedValue([gattService]),
once: jest.fn(),
removeListener: jest.fn(),
} as unknown) as Peripheral;

bluetoothService.connectLowEnergyDevice.mockResolvedValue(peripheral);
Expand All @@ -1253,6 +1255,7 @@ describe('BluetoothLowEnergyService', () => {
manufacturerData: APPLE_MANUFACTURER_DATA,
},
discoverServicesAsync: jest.fn().mockResolvedValue([gattService]),
removeListener: jest.fn(),
} as unknown) as Peripheral;

bluetoothService.connectLowEnergyDevice.mockResolvedValue(peripheral);
Expand All @@ -1273,6 +1276,7 @@ describe('BluetoothLowEnergyService', () => {
manufacturerData: APPLE_MANUFACTURER_DATA,
},
discoverServicesAsync: jest.fn().mockResolvedValue([]),
removeListener: jest.fn(),
} as unknown) as Peripheral;

bluetoothService.connectLowEnergyDevice.mockResolvedValue(peripheral);
Expand All @@ -1298,6 +1302,7 @@ describe('BluetoothLowEnergyService', () => {
manufacturerData: APPLE_MANUFACTURER_DATA,
},
discoverServicesAsync: jest.fn().mockResolvedValue([gattService]),
removeListener: jest.fn(),
} as unknown) as Peripheral;

bluetoothService.connectLowEnergyDevice.mockResolvedValue(peripheral);
Expand Down Expand Up @@ -1326,6 +1331,7 @@ describe('BluetoothLowEnergyService', () => {
manufacturerData: APPLE_MANUFACTURER_DATA,
},
discoverServicesAsync: jest.fn().mockResolvedValue([gattService]),
removeListener: jest.fn(),
} as unknown) as Peripheral;

bluetoothService.connectLowEnergyDevice.mockResolvedValue(peripheral);
Expand Down Expand Up @@ -1353,6 +1359,7 @@ describe('BluetoothLowEnergyService', () => {
manufacturerData: APPLE_MANUFACTURER_DATA,
},
discoverServicesAsync: jest.fn().mockResolvedValue([gattService]),
removeListener: jest.fn(),
} as unknown) as Peripheral;

bluetoothService.connectLowEnergyDevice.mockResolvedValue(peripheral);
Expand Down
Expand Up @@ -140,13 +140,22 @@ export class BluetoothLowEnergyService
* @param tag - Peripheral to connect to
*/
async discoverCompanionAppId(tag: Tag): Promise<string | null> {
let disconnectListener: () => void;
const disconnectPromise = new Promise<string>((resolve) => {
disconnectListener = () => resolve(null);
tag.peripheral.once('disconnect', disconnectListener);
});

const peripheral = await this.bluetoothService.connectLowEnergyDevice(
tag.peripheral
);

try {
return await promiseWithTimeout<string>(
BluetoothLowEnergyService.readCompanionAppId(peripheral),
Promise.race([
BluetoothLowEnergyService.readCompanionAppId(peripheral),
disconnectPromise,
]),
15 * 1000
);
} catch (e) {
Expand All @@ -156,6 +165,7 @@ export class BluetoothLowEnergyService
);
return null;
} finally {
tag.peripheral.removeListener('disconnect', disconnectListener);
this.bluetoothService.disconnectLowEnergyDevice(tag.peripheral);
}
}
Expand Down

0 comments on commit ac2c13d

Please sign in to comment.