I'm looking to develop a platform for my car which automatically connects any device which attempts to connect to the Pi via bluetooth. Currently it requires a pairing accept by the Pi but in this case there will be no screen so it will need to do it automatically. How can I accomplish this?
5 Answers
(Note that this answer is no longer working as of Raspbian Jessie, since bluez removed the bluetooth-agent command)
So currently you state that you can connect to the Pi just fine, but require a monitor for the pairing process.
Note: accepting any connection is an incredibly insecure idea, and if you are only using a limited set of devices, setting them up as trusted devices would make a lot more sense as it would limit connection to your Mac addresses but wold not allow for new devices to be added without a monitor
But anyway, the commands that you will need to run to accept all connections being made to the Pi, to make it discoverable and to set a predictable pin are listed below:
Note: you may need to change the zero in hci0 to your devices Bluetooth number.
hciconfig hci0 up
hciconfig hci0 sspmode 1
hciconfig hci0 piscan
sudo bluetooth-agent 1234
Now after running these commands and seeing if they work as expected we can move on to setting them up to start on the boot of your Pi.
- We start by creating a bash file by running the command
sudo nano mohammadIsAmazing.sh We now enter the following into that file:
#!/bin/sh sleep 20 hciconfig hci0 up hciconfig hci0 sspmode 1 hciconfig hci0 piscan sudo bluetooth-agent 1234Now we save and close our file using the
control key and xWe open up the file
/etc/rc.localusing the following command:Sudo nano /etc/rc.localNow we enter the command to run our bash script into
/etc/rc.localsudo /path/to/script/mohammadIsAmazing.sh &Note: you have to put the command before the last line in /etc/rc.local that contains:
exit 0We now save and close that file, then reboot the Pi.
On a side note: be sure to chose a random pin to marginally increase security
On another side note: If you would like to change the bluetooth device name, you need to create a file called /etc/machine-info which should contain the following:
PRETTY_HOSTNAME=device-name
Then run service bluetooth restart after that
- 123
- 5
- 2,373
- 1
- 12
- 18
A few years later, I did this on Raspberry Pi 4B, maybe this can help someone.
Steps: 1.Make Pi's Bluetooth discoverable and pairable. Create a bluetooth-pair.sh file with content as below at /home/pi/AutoStart
sudo bluetoothctl <<EOF
discoverable on
pairable on
exit
2.Correct the Bluetooth spec file to make time limit to nolimit.
sudo nano /etc/bluetooth/main.conf
set timelimit to 0
DiscoverableTimeout = 0
PairableTimeout = 0
3.Let the bluetooth-pair.sh file to be run automatically when Pi starts. Correct /etc/rc.local Add
sudo sh /home/pi/AutoStart/bluetooth-pair.sh
before
exit 0
This works on Pi 4B normally.
- 21
- 4
Based on what I understand what you require is a slave bluetooth module. You can use the popular HC-06 Module and connect it to the raspberry pi via a level shifter to the TX and RX pins on the Pi and use a python script to read the serial data and process it as required
- 296
- 2
- 6
In Raspberry Pi3 Model B there is a bulit in bluetooth and wifi. Using OBEX server you can receive the files from other devices without asking about pairing request every time. You have to do pairing first time with that device and make one directory, in that the files received all the time.
Just follow the step of below link.
https://www.raspberrypi.org/forums/viewtopic.php?p=963751#p963751
- 39
- 4
I'm not going to discuss the security implication or not but this is how I do it:
- Create a script called auto_connect.sh with the following content:
#!/bin/bash
bt-adapter --set Powered 1
bt-adapter --set DiscoverableTimeout 0
bt-adapter --set Discoverable 1
bt-adapter --set PairableTimeout 0
bt-adapter --set Pairable 1
/path/to/python_script.py >/dev/nul 2>dev/nul &
list=""
bt-device -l | grep -E -o '[[:xdigit:]]{2}(:[[:xdigit:]]{2}){5}' | { while read line
do
list="$list connect $line
"
done
bluetoothctl << EOF
$list
EOF
}
- Create the file /path/to/python_script.py with the following content:
#!/usr/bin/python3
from __future__ import absolute_import, print_function, unicode_literals
#import gobject
from gi.repository import GObject as gobject
import re
import dbus
import dbus.mainloop.glib
import subprocess
relevant_ifaces = [ "org.bluez.Adapter1", "org.bluez.Device1" ]
def property_changed(interface, changed, invalidated, path):
iface = interface[interface.rfind(".") + 1:]
for name, value in changed.iteritems():
val = str(value)
print("{%s.PropertyChanged} [%s] %s = %s" % (iface, path, name, val))
def interfaces_added(path, interfaces):
for iface in interfaces:
if not(iface in relevant_ifaces):
continue
try:
found = re.search('dev\_(..\_..\_..\_..\_..\_..)', path).group(1)
except AttributeError:
found = '' # apply your error handling
mac=found.replace("_",":")
cmd='echo -e "trust '+mac+' \\nconnect '+mac+' \\nquit" | bluetoothctl'
subprocess.call(cmd, shell=True)
def interfaces_removed(path, interfaces):
for iface in interfaces:
if not(iface in relevant_ifaces):
continue
print("{Removed %s} [%s]" % (iface, path))
if __name__ == '__main__':
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
bus.add_signal_receiver(interfaces_added, bus_name="org.bluez", dbus_interface="org.freedesktop.DBus.ObjectManager", signal_name="InterfacesAdded")
bus.add_signal_receiver(interfaces_removed, bus_name="org.bluez", dbus_interface="org.freedesktop.DBus.ObjectManager", signal_name="InterfacesRemoved")
mainloop = gobject.MainLoop()
mainloop.run()
- Depending on the OS you are using make sure you run the auto_connect.sh on your boot.
What this all does:
Sets the device to be always discoverable.
When a device pairs to it, it will automatically mark it at trusted device and connect to it.
On every boot it will itinerant through the list of known devices and will try to connect to them.
Please be mindful that this approach goes directly against any kind of security, but there might be user cases when you want to achieve just that.
- 101
- 2