Friday, 19 August 2016

How to switch off the need to pair your micro:bit when using Bluetooth

Pairing is the bedrock of all Bluetooth security. If you pair your micro:bit with your phone, for example, from that point on, only you can connect to and interact with your micro:bit. Imagine you were using your phone with my micro:bit Blue application to drive a  micro:bit controlled Kitronik buggy. You wouldn't want someone else connecting to it and driving it away, would you?

But not all applications need security. And pairing definitely impacts user experience. Furthermore, currently, whenever you flash a new hex file to your micro:bit, the pairing data gets wiped off your micro:bit so you have to 'forget' the pairing details on your phone and go through the entire pairing process again. Not great when you're performing develop/test cycles frequently.

Note: some changes are planned in this area. USB firmware is to be changed so that data on micro:bit, including pairing keys, will persist when new hex files are flashed. I'm also aiming to have a Block in Microsoft's PXT that lets you choose whether you want pairing or not and if you do, whether it should be based on the passkey system or Bluetooth's "Just Works" method. Watch this space for updates on both these developments.

If you're a micro:bit C/C++ developer, you can get under the hood in ways in which users of other tools often cannot. And this includes the ability to create a hex file which will result in your micro:bit not needing to be paired. Here's how.

In your project folder's root, you should have a file called config.json. All you need to do is ensure there's a property of the bluetooth json object called 'open' which has a value of 1. open=1 means 'no security'. Here's the first part of my config.json file:

{
    "microbit-dal": {
        "bluetooth": {
            "enabled": 1,
            "pairing_mode": 1,
            "open": 0,
            "dfu_service": 0,
            "event_service": 0,
            "device_info_service": 1
        },
        "gatt_table_size": "0x700"
    }
}



And that's it! Run 'yt clean' followed by 'yt build' and the resultant hex file will remove the need for your microbit to be paired when using Bluetooth.

Even with applications that need security, this is useful. You can do development and test cycles with pairing switched off, and enable it when you come to do your final testing and release build. The best of both worlds.

Use responsibly! :-)

Code it. Connect it. Bitty Software.

12 comments :

  1. I'm enjoying your posts about micro:bit Bluetooth. Can you discuss how flashing over Bluetooth work as well?

    I am having an issue where Samsung micro:bit app fails to flash over Bluetooth with an error "File not valid HEX." and once this happen I lost Bluetooth pairing and micro:bit, of course, fails to load the HEX intended.

    I like to find out why this issue happen and first step will be understanding how flashing over Bluetooth works on micro:bit.

    ReplyDelete
    Replies
    1. Sorry, since the message is coming from the Samsung app and assuming you are creating your hex file using one of the official tools (Blocks, TD, PXT, mbed, Yotta for example), I think you need to ask Samsung. Afaik the only way to post feedback to Samsung regarding their app is via Google Play.

      The basis for the device firmware update capability over Bluetooth however is a custom Bluetooth service designed by Nordic Semiconductor and implemented in the micro:bit firmware. See https://developer.nordicsemi.com/nRF5_SDK/nRF51_SDK_v8.x.x/doc/8.1.0/s110/html/a00105.html

      Delete
  2. Can't find the file config.json! I can see a module.json, but that already has something in it.

    Screenshot perhaps, or do I need to create it myself - well I tried that and got warnings.

    ReplyDelete
    Replies
    1. It needs to be in your project folder at the same level as your source folder. You may have to create it yourself. See https://github.com/lancaster-university/microbit-samples/tree/master/source/examples/bluetooth-services for an example. Set tx_power to 6 for better range.


      Delete
  3. Thanks for your help. The power setting seemed to help. However, I'm not sure I understand what I'm trying to accomplish here.

    Ive set up a couple of bluetooth service in the app :
    // Insert your code here!
    uBit.display.print("*");

    new MicroBitLEDService(*uBit.ble , uBit.display);
    new MicroBitButtonService(*uBit.ble);
    new MicroBitTemperatureService(*uBit.ble , uBit.thermometer);

    I'm using your microbit blue app and I can connect my phone going through the rigmarole of pairing.

    However if I use this config: (at the same level as main.cpp (using online mbed)

    {
    "microbit-dal": {
    "bluetooth": {
    "enabled": 1,
    "pairing_mode": 1,
    "private_addressing": 0,
    "open": 1,
    "whitelist": 1,
    "advertising_timeout": 0,
    "tx_power": 6,
    "dfu_service": 0,
    "event_service": 0,
    "device_info_service": 1
    },
    "gatt_table_size": "0x700"
    }
    }

    I still seem to need to go through the pairing rigmarole.

    I was expecting that once I copy the hex cross, do the three button thingy then I'd be paired, but the blue app comes up with errors, even if I re find paired devices (which it seems to do quite happily).

    Sorry I'm being a bit dense probably.

    What should I expect please :)

    ReplyDelete
    Replies
    1. Hi, config.json needs to be one level above main.cpp. So for example, my environment (offline since I'm using Yotta) looks like this:

      c:\workspaces
      ----- \microbit-samples (cloned repo)
      --------- \build
      --------- \source
      ---------------- main.cpp
      --------- \yotta_modules
      --------- \yotta_targets
      --------- config.json
      --------- README.MD

      and so on.

      Try moving config.json and try again. With open set to 1 you will not need to pair.



      Delete
    2. That seemed to work fine :) Thanks.

      I must admit that I quite like the online mbed. However, the main.cpp is in the root folder. Do you have any idea where the config.json could be placed so as to be able to use mbed?

      It's not a major issue (for me) so please don't expend any time on this.

      Gordon.

      Delete
  4. Hi, I might add that the Micro:Bit displays the pairing led display (before the arrow to A). I've tried with open : 0 just in case I've got it the wrong way round. I also notice a whole heap of warnings during compile.

    e.g.
    Warning: SystemTick inherits implicit virtual in "microbit/microbit-dal/inc/core/MicroBitSystemTimer.h", Line: 158, Col: 11
    Warning: ~MicroBitButton inherits implicit virtual in "microbit/microbit-dal/inc/drivers/MicroBitButton.h", Line: 142, Col: 6
    Warning: ~MicroBitAccelerometer inherits implicit virtual in "microbit/microbit-dal/inc/drivers/MicroBitAccelerometer.h", Line: 384, Col: 6
    ...

    After a while the Micro:bit seems to have reset itself (no pairing?) to display my asterisk in the first print statement.

    I just know I've done something silly :(

    ReplyDelete
  5. Hi Gordon, I've lost track of what you want to achieve but I think the fact you're using mbed (on-line) as opposed to Yotta (offline) hadn't properly registered with me. I don't think mbed uses config.json (but Yotta does). My mistake if that's the case. The other place you have access to configuration properties, which mbed definitely lets you change, is in this header file:

    microbit-dal/inc/core/MicroBitConfig.h

    In fact you have access to more properties there than are available through config.json.

    Hope this sets you on the right path

    Martin

    ReplyDelete
  6. Thanks, your help is very much appreciated. I'll check it out :)

    ReplyDelete
  7. Job done, yes the same code options - many thanks. Now to write my own Android app to work with it :)

    ReplyDelete