Skip to content

Commit

Permalink
fix(bluetooth-low-energy): only disconnect from connected devices
Browse files Browse the repository at this point in the history
  • Loading branch information
mKeRix committed Jan 10, 2021
1 parent ac2c13d commit d54c53d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/integrations/bluetooth/bluetooth.service.spec.ts
Expand Up @@ -411,6 +411,7 @@ Requesting information ...

it('should disconnect from a peripheral', async () => {
const peripheral = {
state: 'connected',
disconnectAsync: jest.fn().mockResolvedValue(undefined),
};

Expand All @@ -421,6 +422,19 @@ Requesting information ...
expect(peripheral.disconnectAsync).toHaveBeenCalled();
});

it('should not try to disconnect from a peripheral that is not connected', async () => {
const peripheral = {
state: 'disconnected',
disconnectAsync: jest.fn().mockResolvedValue(undefined),
};

await service.disconnectLowEnergyDevice(
(peripheral as unknown) as Peripheral
);

expect(peripheral.disconnectAsync).not.toHaveBeenCalled();
});

it('should restart scanning if nothing has been detected for a while', async () => {
jest.useFakeTimers('modern');

Expand Down
4 changes: 4 additions & 0 deletions src/integrations/bluetooth/bluetooth.service.ts
Expand Up @@ -112,6 +112,10 @@ export class BluetoothService {
* @param peripheral - BLE peripheral to disconnect from
*/
async disconnectLowEnergyDevice(peripheral: Peripheral): Promise<void> {
if (!['connecting', 'connected'].includes(peripheral.state)) {
return;
}

this.logger.debug(
`Disconnecting from BLE device at address ${peripheral.address}`
);
Expand Down

0 comments on commit d54c53d

Please sign in to comment.