[X] I checked the documentation and FAQ without finding a solution
[X] I checked to make sure that this issue has not already been filed
[X] 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.
[X] I'm running the latest version
Question
I'm trying to make an app that allows for my phone to connect to multiple devices over BLE so I could send the same data to each one. I could not find any documentation relating to this or showing any examples. Right now, my code is repeatedly throwing out connection errors such as:
LOG List of connected devices: []
LOG Connection error: [BleError: Device ? is already connected]
LOG Connection error: [BleError: Operation was cancelled]
LOG Connection error: [BleError: Device ? is already connected]
LOG Connection error: [BleError: Operation was cancelled]
I believe the error has to do with my usage of bleManager.startDeviceScan as it looks like it's continuously scanning for devices when I currently want it to only find 2 devices and then stop. In addition, when I change it to only look for 1 device instead of 2, the code works and I am able to send data successfully.
Any help is appreciated. Thank you.
Question related code
number_of_devices = 2
const searchAndConnectToDevice = () => {
for(let i = 1; i < number_of_devices + 1; i++){
setConnected(false);
bleManager.startDeviceScan(null, null, (error, device) => {
if (error) {
console.error(error);
setConnectionStatus("Error searching for devices");
return;
}
//Check for esp 32 devices
let esp32_name = "sensor_" + i;
if (device.name === esp32_name) {
bleManager.stopDeviceScan();
console.log("Device name is: ", esp32_name);
setConnectionStatus(`Connecting...`);
connectToDevice(device);
setConnected(true);
}
});
}
};
const connectToDevice = async (device) => {
console.log("connect start");
try {
const connectedDevice = await device.connect();
console.log("Device Id is: ", connectedDevice.id)
//Put deviceID into connectedDevices array
setDeviceIDs((prevDevices) => [...prevDevices, connectedDevice.id]);
console.log("List of connected devices: ", deviceIDs)
await connectedDevice.discoverAllServicesAndCharacteristics();
return connectedDevice;
} catch (error) {
console.log("Connection error: ", error);
setConnectionStatus((prevStatus) => ({
...prevStatus,
[device.id]: "Error in Connection",
}));
}
};
Prerequisites
Question
I'm trying to make an app that allows for my phone to connect to multiple devices over BLE so I could send the same data to each one. I could not find any documentation relating to this or showing any examples. Right now, my code is repeatedly throwing out connection errors such as:
LOG List of connected devices: [] LOG Connection error: [BleError: Device ? is already connected] LOG Connection error: [BleError: Operation was cancelled] LOG Connection error: [BleError: Device ? is already connected] LOG Connection error: [BleError: Operation was cancelled]
I believe the error has to do with my usage of bleManager.startDeviceScan as it looks like it's continuously scanning for devices when I currently want it to only find 2 devices and then stop. In addition, when I change it to only look for 1 device instead of 2, the code works and I am able to send data successfully.
Any help is appreciated. Thank you.
Question related code