bluez / bluez

Main BlueZ tree
https://bluez.github.io/bluez/
GNU General Public License v2.0
721 stars 269 forks source link

Unable to persistently pair device in non-interactive mode (no LinkKey stored) #748

Open electricworry opened 7 months ago

electricworry commented 7 months ago

I've got an issue where I cannot use bluetoothctl non-interactive commands to properly pair my device (an 8BitDo controller). If I pair in interactive mode then the device pairs properly storing the LinkKey data for the device and also I notice that the store_hint field is 1 in the output:

[bluetooth]# scan on
hci0 type 7 discovering on
Discovery started
[CHG] Controller 72:66:E5:42:9F:F2 Discovering: yes
[NEW] Device E4:17:D8:CA:78:76 8BitDo M30 gamepad
hci0 type 7 discovering off
[bluetooth]# scan off
[CHG] Device E4:17:D8:CA:78:76 RSSI is nil
Discovery stopped
[bluetooth]# pair E4:17:D8:CA:78:76
Attempting to pair with E4:17:D8:CA:78:76
hci0 device_flags_changed: E4:17:D8:CA:78:76 (BR/EDR)
     supp: 0x00000000  curr: 0x00000000
hci0 E4:17:D8:CA:78:76 type BR/EDR connected eir_len 7
[CHG] Device E4:17:D8:CA:78:76 Connected: yes
[CHG] Device E4:17:D8:CA:78:76 Alias: E4-17-D8-CA-78-76
[CHG] Device E4:17:D8:CA:78:76 Name is nil
hci0 new_link_key E4:17:D8:CA:78:76 type 0x04 pin_len 0 store_hint 1
[CHG] Device E4:17:D8:CA:78:76 Bonded: yes
[CHG] Device E4:17:D8:CA:78:76 Modalias: usb:v2DC8p0651d0100
[CHG] Device E4:17:D8:CA:78:76 UUIDs: 00001124-0000-1000-8000-00805f9b34fb
[CHG] Device E4:17:D8:CA:78:76 UUIDs: 00001200-0000-1000-8000-00805f9b34fb
[CHG] Device E4:17:D8:CA:78:76 ServicesResolved: yes
[CHG] Device E4:17:D8:CA:78:76 Paired: yes
Pairing successful
[8BitDo M30 gamepad]# exit
root@orange-pi-zero3:/var/lib/bluetooth/72:66:E5:42:9F:F2# cat E4\:17\:D8\:CA\:78\:76/info 
[General]
Name=
Class=0x002508
SupportedTechnologies=BR/EDR;
Trusted=false
Blocked=false
WakeAllowed=true
Services=00001124-0000-1000-8000-00805f9b34fb;00001200-0000-1000-8000-00805f9b34fb;

[LinkKey]
Key=B90A2E4FAD48435EFE0D42B7CDE6201F
Type=4
PINLength=0

[DeviceID]
Source=2
Vendor=11720
Product=1617
Version=256

Good. But it I do everything the same but attempt to use non-interactive mode it doesn't store the LinkKey:

[bluetooth]# scan on 
hci0 type 7 discovering on
Discovery started
[CHG] Controller 72:66:E5:42:9F:F2 Discovering: yes
[NEW] Device E4:17:D8:CA:78:76 8BitDo M30 gamepad
hci0 type 7 discovering off
[bluetooth]# scan off
[CHG] Device E4:17:D8:CA:78:76 RSSI is nil
[CHG] Controller 72:66:E5:42:9F:F2 Discovering: no
Discovery stopped
[bluetooth]# exit
root@orange-pi-zero3:/var/lib/bluetooth/72:66:E5:42:9F:F2# bluetoothctl -- pair E4:17:D8:CA:78:76
Attempting to pair with E4:17:D8:CA:78:76
hci0 device_flags_changed: E4:17:D8:CA:78:76 (BR/EDR)
     supp: 0x00000000  curr: 0x00000000
hci0 E4:17:D8:CA:78:76 type BR/EDR connected eir_len 7
[CHG] Device E4:17:D8:CA:78:76 Connected: yes
[CHG] Device E4:17:D8:CA:78:76 Alias: E4-17-D8-CA-78-76
[CHG] Device E4:17:D8:CA:78:76 Name is nil
[DEL] Device 79:4B:51:EF:1B:64 79-4B-51-EF-1B-64
[DEL] Device 48:AC:67:15:A6:63 48-AC-67-15-A6-63
[DEL] Device 7D:FB:AA:86:C5:3A 7D-FB-AA-86-C5-3A
[DEL] Device 46:9A:5B:65:71:26 46-9A-5B-65-71-26
hci0 new_link_key E4:17:D8:CA:78:76 type 0x04 pin_len 0 store_hint 0
[CHG] Device E4:17:D8:CA:78:76 Modalias: usb:v2DC8p0651d0100
[CHG] Device E4:17:D8:CA:78:76 UUIDs: 00001124-0000-1000-8000-00805f9b34fb
[CHG] Device E4:17:D8:CA:78:76 UUIDs: 00001200-0000-1000-8000-00805f9b34fb
[CHG] Device E4:17:D8:CA:78:76 ServicesResolved: yes
[CHG] Device E4:17:D8:CA:78:76 Paired: yes
Pairing successful
root@orange-pi-zero3:/var/lib/bluetooth/72:66:E5:42:9F:F2# cat E4\:17\:D8\:CA\:78\:76/info 
[General]
Name=
Class=0x002508
SupportedTechnologies=BR/EDR;
Trusted=false
Blocked=false
WakeAllowed=true
Services=00001124-0000-1000-8000-00805f9b34fb;00001200-0000-1000-8000-00805f9b34fb;

[DeviceID]
Source=2
Vendor=11720
Product=1617
Version=256

It does 'pair' but because the LinkKey isn't stored in /var/lib/bluetooth on a reboot I need to go through pairing again.

Any ideas?

electricworry commented 7 months ago

The difference between running interactively and non-interactively would appear to be the call to agent register at client/main.c:491

I've not dug into the differences that this causes, but I can confirm that if I modify my build of bluez to call agent_register regardless of mode then LinkKey is properly recorded when paired in non-interactive mode.