Setting up Bluetooth automatic pairing on Linux [closed]
Asked Answered
M

2

33

I want to use any mobile phone to connect to a IoT device via Bluetooth Classic with the serial port protocol (SPP). The IoT device has no screen and no keyboard, and it's supposed to accept connections automatically as long as the connecting phone knows a secret PIN (ie, I don't want to be forced to ssh into the IoT device to set up pairing every time a new mobile phone tries to connect).

These are the commands that I've run so far on the IoT device:

# Make the device discoverable:
hciconfig hci0 piscan

# Register SPP:
sdptool add --channel=22 SP

# Start rfcomm:
rfcomm -r watch /dev/rfcomm0 22

Unfortunately, I couldn't find any way to set up a PIN, and pairing fails when I try to connect with a mobile phone. In addition, the device is only discoverable for a short interval.

How do I configure the IoT device's Bluetooth stack (running a recent Bluez) to auto pair with any phone that knows a given PIN, and how do I make the discovery period eternal?

Marismarisa answered 9/2, 2017 at 11:18 Comment(2)
You can make use of the "bluetoothctl" utility which ships with bluez library. You need to enable agent for authentication. As your system is headless, use can use "NoInputNoOutput". This avoids the interaction of passkey for authentication (bluetoothctl agen NoInputNoOutput). But if you want to accept all the connection, use the Dbus interface "org.bluez.AgentManager1" to register your own agent with minimum method "RequestAuthorization" implemented in "org.bluez.Agent1"Safe
Yes, it still asks for confirmation (yes/no). But you can answer it to "Yes" by writing your own simple Agent using Agent1 interface. Yes, BlueZ provides agent manager with "DisplayOnly", "DisplayYesNo", "KeyboardOnly", "NoInputNoOutput" and "KeyboardDisplay" these capabilities. As you need special case to accept without any intervention, you could achieve it with your own Agent1 sending "Yes"Safe
F
1

If anyone happens upon this question, to use (much of) the BlueZ "BT Management Sockets" C API directly from bash, try:

btmgmt --help

btmgmt add-device, btmgmt find, btmgmt discov, etc.

Frolic answered 9/5, 2021 at 18:23 Comment(0)
D
0

You can run an application which implements BlueZ DBus API. I recommands BlueZ >= 5.42. Use the agent interface and implements your own PIN code. After registring your agent, bluez will automaticaly call and use you own agent when a pairing is asked.

Moreover, you can set the DiscoverableTimeout to 0 through DBus with org.bluez.Adapter1 interface.

"A value of zero means that the timeout is disabled and it will stay in discoverable/limited mode forever."

https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt (dbus adapter object doc) https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/agent-api.txt (dbus agent object doc) https://kernel.googlesource.com/pub/scm/bluetooth/bluez/+/5.44/test/simple-agent (sample) http://www.bluez.org/bluez-5-api-introduction-and-porting-guide/ (read at the bottom)

Digitize answered 28/3, 2017 at 8:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.