I am trying to execute a piece of code when a Bluetooth device is in range. What is the best way to do this? I have tried using 'hcitool scan' but that only produces a list of devices that are visable, mine won't be.
You can only find devices that are either paired or are in visible/discovery mode when you use
hcitool. That is because devices that do not meet those conditions simply won't respond when your Bluetooth chip queries for them.
If the device you are trying to detect is a Bluetooth Low Energy (BLE) device, you might be able to see it using
hcitool lescan because the discoverability rules for some BLE devices are different. However, this is not guaranteed to work either.
Most non-paired Bluetooth devices will still respond to a Bluetooth "ping", so if you know the address of the Bluetooth device you should be able to use
l2ping XX:XX:XX:XX:XX:XX on a schedule and see if you get a response.
- Open Terminal and type sudo apt-get install bluetooth bluez blueman. Press enter.
- Once the packages have downloaded and installed, type sudo reboot.
- To access this menu, on the Raspberry Pi desktop click Menu, located in the upper left corner of the screen, scroll down to Preferences with your cursor and click Bluetooth Manager. From there, you can pair to any nearby devices, or you can make your Raspberry Pi discoverable so you can pair your phone to it from your phone's Bluetooth settings. I hope it will help you.