SureshJoshi.com ▼

Bluegiga Blueteeth OTA


2016-05-31

It’s been a bit under a year in the making, but I FINALLY got around to updating my BLE113 Android OTA app. Best part? I used my Blueteeth library!

Over a year ago, I released a post on over-the-air updates on the Bluegiga platform. In that post, I talked about the firmware required to perform reliable OTA updates, and provided sample iOS and Android implementations.

I had a working implementation, and then I started re-factoring as part of the original Blueteeth work, and I just never got back to it - so it stayed in a bit of disarray.

This past weekend, I went back in and re-wrote most of the app, basing the Bluetooth communication on my Blueteeth library. The net result of the re-write was about 2400 fewer lines of code, which was pretty awesome.

Another perk of this re-write was that I got to go in an make use of one of my favourite libraries… Okio!

I’m not sure why, but I just find this line to be so satisfying…

File otaFile = createTempFile(Okio.buffer(Okio.source(getResources().openRawResource(R.raw.ota_0_1_1))));

New App Flow

So, in the new OTA app, you select your device and then have two buttons representing the firmware you can upload. I’ve added two sample firmware files in as raw resources, instead of having to go through the hassle of copying stuff to the Downloads directory and searching by *.ota files.

When you click a button, the progress bar will increment, indicating firmware is being transferred. When it’s complete, you’ll get disconnected (there is no UI response to getting disconnected though), and your new firmware should be uploaded.

It’s Not All Fun and Games

I did notice a problematic callback, and I need to research it a bit to see what could be causing it, or if I was imagining it.

When running the following code, without the delay:

mPeripheral.writeCharacteristic(data, CHARACTERISTIC_DATA_ACK, SERVICE_OTA, response -> {
    mOnFirmwarePacketUploadedListener.call();
    mHandler.postDelayed(uploadNextPacket, 50);
});

I noticed that the GATT would get hung up. I’m not sure if I’m writing too fast (which shouldn’t happen with acknowledged writes), or if there is something else at play.

Just for the sake of it, I used a 50ms delay on the post just to remove race conditions from the equation, and it works great, and very fast!

Now… I need to put some tests into my Blueteeth library to see if this is problematic there too…