4

I'm pretty new to Bluetooth so this might be trivial, but I'll still ask:

I would like to connect 2 devices via Bluetooth - a mobile device with a Linux device (like Raspberry Pi, but another one...).

Side 1 - the mobile: It has an app that should pair with the Linux device, and send some data to it (a msg with "Hello" in this point).

Side 2 - the linux device: It should have a kind of listener to the fact that a device was connected to it via bluetooth, and then expect the data, receive it, and process it.

Side 1 is all fine and clear to me.

As for side 2, for now I only use some command line commands to turn Bluetooth on, set some name to the device, and wait for scan. I do it with "hciconfig", by running the following commands in Python script, one after the other:

hciconfig hci0 up
hciconfig hci0 name MyDevice
hciconfig hci0 sspmode 1
hciconfig hci0 piscan

At this point, my device is discover-able to my mobile and it pairs with it successfully. Now, I'm stuck with the listening part. I would like the linux device to run a certain function (prefer in Python) when the device is paired, and expect to receive data from it. I've read some links over the net, using RFCOMM and Bluez, but none succeeded...

Can someone please assist? Thanks

DanielY
  • 1,141
  • 30
  • 58

1 Answers1

5

Good morning, there is a library written in Python that handle Bluetooth connection for you already PyBluez to install use sudo pip install pybluez here is an example on how to use sockets to communicate with bluetooth devices

import bluetooth
bd_addr = "01:23:45:67:89:AB"
port = 1
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
sock.send("hello!!")
sock.close()

the complete guide is at Bluetooth Programming with PyBluez `

Andrew
  • 7,286
  • 3
  • 28
  • 38
  • I've seen that example before. The thing is that one side never knows in advance the MAC of the other, so what can I but in bd_addr? nothing... Another thing that it looks like the code that should be in the mobile side, the one that connects to the linux device. I'm looking for the code that should be in the Linux device that receives the data (not send) – DanielY Jul 11 '16 at 08:55
  • 1 - to know which MAC address you need to do a scan in both sides, the scan will give you the list of available MAC addresses, then use discovery to look for the actual names of devices so you know which MAC address to bind the socket to 2 - Once you have both MAC addresses bind them to sockets in both Linux machine and the mobile, then proceed to a normal sockets communication ( send recv in both sides ) – sayf eddine Hammemi Jul 11 '16 at 10:25
  • I partially understood...mobile app scans for BL devices, finds that device, and pairs to it. Now it knows its MAC address. How can the other device know that the mobile device is paired to it? – DanielY Jul 11 '16 at 11:26
  • using pybluez library is the same as using sockets to connect over internet, you dont need pairing, if you have the MAC address you can send data over bluetooth, so to answer your question, the device will be listening on that socket once it received something it knows it is "paired" to it – sayf eddine Hammemi Jul 11 '16 at 12:34
  • I tried to use the example from here: https://github.com/karulis/pybluez/blob/master/examples/simple/rfcomm-server.py and I get error '(2, 'no such file or directory') – DanielY Jul 11 '16 at 14:06