Raspberry Pi – Bluetooth verbindingen maken
Je kan bluetooth gebruiken op de Raspberry Pi, en zo de services daarvan gebruiken op apparaten die deze ook ondersteunen.
Lees bluetooth dongle en instellingen hier
Met een bluetooth apparaat verbinden vanaf de Raspberry Pi
Je kan met bluez-simple-agent een verbinding maken met bijvoorbeeld je telefoon:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
NAME bluez-simple-agent - A PIN management and agent program for pairing to Bluetooth device. SYNOPSIS bluez-simple-agent [<hciX>] [<bdaddr>] [remove] DESCRIPTION bluez-simple-agent is pass agent program for bluetooth. OPTIONS <hciX> The command is applied to device hciX , which must be the name of an installed Blue- tooth device. If not specified, the command will be sent to the first available Blue- tooth device. <bdaddr> bdaddr of device doing pairing. remove Remove intended bdaddr from database. |
1) Zet je telefoon in “discovery” mode”.
2) Gebruik hcitool scan op de raspberry pi om het MAC adres van de telefoon te vinden:
1 2 |
Scanning ... 98:52:B1:4C:29:13 Starlord |
3) Gebruik dat mac adres om te koppelen met bluez-simple-agent:
sudo bluez-simple-agent hci0 98:52:B1:4C:29:13
4) voer de pincode in, bijvoorbeeld 0000:
1 2 |
RequestPinCode (/org/bluez/5419/hci0/dev_98_52_B1_4C_29_13) Enter PIN Code: 0000 |
Nu verschijnt er op je telefoon een koppelverzoek, voer hier dezelfde pincode in:
Na het koppelen zie je dit in de console van putty
1 2 |
Release New device (/org/bluez/5419/hci0/dev_98_52_B1_4C_29_13) |
En je telefoon is gekoppeld:
Weiger je de koppeling op de telefoon, dan verschijnt dit in de linux console:
Creating device failed: org.bluez.Error.AuthenticationRejected: Authentication Rejected
Foutmelding “No module named gobject”
Krijg je bij het gebruik van bluez-simple-agent een foutmelding als deze:
1 2 3 4 |
Traceback (most recent call last): File "/usr/bin/bluez-simple-agent", line 3, in <module> import gobject ImportError: No module named gobject |
Dan moet de module python-gobject nog geïnstalleerd worden, met het commando:
sudo apt-get install python-gobject
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Reading package lists... Done Building dependency tree... 50% Building dependency tree Reading state information... Done The following extra packages will be installed: python-gobject-2 Suggested packages: python-gobject-2-dbg The following NEW packages will be installed: python-gobject python-gobject-2 0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded. Need to get 717 kB of archives. After this operation, 1,609 kB of additional disk space will be used. Get:1 http://archive.raspberrypi.org/debian/ wheezy/main python-gobject all 3.8.2-1+rpi1rpi2 [242 kB] Get:2 http://mirrordirector.raspbian.org/raspbian/ wheezy/main python-gobject-2 armhf 2.28.6-10 [475 kB] Fetched 717 kB in 1s (474 kB/s) Selecting previously unselected package python-gobject-2. (Reading database ... 77087 files and directories currently installed.) Unpacking python-gobject-2 (from .../python-gobject-2_2.28.6-10_armhf.deb) ... Selecting previously unselected package python-gobject. Unpacking python-gobject (from .../python-gobject_3.8.2-1+rpi1rpi2_all.deb) ... Setting up python-gobject-2 (2.28.6-10) ... Setting up python-gobject (3.8.2-1+rpi1rpi2) ... |
Foutmelding “Already Exists”
Krijg je bij het gebruik van bluez-simple-agent een foutmelding als deze:
Creating device failed: org.bluez.Error.AlreadyExists: Already Exists
Dan is er al een connectie met het MAC adres, je kan het MAC adres ontkoppelen met het commando:
sudo bluez-test-device remove [MAC ADRES]
bijvoorbeeld:
sudo bluez-test-device remove 98:52:B1:4C:29:13
Met de Raspberry Pi verbinden vanaf een bluetooth apparaat
1) Zet de Raspberry Pi in “discovery” mode, met het commando:
sudo hciconfig hci0 piscan
2) start de agent met het commando:
sudo bluez-simple-agent hci0
Nu wordt er “geluisterd” voor een bluetooth verbinding.
3) Als je nu met bijvoorbeeld een telefoon verbinding maakt met de Raspberry Pi en je tikt de pin code in op de telefoon, verschijnt er in de console op de Raspberry Pi de vraag om de pin in te voeren:
1 2 3 |
Agent registered RequestPinCode (/org/bluez/3230/hci0/dev_98_52_B1_4C_29_13) Enter PIN Code: 0000 |
4) Na het invoeren van de juiste pincode zijn de apparaten gekoppeld.
Vreemd is wel dat je met CTRL+C het script moet beëindigen als je het via de console uitvoert, het script blijft in een loop hangen…
Ps. Je kan ook een PIPE gebruiken voor de wachtwoord zodat de Raspberry Pi automatisch verbind met de telefoon, gebruik daarvoor het voglende commando:
echo "0000" | sudo bluez-simple-agent hci0 [MAC ADRES]
Een bestand verzenden naar de Raspberry Pi
Om een bestand te verzenden via bluetooth op de Raspberry Pi heb je een OBEX (Object Exchange) service nodig voor uitwisselen objecten, zoals adresgegevens, agendagegevens, etc.
Installeer de OBEX pakketten met het commando:
sudo apt-get install obexftp obexpushd
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: libbfb0 libmulticobex1 libobexftp0 libopenobex1 Suggested packages: bluez-utils irda-utils The following NEW packages will be installed: libbfb0 libmulticobex1 libobexftp0 libopenobex1 obexftp obexpushd 0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded. Need to get 162 kB of archives. After this operation, 452 kB of additional disk space will be used. Do you want to continue [Y/n]? Y Get:1 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libbfb0 armhf 0.23-1.1+rpi1 [16.5 kB] Get:2 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libopenobex1 armhf 1.5-2+deb7u1 [21.3 kB] Get:3 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libmulticobex1 armhf 0.23-1.1+rpi1 [13.6 kB] Get:4 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libobexftp0 armhf 0.23-1.1+rpi1 [22.7 kB] Get:5 http://mirrordirector.raspbian.org/raspbian/ wheezy/main obexftp armhf 0.23-1.1+rpi1 [29.9 kB] Get:6 http://mirrordirector.raspbian.org/raspbian/ wheezy/main obexpushd armhf 0.11.2-1 [57.5 kB] Fetched 162 kB in 0s (165 kB/s) Selecting previously unselected package libbfb0. (Reading database ... 77172 files and directories currently installed.) Unpacking libbfb0 (from .../libbfb0_0.23-1.1+rpi1_armhf.deb) ... Selecting previously unselected package libopenobex1. Unpacking libopenobex1 (from .../libopenobex1_1.5-2+deb7u1_armhf.deb) ... Selecting previously unselected package libmulticobex1. Unpacking libmulticobex1 (from .../libmulticobex1_0.23-1.1+rpi1_armhf.deb) ... Selecting previously unselected package libobexftp0. Unpacking libobexftp0 (from .../libobexftp0_0.23-1.1+rpi1_armhf.deb) ... Selecting previously unselected package obexftp. Unpacking obexftp (from .../obexftp_0.23-1.1+rpi1_armhf.deb) ... Selecting previously unselected package obexpushd. Unpacking obexpushd (from .../obexpushd_0.11.2-1_armhf.deb) ... Processing triggers for man-db ... Setting up libbfb0 (0.23-1.1+rpi1) ... Setting up libopenobex1 (1.5-2+deb7u1) ... Setting up libmulticobex1 (0.23-1.1+rpi1) ... Setting up libobexftp0 (0.23-1.1+rpi1) ... Setting up obexftp (0.23-1.1+rpi1) ... Setting up obexpushd (0.11.2-1) ... |
Om de OBEX service te gebruiken start deze met het commando:
obexpushd -B
De -B parameter vertelt het programma te luisteren voor bluetooth connecties.
Zodra er een bestand wordt verzonden, van bijvoorbeeld van een telefoon:
wordt het bestand geplaatst in de folder waar je obexpushd hebt gestart, om een andere folder aan te wijzen om bestanden naartoe weg te schrijven gebruik de parameter -o, een voorbeeld om naar de HOME folder weg te schrijven:
obexpushd -B -o ~/
Je ziet dan de data/bestanden in de desbetreffende folder staan:
Let op: er kan maar 1 service van obexpushd actief zijn, gebruik het commando:
ps aux | grep obexpushd
Om te zien welke commandolijn parameters actief zijn, beëindig een service via een PID nummer met KILL, bijvoorbeeld:
kill 3949
Start dan opnieuw een obexpushd sessie met andere commandolijn parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
NAME obexpushd - receive files with OBEX protocol via Bluetooth, IrDA or network connection SYNOPSIS obexpushd [-B[channel]] [-I[app]] [-N[port]] [-p file] [-a file] [-r file] [-s file] [-n | -d] [-h | -v] DESCRIPTION obexpushd is a program that can be used to receive files using OBEX (OBject EXchange) protocol over Bluetooth, IrDA or network connection. It can be used to receive files from mobile phones and other devices. When run without -d or -n options obexpushd puts itself to the background and starts to listen for incoming connections via Bluetooth (default) and/or IrDA and/or network connection(TCP). obexpushd saves all received files to it´s current directory but can alternativly forward all received data to a script for further processing. OPTIONS -B Listen to Bluetooth connections. It is possible to specify a channel number to listen to. Default is to use channel 9. -I Listen to IrDA connections on the default inbox and, in addition and optionally, to the inbox identified by app (example: IrXfer) -N Listen to network connections. If compiled with TcpOBEX support (openobex > 1.3), it is possible to specify a port number to listen to (default: tcp/*:650). Note that to bind to the default TCP OBEX port, you need root priviledges. -p Write the process ID of the daemon to file -a Read user:password pairs from each line of file. Note that many client cannot handle authentication and will stop working when using this option. A realm of "obexpushd" will be sent to the client. Note that no encoding is used, the plain byte sequence will be matched. The user identifier must not be longer than 20 bytes and the password must not be longer than 128 bytes. If file starts with a pipe symbol, the file after the pipe symbol will be executed with one argument, the user name. The output shall be the same as a file line. -r Read realm:user:password triples from each line of file. This is only needed if a client wants a server that authenticates itself. The realm is a value sent by the client. User and password from the first realm match are used. The value of realm can have zero length, else it must be encoded in UTF-8. The values for user and password to not have any encoding, the plain byte sequence will be used. The user identifier must not be longer than 20 bytes and the password must not be longer than 128 bytes. If file starts with a pipe symbol, the file after the pipe symbol will be executed with one argument, the realm. The output shall be the same as a file line. -s Open pipes to the script or program specified by file. These pipes are seen by the program as stdin and stdout. There is one parameter, that can be either "put" or "get". Further parameters are fed to the script via stdin. With the first parameter being "put", data optionally follows on stdin. With "get", parameters and data are read via stdout. Parameters that are transmitted via stdin or stdout have the form "Parameter: value". The first empty line seperates parameters and data. The empty line can be the first line. In this case, no parameters are present. Line delimiter is system specific. The possible parameters are: · "From: type/client-identifier" which specifies the client address. Currently, types can be "bluetooth", "irda" or "tcp". The client identifier is an address (embraced in brackets) with optionally a port (seperated by a colon). · "Name: utf8-string" (required on "get" output) which specifies the file name, · "Length: uint32" (required on "get" output) which specifies the amount of data in bytes in the data section that follows, and · "Type: ascii-string" which specifies the mime type of the data. Unknown parameters shall be ignored. With "put", obexpushd is waiting for an ok after the list of parameters. The script must either print a line with "OK" to stdout to accept the input or a line with any other content to reject it. -n Do not detach from terminal. -d Enable debug messages (implies -n). -h Show summary of options. -v Show version of program. |
Bronnen: jamesrobertson.eu / manpages.ubuntu.com / zitzlinux.wordpress.com / elinux.org