본문 바로가기

언어/라즈베리파이

Bluetooth

 

.Installing Bluetooth

WARNING – Bluetooth on the RPi can be a nightmare and result in massive time loss!  If it just works for you then great (and consider sharing how). If it doesn't and after hitting your head against a wall for hour after hour it still doesn't it isn't just you (search online RPi forums for 'bluetooth' to get a feel for the number of issues people have).  We found massive problems getting a simple USB Bluetooth serial (rfcomm) link to work properly and ended up abandoning it after huge amounts of time lost to trying to troubleshoot it…

From the command line enter:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install bluetooth bluez-utils blueman

List the connected USB devices

lsusb

Find the Bluetooth adaptor, e.g.
Bus 001 Device 006: ID 0b05:17cb ASUSTek Computer, Inc.

Copy the ID without colon and write it into the /sys/bus/usb/drivers/btusb/new_id by adjusting and using the following command:

echo "0b05 17cb" >> /sys/bus/usb/drivers/btusb/new_id

Now use the following command to load it:

modprobe -v btusb

Now use the following command to edit the config:

nano /etc/default/bluetooth

Copy these lines into it or update what is there as needed:

Press CTRL+X to exit and select ‘Y’ to save

HID2HCI_ENABLED=0 
HID2HCI_UNDO=0 
HIDD_ENABLED=1

Restart Bluetooth:

sudo invoke-rc.d bluetooth restart

Bluetooth failed to work after this within the RPi GUI for us. This was the command line solution:

sudo nano /etc/group

move to the line:     lp:x:7:
Change it to be:     lp:x:7:pi

CTRL+X to exit, selecting ‘Y’ to save and then reboot

 

Bluetooth Commands

In these notes "xx:xx:xx:xx:xx:xx" means the MAC address of the remote bluetooth device

Reset Bluetooth Adaptor

sudo hciconfig hci0 reset

Restart Bluetooth Service

sudo invoke-rc.d bluetooth restart

List Bluetooth Adaptors
hciconfig Scan for Bluetooth devices

hcitool scan

To then connect to one of the found devices  you can use this:

sudo bluez-simple-agent hci0 xx:xx:xx:xx:xx:xx

Make The Raspberry Pi Discoverable

sudo hciconfig hci0 piscan

You will now be able to search and find the RPi from other devices such as windows "Add A Device" (development tip – if you've previously connected you'll need to remove it from Windows Devices for it to be able to found it again by Add A Device)

To allow the connection to complete you need to also do the following step to allow a remote device to actually connect:

sudo bluetooth-agent 1234

1234 is the pairing code for it to use, but this will can be automatically overridden by a randomly selected code displayed on both devices

Discover Bluetooth Devices

hcitool scan

List Previously Paired Devices

bluez-test-device list

(This will appear to do nothing if there are no devices).

Remove Previously Paired Device

You will need to get the devices MAC address by using the List command

If you need to disconnect it first:

bluez-test-device disconnect xx:xx:xx:xx:xx:xx

Then

bluez-test-device remove xx:xx:xx:xx:xx:xx

Ping Device

sudo l2ping -c 1 xx:xx:xx:xx:xx:xx

 

Serial over Bluetooth

Once you have your RPi paired with another device you have a Bluetooth link, but you still need to set up another protocol over the Bluetooth called "RFCOMM" to provide an emulated serial connection between the two devices.

Setting The RPi to create a serial connection to a Bluetooth device at startup

Enter the command:

sudo nano /etc/bluetooth/rfcomm.conf

Add the connection details:

rfcomm1 {
	# Automatically bind the device at startup
	bind yes;
	# Bl0uetooth address of the device
	device xx:xx:xx:xx:xx:xx;
	# RFCOMM channel for the connection
	channel 1;
	# Description of the connection
	comment "My Bluetooth Connection";
}

Creating A Serial Link Via Bluetooth To A PC Running Windows

Follow the RPI steps to setup the rfcom link at its end.

Open the Windows Control Panel.  In search box, type "Bluetooth", and then click "Change Bluetooth settings".

In the Bluetooth Settings dialog box, click the COM Ports tab, select a port, and then click OK.  If no COM port is listed, then setup Windows to accept a connection by pressing "Add", and then select "Incoming".  OK and exit.  Windows should then see the RPi serial connection after a few seconds and create a serial port automatically for it (you see a taskbar message its happened and the new COM port appears in device manager).

Adding The RPi From Windows

In Devices and printers use 'Add Device' and if the RPi is in pairing mode it should find the RPi.

Once the device is added and windows has finished adding drivers (can take quite a a while – wait for the taskbar device added message), right click the RPi in device and select properties.  In the 'Services' tab there should be a servicee for serial, e.g. 'serial port SSP'.  Ensure it is enabled.  If there isn't then there is likely a problem. <<< We got stuck here and had to give up due to time constraints as this was the case for us

Send serial from the RPi command line
echo "Hello" > /dev/rfcomm0