react-native-ble-plx: Issue with [BleError: Operation was cancelled] on iOS 17
Prerequisites
- I checked the documentation and FAQ without finding a solution
- I checked to make sure that this issue has not already been filed
- I’m sure that question is related to the library itself and not Bluetooth Low Energy or Classic in general. If that so, please post your question on StackOverflow.
- I’m running the latest version
Question
Hello,
I am encountering an issue with the [BleError: Operation was cancelled] error on iOS 17. This problem does not occur when testing on AOS, but it started appearing on iOS after a period of smooth operation. The issue persists consistently.
I have reviewed and attempted solutions suggested in previous issues, such as Connection Error 2 “Operation was cancelled” on IOS16 #1080. I tried using device.cancelConnection() before device.connect() and modified my code to ensure that new BleManager() is called only once by implementing a singleton pattern. Despite these efforts, the problem remains unresolved.
The most perplexing aspect is that the connection works fine on iOS initially, but the error reappears the next day upon retrying.
Additionally, I have verified iOS permissions, cleaned the iOS build folder, and retried the process. I am currently using the latest version of react-native-ble-plx (3.2.1).
Environment:
- iOS Version: 17
- react-native-ble-plx Version: 3.2.1
Any assistance or insights into resolving this issue would be greatly appreciated.
Thank you.
Question related code
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 2
- Comments: 17
Hi, I encountered this error as well, and fixed it. The error comes from iOS scanning very quickly in the
startDeviceScanfunction. The listener callback function activates multiple times for the same device id, causing the device to cancel all of the connections.To fix this, you can do the following:
optionsof thestartDeviceScanfunction, as followsstartDeviceScan()in a wrapper function, then having a variable calledcurrentDeviceIdat the top level of the function, then setting the listener function as an arrow function so that it can havecurrentDeviceIdin its scope. The implementation looks something like thisHope this helps!
@intent-kacper-cyranowski
I apologize for the delayed response to your comment. As the issue persists, I’ve attempted the following approach, which seems to work without any problems:
This method catches the “Operation was cancelled” error, destroys the existing instance, creates a new one, and then attempts to reconnect to the device.
Do you think this approach is acceptable to use?