Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android Connection timeout #600

Open
Rossella-Mascia-Neosyn opened this issue Nov 23, 2023 · 6 comments
Open

Android Connection timeout #600

Rossella-Mascia-Neosyn opened this issue Nov 23, 2023 · 6 comments
Labels
bug Something isn't working

Comments

@Rossella-Mascia-Neosyn
Copy link

Describe the bug
In my app it often happens that it times out

To Reproduce
Steps to reproduce the behavior:

  1. Connect the device to the ble service
  2. See error

Expected behavior
It should never return connect timeout

My code

  const scan = async (): Promise<void> => {
    try {
      await BleClient.initialize();
      const isEnable = await checkBluetoothStatus();
      if (isEnable) {
        const device = await BleClient.requestDevice({
          scanMode: ScanMode.SCAN_MODE_LOW_POWER,
          namePrefix: 'SMART_LOCK',
        });
        await connectBt(true, device);
        await BleClient.stopLEScan();
        console.info('stopped scanning');
      }
    } catch (error) {
      console.error(error);
    }
  };

 const connectBt = async (withState: boolean, deviceParam?: BleDevice) => {
    try {
      withState && setIsLoadingBle(true);
      const deviceStored = await getLocker();
      const device: LockerStorage = { macAddress: (deviceParam?.deviceId ?? '') || (deviceStored.macAddress ?? '') };
      if (!device.macAddress) throw new Error('macAddress not found');
      await BleClient.disconnect(device.macAddress);
      await BleClient.connect(device.macAddress);
      const services = await BleClient.getServices(device.macAddress);
      if (services[3]) {
        deviceParam?.deviceId &&
          setLocker({
            macAddress: deviceParam.deviceId,
            service: services[3],
          });
        withState && setIsLoadingBle(false);
        withState && setIsConnectBle(true);
      } else {
        console.error('service not found', services);
      }
    } catch (e) {
      console.error(e);
      setIsLoadingBle(false);
      setIsConnectBle(false);
      await toast({
        behavior: 'error',
        message: (e as ErrorBle).message,
        duration: 5000,
      });
    }
  };

Screenshots Logcat
image

Plugin version:

  • @capacitor-community/bluetooth-le: 3.1.1

Smartphone (please complete the following information):

  • Device: Samsung Galaxy S8
  • Android version 9

Additional context
I have already tried, as shown in my code, to insert disconnect and connect together

@Rossella-Mascia-Neosyn Rossella-Mascia-Neosyn added the bug Something isn't working label Nov 23, 2023
@mutasimissa
Copy link
Contributor

I don’t think this is a bug, the timeout means failed of connecting to a certain deviceId, please try to make sure you are connecting to the correct deviceId and if you are sure then try connecting to other peripheral and see if the problem is the same.

@Rossella-Mascia-Neosyn
Copy link
Author

Rossella-Mascia-Neosyn commented Nov 24, 2023

Actually it's just a bug I tried with another device I instead get the disconnection timeout error.
To make it work I had to remove disconnect before connecting (which you recommended)
image

I tried with: Redmi note 9 e android version 12.

I hope to be able to fix it together as soon as possible because for us this error is blocking

However it returns me connection timeout immediately and not after 10 seconds and there is no specific casuistry but it is random.

@madhan-SEET
Copy link

I'm also facing the same issue (Connection Timeout) . I tried to disconnect before connect still Its not working .

@quadratech-dev-admin
Copy link

Any solution for this?

@madhan-SEET
Copy link

I just increase my ble device advertisement packets previously 1 sec a packet and now 200 mill sec a packet . Now the connection timeout error decreased . Its not coming frequently but I cant able to avoid totally .

@peitschie
Copy link
Collaborator

The BLE advertising interval absolutely can impact this significantly. Even 200ms is still only middling. Apple recommends an interval of 20ms for at least the first 30s for best discovery, then has some suggested intervals to slow down at.

See https://developer.apple.com/accessories/Accessory-Design-Guidelines.pdf

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants