Wednesday, March 18, 2015

Using HotPaw Basic with Bluetooth 4.0/BLE/BTLE devices

Bluetooth LE is a great way to get data from external sensors into your iPhone, and without needing any wires or cables. Bluetooth LE, also known as Bluetooth 4.0, BTLE or BLE, is a standardized short-range wireless communications protocol, designed so the tiny sensors can run for weeks to years on just a coin cell battery (or less!). It's now used by such devices as newer heart rate monitors and fitness trackers.

Here's a tutorial on BTLE (written by a former colleague of mine) with far more details: http://chapters.comsoc.org/vancouver/BTLER3.pdf

One inexpensive BTLE device, usable for experimentation, is the TI SensorTag (around $25 or $30 from TI), smaller than a matchbook, that contains tons of sensors. Another very popular device is the LightBlue Bean, which contains a programmable Arduino processor and a thermometer, as well as a BTLE radio. Runs on a 2032 coin cell and small enough that it ships in a matchbox.

Here's an example of connecting to the TI SensorTag using HotPaw Basic, and getting notified by any change in the status of button 1.

100 rem
120 fn btle.init()
130 fn sleep(2)
140 status = fn btle.list()
150 print status
160 if status < 0 then print "BTLE is off!" : end
170 uuid$ = fn btle.selected()
180 if len(uuid$ < 2) then print "No tag selected" : end
400 print "SensorTag Found!"
410 servic$ = "FFE0"
420 charac$ = "FFE1"
440 fn btle.connect(1,uuid$,servic$,charac$)
450 fn sleep(1)
460 print fn btle.connect(1) : rem connection status
500 dim r%(256) : rem array for characteristic data
520 while (1)
530   fn sleep(0.5)
540   n = fn btle.responsedata(1,1,r%(0))
550   if (n > 0)
560     print n,r%(0)
570   endif
582   if fn touch(0) > 0 then exit while
590 wend
950 fn btle.connect(-1) :rem disconnect
960 print "done"
990 end
Tap the display with 2 fingers to exit the while loop.

No comments: