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

Auto-Connect/Re-connect Issue with Ble Manager in React Native #1290

Open
prabu-nesh opened this issue Nov 26, 2024 · 2 comments
Open

Auto-Connect/Re-connect Issue with Ble Manager in React Native #1290

prabu-nesh opened this issue Nov 26, 2024 · 2 comments

Comments

@prabu-nesh
Copy link

I'm facing an issue with auto-connecting and re-connecting devices using BleManager in React Native. The connection process is working for the initial connection, but the subsequent reconnect attempts are failing. The goal is to connect to a peripheral, set the MTU size, retrieve services, and handle the connected device's characteristics. However, the reconnect logic does not seem to be functioning as expected.

Here is the code that I am currently using:

const onConnect = async (item) => {
try {
await new Promise(async () => {
await BleManager.connect(item);
// Await the MTU request
await BleManager.requestMTU(item, 517);
BleManager.retrieveServices(item).then((res) => {
// handle characteristics response
});
});
} catch (error) {
console.error("Connection error:", error);
}
};

const onConnectedDevices = async () => {
await BleManager.getConnectedPeripherals([]).then(async (peripherals) => {
if (peripherals.length > 0) {
onConnect(peripherals[0]?.id);
}
});
};

Expected Behavior:

  • The first connection attempt should succeed, and the MTU request and service retrieval should work fine.
  • On subsequent calls to onConnectedDevices, the device should reconnect successfully and handle the same operations (MTU request, retrieve services).

Current Behavior:

  • The initial connection works as expected, but the reconnection does not establish a stable connection.
  • The device may appear as connected, but operations like MTU size adjustment and service retrieval fail.
  • No error message is thrown, making it difficult to debug.
@rakshitbharat
Copy link

same here

@Aharta
Copy link

Aharta commented Feb 21, 2025

Hey @prabu-nesh @rakshitbharat , Thanks for sharing the problem, Here is my take into this -

  • First Issue that I found is in the await BleManager.requestMTU(item, 517) function, first parameter is peripheral's ID and second is the MTU size, where we can set the maximum value of 512, but you have set it to 517 which exceeds the limit. Try to set the value to 512.
  • Second is the method that you are using BleManager.getConnectedPeripherals([]), I think you have to pass serviceUUIDs to this Array, can take a look into this.

Here is the solution you can try (as I was also facing the same Issue, and I resolved it by doing this):
Whenever you initialize the BleManager.start() method, add restoreIdentifierKey: "test" and forceLegacy: true, This is the example for that :-
BleManager.start({
showAlert: false,
restoreIdentifierKey: "test",
forceLegacy: true,
})
By doing this what will happen is
restoreIdentifierKey(ios) : Enables background restoration of BLE state after app termination/restart.
forceLegacy : This option forces the library to use legacy scanning APIs on Android.

Let me know the further Updates regarding this, Thank You and Happy Hacking !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants