Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Support initiating pairing for devices that can't do it #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ The following environment variables allow some degree of configuration:
| `BLUETOOTH_HCI_INTERFACE` | The bluetooth interface to be used. | `hci0` | - |
| `BLUETOOTH_PAIRING_MODE` | The bluetooth paring mode:<br>- Secure Simple Pairing (SSP): Secure pinless pairing method<br>- Legacy Pairing: PIN code based authentication. Less secure but older devices might only support this mode. Note that this mode is no longer allowed on [iOS](https://developer.apple.com/accessories/Accessory-Design-Guidelines.pdf) devices. | `SSP` | `SSP`, `LEGACY` |
| `BLUETOOTH_PIN_CODE` | PIN code used for Legacy Pairing. Must be numeric and up to six digits (1 - 999999). | `0000` | - |
| `BLUETOOTH_CONNECT_DEVICE` | Initiate pairing with a device using the provided MAC address. Useful for devices that can't initiate the pairing process like bluetooth headsets. *| - | - |

*: You can find the MAC address by checking the logs of the bluetooth block. Look for a line with your device's name, the address will be on the previous line. For example, in this case `70:BF:12:F6:91:24` is the MAC address of the `Jabra Evolve 75` headset

```
[bluetooth] hci0 dev_found: 70:BF:12:F6:91:24 type BR/EDR rssi -29 flags 0x0000
[bluetooth] name Jabra Evolve 75
```

## Supported devices
The bluetooth block has been tested to work on the following devices:
Expand Down
3 changes: 1 addition & 2 deletions bluetooth-agent
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,9 @@ if __name__ == "__main__":
bluez_objects = bluez_manager.GetManagedObjects()
device_paths = dbus_filter_objects_by_interface(bluez_objects, "org.bluez.Device1")

print("Checking for known bluetooth devices...")
for device_path in device_paths:
props = dbus_get_all_properties("org.bluez", device_path, "org.bluez.Device1")
if bool(props["Paired"]) and bool(props["Trusted"]):
if bool(props["Paired"]):
print("- Attempting to reconnect to %s (%s)..." % (props["Name"], props["Address"]))

# Try to reconnect...
Expand Down
33 changes: 33 additions & 0 deletions entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ DEVICE_NAME=${BLUETOOTH_DEVICE_NAME:-$(printf "balenaOS %s"$(echo "$BALENA_DEVIC
HCI_INTERFACE=${BLUETOOTH_HCI_INTERFACE:-"hci0"}
PAIRING_MODE=${BLUETOOTH_PAIRING_MODE:-"SSP"}
PIN_CODE=${BLUETOOTH_PIN_CODE:-"0000"}
CONNECT_DEVICE="$BLUETOOTH_CONNECT_DEVICE"

echo "--- Bluetooth ---"
echo "Starting bluetooth service with settings:"
Expand Down Expand Up @@ -66,6 +67,38 @@ else
echo "Pairing mode set to 'Secure Simple Pairing Mode (SSPM)'. PIN code is NOT required."
fi

# Handle pairing with devices that can't initiate the pairing process
# Single device allowed via BLUETOOTH_CONNECT_DEVICE
# Bluetooth connection is handled by bluetooth-agent
if [[ -n "$CONNECT_DEVICE" ]]; then
echo "Bluetooth connect device provided, initiating pairing with device: $CONNECT_DEVICE"
PAIRED=$(bluetoothctl -- paired-devices | grep "$CONNECT_DEVICE" || echo "")

if [[ -z "$PAIRED" ]]; then
echo "Scanning for bluetooth devices... please set your device to pairing mode"
btmgmt --index $HCI_INTERFACE find
sleep 10

for i in {1..5}
do
echo "($i/5) Attempting to pair with device: $CONNECT_DEVICE"
btmgmt --index $HCI_INTERFACE pair "$CONNECT_DEVICE"

PAIRED=$(bluetoothctl -- paired-devices | grep "$CONNECT_DEVICE" || echo "")
if [[ -n "$PAIRED" ]]; then
break
fi

sleep 10
done

btmgmt --index $HCI_INTERFACE stop-find
echo "Scanning off"
else
echo "Device already paired: $CONNECT_DEVICE"
fi
fi

# If command starts with an option, prepend bluetooth-agent to it
if [[ "${1#-}" != "$1" ]]; then
set -- bluetooth-agent "$@"
Expand Down