Bluetooth – Edison to Smart Phone

The Most Useful Thing You Can Do With Bluetooth – Connect To Your Phone

I’ve received a number of comments and questions about how to do this, so I figured I’d write up a full blog post on the topic. If you’re looking to connect your Edison to a Linux computer or other Edison, please see my post here instead, as this will be overkill.
  1. 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.
  2. 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.

  3.  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"
  4. Make Executable – Change the permissions to make the file executable
    $ chmod +x /usr/lib/bluez/test/simple-agent
  5. 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.

  6. Connect from Smart Phone – After all of the above is finished, go ahead and connect from the phone side.
  7. 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
Posted in Edison, Tech
5 comments on “Bluetooth – Edison to Smart Phone
  1. Areeb Mehmood says:

    Hi Stephanie. Can you clarify how we should connect from the Android side? e.g. must the phone be programmed to connect on the same port?

  2. Luca Di Stefano says:

    Hi Stephanie,
    That’s probably not the right place but i saw your video about the BMW helmet and i’d like to ask you a few questions like if it passed the SNELL test after the modifications, if there was a weight increase, and how much and about heat generation, if it could affect the helmet’s comfort and how it behaved with extremely warm weather ( ~40 C ).
    I am a motorcycle rider as well and work in IT so i’d be quite interested in some non official ( honest ) answers.
    If you think a better place where i can submit these questions please let me know.
    Thank you very much, and amazing work!

    Luca

  3. Qisong Wang says:

    Hi Stephanie. I followed above steps, I could get data sent from my phone and SPP app like “received: XXX”. Then I need to use such message to control other stuffs in my own python script.
    However, when I was trying pyserial, it gave an error message:
    “Traceback (most recent call last):
    File “serial-bluetooth.py”, line 2, in
    ser = serial.Serial(“/dev/rfcomm0”, timeout=0.1)
    File “/usr/lib/python2.7/site-packages/serial/serialutil.py”, line 282, in __i
    nit__
    self.open()
    File “/usr/lib/python2.7/site-packages/serial/serialposix.py”, line 289, in op
    en
    self.fd = os.open(self.portstr, os.O_RDWR|os.O_NOCTTY|os.O_NONBLOCK)
    OSError: [Errno 111] Connection refused: ‘/dev/rfcomm0′”

    Could you give any suggestions or alternatives so that I could at least read data from my phone using python?

    • Stephanie Moyerman says:

      Hey Qisong. I realize I’m super late on this (sorry!), but did you ever get this worked out. It looks like you never opened the rfcomm0 port or that something else had a hold of it, causing the socket open connection error.

      If you’ve not figured this out yet, or put it aside for some time, Intel has a great script that allows you to connect and disconnect your Edison at will from an Android phone https://software.intel.com/en-us/articles/connecting-the-intel-edison-board-to-your-android-phone-with-serial-port-profile-spp. The SPP-loopback.py script is very easy to modify to carry out your wanted commands. They advocate using their created app on android, but any bluetooth terminal app (BlueTerm for example) will also work.

      Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *

*