Skip to content

Cannot connect multiple devices simultaneously on Raspberry Pi #29

@highflying

Description

@highflying

Using a variant of peripheral-explorer.ts

  • I can connect to 2 devices at the same time on my Macbook (stays connected and stays running)
  • Only connect to 1 device on my Raspberry Pi (and this is on an older kernel/OS that doesn't have the connecting issues in Can't successfully connect BLE devices on Linux #12) and the discoverAsync for loop exits after handling the first device it connects to.
const noble = require("@stoprocent/noble");

const addresses = [
  "e44510a1bd3e0ae1d364d263a964918e",
  "e050454ee7b5ce4ed12b436fbabbe133",
  "90842b5ee58a",
  "90842b4eab24",
];

const main = async () => {
  await noble.waitForPoweredOnAsync();
  console.log("noble started");

  for await (const peripheral of noble.discoverAsync()) {
    if (addresses.includes(peripheral.id) === false) {
      continue;
    }
    console.log("connecting to", peripheral.id);

    console.log("peripheral", peripheral.address || peripheral.id);
    console.log("discovered", peripheral.advertisement);

    peripheral.on("error", (err) => console.error("emitted error", err));

    console.log("status", peripheral.state);
    try {
      await peripheral.connectAsync();
      console.log("connected");
    } catch (err) {
      console.error("err connecting", err);
      continue;
    }

    try {
      console.log("status", peripheral.state);
      console.log("preservices");
      const { services } =
        await peripheral.discoverAllServicesAndCharacteristicsAsync();
      console.log("postservices");

      for (const service of services) {
        console.log("Service\x1b[34m", service.uuid, "\x1b[0m");
        for (const characteristic of service.characteristics) {
          console.log(
            "  | Characteristic\x1b[32m",
            characteristic.uuid,
            "\x1b[0m"
          );
          console.log("  |  | Type:", characteristic.type);
          console.log("  |  | Properties:", characteristic.properties);

          if (characteristic.properties.includes("read")) {
            let value = await characteristic.readAsync();
            console.log(`  |  | Value: 0x${value.toString("hex")}`);
          }

          const descriptors = await characteristic.discoverDescriptorsAsync();
          for (const descriptor of descriptors) {
            console.log(
              "  |  | Descriptor\x1b[33m",
              descriptor.uuid,
              "\x1b[0m"
            );
            const value = await descriptor.readValueAsync();
            console.log(`  |  |  | Value: 0x${value.toString("hex")}`);
          }
        }
      }
    } catch (err) {
      console.error("err getting services", err);
      continue;
    }
  }

  noble.stop();
  console.log("noble stopped");
};

void main();

pi-output.txt
macbook-output.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions