From ec6cf92eb0da7ad8ceee1dd57c7a6b9ff091c940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Migone?= Date: Thu, 2 Dec 2021 13:31:34 -0300 Subject: [PATCH] Support initiating pairing for devices that can't do it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-type: minor Signed-off-by: Tomás Migone --- README.md | 8 ++++++++ bluetooth-agent | 3 +-- entry.sh | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a2e69ab..6994d0b 100644 --- a/README.md +++ b/README.md @@ -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:
- Secure Simple Pairing (SSP): Secure pinless pairing method
- 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: diff --git a/bluetooth-agent b/bluetooth-agent index a7e58e6..295d2c5 100644 --- a/bluetooth-agent +++ b/bluetooth-agent @@ -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... diff --git a/entry.sh b/entry.sh index 5e98136..a3df591 100644 --- a/entry.sh +++ b/entry.sh @@ -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:" @@ -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 "$@"