I'm developing a BLE peripheral device based on linux(bluez 5.48) and QT app for a gatt service. I'm also developing an Android phone's app for connecting the device.
My problem is on second connection(#3) as below procedure,
- Device starts advertising -> Android app scan it and connect -> Android app try to write to an characteristic which have authentication security -> insufficient authentication occurred and pincode entry popped up on the phone -> Enter pincode and pairing success and the writing of the characteristic is OK.
- Disconnect
- Device starts advertising -> Android app scan it and connect -> Android app try to write to an characteristic which have authentication security -> insufficient authentication occurred and pincode entry popped up on the phone ????????????
I found "pin or key missing" error in the device's log. It seems my device recognizes the phone as a new device because of the phone's random MAC address. Any idea or solution for this problem?
Ps:The bluez device set to DisplayOnly
Thanks, Calvin
I added the device's log of above test sequence. Why is "Removing temporary device..." happening?
=============== first connection and pairing with my android test app: connect -> bonded -> disconnect from app =========
Dec 17 07:35:38 bluetoothd[368]: ../bluez-5.50/src/agent.c:agent_ref() 0xfd1000: ref=1
Dec 17 07:35:38 bluetoothd[368]: ../bluez-5.50/src/agent.c:register_agent() agent :1.49
Dec 17 07:35:38 bluetoothd[368]: ../bluez-5.50/src/agent.c:unregister_agent() agent :1.49
Dec 17 07:35:38 bluetoothd[368]: ../bluez-5.50/src/agent.c:agent_disconnect() Agent :1.49 disconnected
Dec 17 07:35:38 bluetoothd[368]: ../bluez-5.50/src/agent.c:agent_destroy() agent :1.49
Dec 17 07:35:38 bluetoothd[368]: ../bluez-5.50/src/agent.c:agent_unref() 0xfd1000: ref=0
Dec 17 07:35:38 bluetoothd[368]: ../bluez-5.50/src/agent.c:agent_ref() 0xfd1000: ref=1
Dec 17 07:35:38 bluetoothd[368]: ../bluez-5.50/src/agent.c:register_agent() agent :1.49
Dec 17 07:35:38 bluetoothd[368]: ../bluez-5.50/src/agent.c:add_default_agent() Default agent set to :1.49 /org/bluez/agent
Dec 17 07:35:46 bluetoothd[368]: ../bluez-5.50/src/adapter.c:connected_callback() hci0 device 5C:CB:99:7F:77:02 connected eir_len 0
Dec 17 07:35:47 bluetoothd[368]: ../bluez-5.50/src/adapter.c:user_passkey_notify_callback() hci0 5C:CB:99:7F:77:02
Dec 17 07:35:47 bluetoothd[368]: ../bluez-5.50/src/adapter.c:user_passkey_notify_callback() passkey 859060 entered 0
Dec 17 07:35:47 bluetoothd[368]: ../bluez-5.50/src/device.c:new_auth() Requesting agent authentication for 5C:CB:99:7F:77:02
Dec 17 07:35:47 bluetoothd[368]: ../bluez-5.50/src/agent.c:agent_ref() 0xfd1000: ref=2
Dec 17 07:35:56 bluetoothd[368]: ../bluez-5.50/src/adapter.c:new_irk_callback() hci0 new IRK for 5C:CB:99:7F:77:02 RPA 00:00:00:00:00:00
Dec 17 07:35:56 bluetoothd[368]: ../bluez-5.50/src/adapter.c:new_csrk_callback() hci0 new CSRK for 5C:CB:99:7F:77:02 type 3
Dec 17 07:35:56 bluetoothd[368]: ../bluez-5.50/src/adapter.c:new_csrk_callback() hci0 new CSRK for 5C:CB:99:7F:77:02 type 2
Dec 17 07:35:56 bluetoothd[368]: ../bluez-5.50/src/adapter.c:new_long_term_key_callback() hci0 new LTK for 5C:CB:99:7F:77:02 type 1 enc_size 16
Dec 17 07:35:56 bluetoothd[368]: ../bluez-5.50/src/device.c:device_set_bonded()
Dec 17 07:35:56 bluetoothd[368]: ../bluez-5.50/src/device.c:device_bonding_complete() bonding (nil) status 0x00
Dec 17 07:35:56 bluetoothd[368]: ../bluez-5.50/src/agent.c:agent_unref() 0xfd1000: ref=1
Dec 17 07:35:56 bluetoothd[368]: ../bluez-5.50/src/adapter.c:resume_discovery()
Dec 17 07:35:56 bluetoothd[368]: ../bluez-5.50/src/adapter.c:new_long_term_key_callback() hci0 new LTK for 5C:CB:99:7F:77:02 type 1 enc_size 16
Dec 17 07:35:56 bluetoothd[368]: ../bluez-5.50/src/device.c:device_set_bonded()
Dec 17 07:35:56 bluetoothd[368]: ../bluez-5.50/src/device.c:device_bonding_complete() bonding (nil) status 0x00
Dec 17 07:35:56 bluetoothd[368]: ../bluez-5.50/src/adapter.c:resume_discovery()
Dec 17 07:36:15 bluetoothd[368]: ../bluez-5.50/src/adapter.c:dev_disconnected() Device 5C:CB:99:7F:77:02 disconnected, reason 3
Dec 17 07:36:15 bluetoothd[368]: ../bluez-5.50/src/adapter.c:adapter_remove_connection() Dec 17 07:36:15 bluetoothd[368]: ../bluez-5.50/plugins/policy.c:disconnect_cb() reason 3
Dec 17 07:36:15 bluetoothd[368]: ../bluez-5.50/src/adapter.c:bonding_attempt_complete() hci0 bdaddr 5C:CB:99:7F:77:02 type 1 status 0xe
Dec 17 07:36:15 bluetoothd[368]: ../bluez-5.50/src/device.c:device_bonding_complete() bonding (nil) status 0x0e
Dec 17 07:36:15 bluetoothd[368]: ../bluez-5.50/src/device.c:device_bonding_failed() status 14
Dec 17 07:36:15 bluetoothd[368]: ../bluez-5.50/src/adapter.c:resume_discovery()
Dec 17 07:36:36 bluetoothd[368]: ../bluez-5.50/src/agent.c:agent_disconnect() Agent :1.49 disconnected
Dec 17 07:36:36 bluetoothd[368]: ../bluez-5.50/src/agent.c:remove_default_agent() Default agent cleared
Dec 17 07:36:36 bluetoothd[368]: ../bluez-5.50/src/agent.c:agent_destroy() agent :1.49
Dec 17 07:36:36 bluetoothd[368]: ../bluez-5.50/src/agent.c:agent_unref() 0xfd1000: ref=0
Dec 17 07:36:37 bluetoothd[368]: ../bluez-5.50/src/agent.c:agent_ref() 0xfd2808: ref=1
Dec 17 07:36:37 bluetoothd[368]: ../bluez-5.50/src/agent.c:register_agent() agent :1.51
Dec 17 07:36:37 bluetoothd[368]: ../bluez-5.50/src/agent.c:unregister_agent() agent :1.51
Dec 17 07:36:37 bluetoothd[368]: ../bluez-5.50/src/agent.c:agent_disconnect() Agent :1.51 disconnected
Dec 17 07:36:37 bluetoothd[368]: ../bluez-5.50/src/agent.c:agent_destroy() agent :1.51
Dec 17 07:36:37 bluetoothd[368]: ../bluez-5.50/src/agent.c:agent_unref() 0xfd2808: ref=0
============= second connection : this device unpaired unintentionally from the app system as soon as connected ===================================
Dec 17 07:36:37 bluetoothd[368]: ../bluez-5.50/src/agent.c:agent_ref() 0xfd2808: ref=1
Dec 17 07:36:37 bluetoothd[368]: ../bluez-5.50/src/agent.c:register_agent() agent :1.51
Dec 17 07:36:37 bluetoothd[368]: ../bluez-5.50/src/agent.c:add_default_agent() Default agent set to :1.51 /org/bluez/agent
Dec 17 07:36:42 bluetoothd[368]: ../bluez-5.50/src/adapter.c:connected_callback() hci0 device 4D:EE:4F:F1:6F:FD connected eir_len 0
Dec 17 07:36:42 bluetoothd[368]: ../bluez-5.50/src/device.c:device_create() dst 4D:EE:4F:F1:6F:FD
Dec 17 07:36:42 bluetoothd[368]: ../bluez-5.50/src/device.c:device_new() address 4D:EE:4F:F1:6F:FD
Dec 17 07:36:42 bluetoothd[368]: ../bluez-5.50/src/device.c:device_new() Creating device /org/bluez/hci0/dev_4D_EE_4F_F1_6F_FD
Dec 17 07:36:43 bluetoothd[368]: ../bluez-5.50/src/adapter.c:dev_disconnected() Device 4D:EE:4F:F1:6F:FD disconnected, reason 3
Dec 17 07:36:43 bluetoothd[368]: ../bluez-5.50/src/adapter.c:adapter_remove_connection()
Dec 17 07:36:43 bluetoothd[368]: ../bluez-5.50/src/adapter.c:adapter_remove_connection() Removing temporary device /org/bluez/hci0/dev_4D_EE_4F_F1_6F_FD
Dec 17 07:36:43 bluetoothd[368]: ../bluez-5.50/src/device.c:device_remove() Removing device /org/bluez/hci0/dev_4D_EE_4F_F1_6F_FD
Dec 17 07:36:43 bluetoothd[368]: ../bluez-5.50/src/device.c:btd_device_unref() Freeing device /org/bluez/hci0/dev_4D_EE_4F_F1_6F_FD
Dec 17 07:36:43 bluetoothd[368]: ../bluez-5.50/src/device.c:device_free() 0xff3058
Dec 17 07:36:43 bluetoothd[368]: ../bluez-5.50/plugins/policy.c:disconnect_cb() reason 3
Dec 17 07:36:43 bluetoothd[368]: ../bluez-5.50/src/adapter.c:bonding_attempt_complete() hci0 bdaddr 4D:EE:4F:F1:6F:FD type 2 status 0xe
Dec 17 07:36:43 bluetoothd[368]: ../bluez-5.50/src/adapter.c:resume_discovery()
Update:
While resolving the issue within Direct-BT, I was able to resolve the semantics about the
master
(==central
==initiator
) field of BlueZ/Kernel mgmt-api's Load Long Term Keys Command and New Long Term Key Event.BlueZ interprets this flag's
role
as follows:After resolving this issue, we got no more pin or key missing error when uploading the previously received and stored legacy LTK.
Note: Using secure connections (SC), the DH negotiated key is the same for both communication partner and no more transported between them.
Hope this helps a little.
+++
I experience this for 'pre-paired' devices using legacy security (no SC) on kernel >= 5.15 as well so far.
HCI event caught:
Not sure yet whether this is a BlueZ regression or mine within Direct-BT https://jausoft.com/cgit/direct_bt.git/about/ - but pre-pairing works on Debian 11, kernel 5.10 w/ same BLE server peripheral.
Interesting is that
HCI_ERROR_PIN_OR_KEY_MISSING 0x06
is only READ and not set in git-linux-stable 5.16 or bluetooth-next Linux kernel.The previously set encryption status via uploaded keys on the client/master was: