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

Connecting to multiple devices using BLE? #1247

Open
4 tasks done
ejeng888 opened this issue Nov 3, 2024 · 0 comments
Open
4 tasks done

Connecting to multiple devices using BLE? #1247

ejeng888 opened this issue Nov 3, 2024 · 0 comments
Labels

Comments

@ejeng888
Copy link

ejeng888 commented Nov 3, 2024

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

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",
      }));
    }
  };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant