manekinekko / angular-web-bluetooth

The missing Web Bluetooth module for Angular
https://manekinekko.github.io/angular-web-bluetooth/
MIT License
195 stars 56 forks source link

Reconnection failed but success callback was executed #56

Closed ChewieWookie closed 1 year ago

ChewieWookie commented 5 years ago

Hi :)

Describe the bug When I try to reconnect to previously connected device and this reconnection failed because the device is no longer available ( turned off bluetooth ), success callback is called instead of error.

To Reproduce Steps to reproduce the behavior:

  1. Connect to device
  2. Turn off the device's bluetooth
  3. Wait for a reconnection try
  4. See that the success callback is executed

Expected behavior Method connectDevice$() should give an observable that in case of every failure in connecting a device will execute error callback

Code Invoking Reconnection

private reconnect() {
        console.info('Reconnecting');
        this.tryReconnect(3, 2,
            () => {
                return this.ble.connectDevice$(this.bluetoothDevice);
            },
            (server: BluetoothRemoteGATTServer) => {
                console.info('Reconnection success');
            },
            (error) => {
                console.error(error);
                throw error;
            }
        );
    }

Subscribing to Observable

tryReconnect(retries: number, delay: number, toTry:() => Observable<any>, success: (obj:any) => void, fail: (text: any) => void){
        console.info('Try count: ' + retries);
        toTry().subscribe(
                result => success(result),
                error => {
                    if(retries == 0){
                        return fail(error);
                    }
                    console.info('Retry: ' + retries);
                    setTimeout(() => {
                        this.tryReconnect(--retries, delay,toTry,success,fail);
                    }, delay * 1000);
                }
            )
    }

Here the error callback with console.info('Retry'.... ) newer actually logs that message. Instead console.info('Reconnection success') is logged.

manekinekko commented 5 years ago

hi @ChewieWookie thanks for reporting this issue. Can you provide a link to a stackblitz demo or a repo so I can try and debug it myself?