The Most Useful Thing You Can Do With Bluetooth – Connect To Your Phone
- Pair Edison and your Smart Phone and Establish Trust – Connect Edison and the Android phone using bluetoothctl as described in this post. You can skip the very last step where you actually connect, but make sure to do the “trust” thing before exiting bluetoothctl. You will only ever have to do this once unless you remove the paired devices from your phone or your Edison.
Get the simple-agent script – Edison and your smartphone need to do a handshake connection when they pair. This involves exchanging a 4 digit pin-code on both ends. You’ve probably seen this with other devices when you pair to them for the first time; they ask you to insert the pin-code on the smart phone side and it has to match the pre-programmed one on the device side.
In our case, we’ll use a script called simple-agent from the bluez testtools to handle this. You can either download the simple-agent script from it’s github repo or, if you have the unofficial repo installed (http://alextgalileo.altervista.org/edison-package-repo-configuration-instructions.html), you can install all the bluez testtools with
$ opkg install bluez5-testtools
If you uses opkg, it’ll be installed by default in the /usr/lib/bluez/test/ directory. If you pulled from the git repository, you can put the file anywhere that’s convenient.
- Set a pin code – By default, simple-agent will ask to set a pin on both sides that match. If you’re looking to connect without validating a pin on the Edison side (ie – just connect seamlessly from the phone), then edit the simple script so that the RequestPinCode function is always set to “1234” (or whatever else you like) instead of being requested
def RequestPinCode(self, device): print("RequestPinCode (%s)" % (device)) return "1234"
and so that the capability is set to “NoInputNoOutput”
capability = "NoInputNoOutput"
- Make Executable – Change the permissions to make the file executable
$ chmod +x /usr/lib/bluez/test/simple-agent
- Issue Bluetooth Commands – After doing all of the above, issue the following commands at the command line (or in a shell script)
$ rfkill unblock bluetooth $ hciconfig hci0 piscan $ hciconfig hci0 sspmode 0 $ /usr/lib/bluez/test/simple-agent & $ sdptool add --channel=22 SP $ rfcomm listen /dev/rfcomm0 22
The first line enables the bluetooth, the second line makes Edison discoverable, and the third configures simple secure pairing mode. Simple-agent is run to handle the pairing handshake and then a port is opened and used to listen for a connection.
- Connect from Smart Phone – After all of the above is finished, go ahead and connect from the phone side.
- Read/Write Serial on Edison – If the phone connected properly, you should see the /dev/rfcomm0 port becomes available. You can read/write data from/to the smartphone using this port. If you’re planning on using python for this, my recommended tool is pyserial. You can use pip to install it, and then it’s as simple as:
>>> import serial >>> ser = serial.Serial("/dev/rfcomm0",timeout=0.1) # Good idea to use a timeout or it will hang forever >>> ser.read(1) # Read a single byte >>> ser.read(10) # Read an array of 10 bytes >>> ser.write("hello") # Write an array of characters >>> b = [0, 2, 5, 7, 255] >>> ser.write(b) # Write an array of 5 bytes