Connected/disconnected to one of a list of bluetooth devices

Post your questions and help other users.

Moderator: Martin

Post Reply
rubaboo
Posts: 10
Joined: 20 Oct 2019 19:50

Connected/disconnected to one of a list of bluetooth devices

Post by rubaboo » 20 Oct 2019 21:44

The problem: I drive more than one car, and I'd like to set

Code: Select all

global_inCar=true;
when I connect to any of the cars, and

Code: Select all

global_inCar = false;
when I disconnect from any of the cars. So, I'm thinking I would trigger Bluetooth Device Connected (or Disconnected, accordingly) and then check if the device name is one of the names in a global list. But I don't see any function that would tell me if a string is in a list. Do I need to loop over the list to find out?

The alternative is to maintain two multiple trigger conditions, one for Connected and another for Disconnected, but I thought sharing a list between the two would be neater.

rubaboo
Posts: 10
Joined: 20 Oct 2019 19:50

Re: Connected/disconnected to one of a list of bluetooth devices

Post by rubaboo » 20 Oct 2019 23:26

Looked into it some more. Now I'm thinking it should be a trigger Bluetooth Device Connected (any), followed by a condition Bluetooth Device Connected, where I save connected_device_names to another variable, and then a script to find if connected device list and car device list intersect. What is the scope of connected_device_names variable, is it available in subsequent actions or only within the Bluetooth Device Connected condition? And how do I script finding if the intersection of two lists is empty or not?

rubaboo
Posts: 10
Joined: 20 Oct 2019 19:50

Re: Connected/disconnected to one of a list of bluetooth devices

Post by rubaboo » 21 Oct 2019 05:51

I solved it. Here is the flow and the script. The "False" branch is mainly for clarity. The flow will work correctly if both "True" and "False" are pointing to the same script, the one on the left (full source listed under the pic).
Screenshot_20191020-224732.jpg
Screenshot_20191020-224732.jpg (131.54 KiB) Viewed 7987 times

Code: Select all

if (isList(connected_devices_names))
{
  for (c in global_list_cars)
  {
    inCar=containsElement(connected_devices_names, c);
    if (inCar)
    {
      break;
    }
  }
  if (global_state_inCar!=inCar)
  {
    global_state_inCar=inCar;
  }
}
else
{
  global_state_inCar=false;
  log("NOT A LIST: connected_devices_names");
}

Post Reply