Skip to content

Allow selecting BT adapter via BD address#370

Closed
Lenart12 wants to merge 4 commits intoteslamotors:mainfrom
Lenart12:set-bd-address
Closed

Allow selecting BT adapter via BD address#370
Lenart12 wants to merge 4 commits intoteslamotors:mainfrom
Lenart12:set-bd-address

Conversation

@Lenart12
Copy link
Contributor

@Lenart12 Lenart12 commented Feb 5, 2025

Description

Instead of only scanning hci devices for first available one (like #369), allow the user to define target Bluetooth device address, which uniquely identifies a hci device, making hci ordering changes not an issue. If address is not specified it will use the first available one. Also adds a small testing utility program in cmd/test-ble and configuration option for vehicle-control utility.

Right now only MAC address on linux is supported, but someone with a mac could probably implement something similar on Darwin, with UUIDs.

Example:

lenart@etera:~$ ./tesla-control -ble -bdAddr 43:45:c0:00:1f:ae -vin VIN body-controller-state
Error: failed to find a BLE device: can't down device: operation not permitted

Try again after granting this application CAP_NET_ADMIN:

        sudo setcap 'cap_net_admin=eip' "$(which ./tesla-control)"

lenart@etera:~$ sudo ./tesla-control -ble -bdAddr 43:45:c0:00:1f:ae -vin VIN body-controller-state
[sudo] password for lenart:
Error: failed to find a BLE device: no device with address 43:45:c0:00:1f:ae
lenart@etera:~$ sudo ./tesla-control -ble -bdAddr 43:45:c0:00:1f:ac -vin VIN body-controller-state
{
        "vehicleLockState": "VEHICLELOCKSTATE_LOCKED",
        "vehicleSleepStatus": "VEHICLE_SLEEP_STATUS_ASLEEP",
        "userPresence": "VEHICLE_USER_PRESENCE_NOT_PRESENT"
}
lenart@etera:~$ sudo ./tesla-control -ble -vin VIN body-controller-state
{
        "vehicleLockState": "VEHICLELOCKSTATE_LOCKED",
        "vehicleSleepStatus": "VEHICLE_SLEEP_STATUS_ASLEEP",
        "userPresence": "VEHICLE_USER_PRESENCE_NOT_PRESENT"
}
lenart@etera:~$ ./test-ble
2025-02-05T11:41:44+01:00 [info ] Target BLE device address:
2025-02-05T11:41:44+01:00 [debug] Creating new BLE device
2025-02-05T11:41:44+01:00 [debug] Scanning for HCI devices
2025-02-05T11:41:44+01:00 [debug] Can't init HCI 0: can't down device: operation not permitted
2025-02-05T11:41:44+01:00 [debug] Can't init HCI 1: can't down device: operation not permitted
2025-02-05T11:41:44+01:00 [debug] Can't init HCI 2: can't down device: operation not permitted
2025-02-05T11:41:44+01:00 [debug] Can't init HCI 3: can't down device: operation not permitted
2025-02-05T11:41:44+01:00 [debug] Can't init HCI 4: can't down device: operation not permitted
2025-02-05T11:41:44+01:00 [debug] Can't init HCI 5: can't down device: operation not permitted
2025-02-05T11:41:44+01:00 [debug] Can't init HCI 6: can't down device: operation not permitted
2025-02-05T11:41:44+01:00 [debug] Can't init HCI 7: can't down device: operation not permitted
2025-02-05T11:41:44+01:00 [debug] Can't init HCI 8: can't down device: operation not permitted
2025-02-05T11:41:44+01:00 [debug] Can't init HCI 9: can't down device: operation not permitted
2025-02-05T11:41:44+01:00 [debug] Can't init HCI 10: can't down device: operation not permitted
2025-02-05T11:41:44+01:00 [debug] Can't init HCI 11: can't down device: operation not permitted
2025-02-05T11:41:44+01:00 [debug] Can't init HCI 12: can't down device: operation not permitted
2025-02-05T11:41:44+01:00 [debug] Can't init HCI 13: can't down device: operation not permitted
2025-02-05T11:41:44+01:00 [debug] Can't init HCI 14: can't down device: operation not permitted
2025-02-05T11:41:44+01:00 [debug] Can't init HCI 15: can't down device: operation not permitted
2025-02-05T11:41:44+01:00 [error] No BLE device found
lenart@etera:~$ sudo ./test-ble
2025-02-05T11:41:53+01:00 [info ] Target BLE device address:
2025-02-05T11:41:53+01:00 [debug] Creating new BLE device
2025-02-05T11:41:53+01:00 [debug] Scanning for HCI devices
2025-02-05T11:41:53+01:00 [debug] Found HCI 0: 43:45:c0:00:1f:ac
2025-02-05T11:41:53+01:00 [debug] Using HCI 0
2025-02-05T11:41:53+01:00 [info ] BLE is working
lenart@etera:~$ sudo ./test-ble -bdAddr 43:45:c0:00:1f:ae
2025-02-05T11:42:05+01:00 [info ] Target BLE device address: 43:45:c0:00:1f:ae
2025-02-05T11:42:05+01:00 [debug] Creating new BLE device
2025-02-05T11:42:05+01:00 [debug] Scanning for HCI devices
2025-02-05T11:42:05+01:00 [debug] Found HCI 0: 43:45:c0:00:1f:ac
2025-02-05T11:42:05+01:00 [error] No BLE device found
lenart@etera:~$ sudo ./test-ble -bdAddr 43:45:c0:00:1f:ac
2025-02-05T11:42:07+01:00 [info ] Target BLE device address: 43:45:c0:00:1f:ac
2025-02-05T11:42:07+01:00 [debug] Creating new BLE device
2025-02-05T11:42:07+01:00 [debug] Scanning for HCI devices
2025-02-05T11:42:07+01:00 [debug] Found HCI 0: 43:45:c0:00:1f:ac
2025-02-05T11:42:07+01:00 [debug] Using HCI 0
2025-02-05T11:42:07+01:00 [info ] BLE is working

Fixes #261

Type of change

Please select all options that apply to this change:

  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Bug fix (non-breaking change which fixes an issue)
  • Documentation update

Checklist:

Confirm you have completed the following steps:

  • My code follows the style of this project.
  • I have performed a self-review of my code.
  • I have made corresponding updates to the documentation.
  • I have added/updated unit tests to cover my changes.

Lenart12 added a commit to Lenart12/TeslaBleHttpProxy that referenced this pull request Feb 5, 2025
Uses diferent vehicle-command, untill teslamotors/vehicle-command#370 gets merged
@Lenart12
Copy link
Contributor Author

Lenart12 commented Feb 8, 2025

Unforunately getting BD addres of a device by opening a HCI connection will break other applications using bluetooth... This type of feature wont be possible with current ble library.

@Lenart12
Copy link
Contributor Author

See #373

@Lenart12 Lenart12 closed this Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scan hci0 and hci1

1 participant