|
使用techBASIC直接在iPad上编写SensorTag应用
[复制链接]
本帖最后由 southwolf1813 于 2014-5-5 14:45 编辑
最近一直比较忙,好多时间都在外面跑,可是已经收了论坛的 SensorTag, 总不能不干活啊…
大家都知道安卓系统是基于linux的,完全可以拿来做一台小电脑来用,甚至可以装个开发编译环境,但是安卓对 BLE 的支持实在是不理想,而iOS虽然对BLE支持很好,但是没有苹果电脑就没法开发程序,怎么办呢?
前不久我找到了一款软件 techBASIC (收费的→_→),可以直接在iPad上开发一些简单的程序,包括BLE程序。对于像我这样有时候不方便开电脑的简直是神器啊…立马咬咬牙狠狠心掏出98大元买了一发。软件长得像这样:
虽然是古董级别的BASIC语言,虽然软件还是很简陋,不过至少满足了我的需求:不开电脑,开发 SensorTag 应用。
语法也比较简单,比如最简单的例子,扫描蓝牙设备并列出名称和RSSI信号强度
- DIM tag AS BLEPeripheral
- discoveredPeripheral = 0
- BLE.startBLE
- DIM uuid(0) AS STRING
- BLE.startScan(uuid)
- // 回调函数,发现 BLE 从设备时需要进行的操作
- SUB BLEDiscoveredPeripheral(time AS DOUBLE, peripheral AS BLEPeripheral,
- services() AS STRING, advertisements(,) AS STRING, rssi AS SINGLE)
- IF NOT discoveredPeripheral THEN
- discoveredPeripheral = 1
- somePeripheral = peripheral
- PRINT "Device: "; peripheral.bleName
复制代码 看过techBASIC的手册之后,发现这个软件还是能够实现一些基本的BLE应用,比如扫描、连接、查看Service,测量信号强度,控制BLE设备等。
我们来看一个操作 SensorTag LED的程序, 只用了100行左右的代码,实现了一个完整的BLE扫描、连接、Service/Characteristic操作和简单GUI界面,是不是方便多了!
- // 指定 Test Service 的 UUID
- DIM sensorTag AS BLEPeripheral
- DIM servicesHeader AS STRING, testService AS STRING
- servicesHeader = "-0451-4000-B000-000000000000"
- testService = "F000AA60" & servicesHeader
- // 扫描 BLE 外设
- BLE.startBLE
- DIM uuid(0) AS STRING
- BLE.startScan(uuid)
- DIM testChar AS BLECharacteristic
- DIM LED1 AS Button, LED2 AS Button, quit AS Button
- // 显示界面
- setUpGUI
- // 发现外设的回调函数
- SUB BLEDiscoveredPeripheral (time AS DOUBLE, peripheral AS BLEPeripheral, services() AS STRING, advertisements(,) AS STRING, rssi)
- IF peripheral.bleName = "TI BLE Sensor Tag" THEN
- sensorTag = peripheral
- BLE.connect(sensorTag)
- BLE.stopScan
- PRINT "Discovered SensorTag."
- END IF
- END SUB
- // 查找服务
- SUB BLEPeripheralInfo (time AS DOUBLE, peripheral AS BLEPeripheral, kind AS INTEGER, message AS STRING, err AS LONG)
- IF kind = 1 THEN
- peripheral.discoverServices(uuid)
- ELSE IF kind = 2 OR kind = 3 THEN
- BLE.connect(sensorTag)
- ELSE IF kind = 4 THEN
- DIM availableServices(1) AS BLEService
- availableServices = peripheral.services
- FOR a = 1 TO UBOUND(availableServices, 1)
- IF testService = availableServices(a).uuid THEN
- PRINT "Test Service Found"
- peripheral.discoverCharacteristics(uuid, availableServices(a))
- END IF
- NEXT
- END IF
- END SUB
- // 查找特征值
- SUB BLEServiceInfo (time AS DOUBLE, peripheral AS BLEPeripheral, service AS BLEService, kind AS INTEGER, message AS STRING, err AS LONG)
- IF kind = 1 THEN
- DIM characteristics(1) AS BLECharacteristic
- characteristics = service.characteristics
- FOR i = 1 TO UBOUND(characteristics, 1)
- IF characteristics(i).uuid = ("F000AA62" & servicesHeader) THEN
- PRINT "Test Char Found"
- testChar = characteristics(i)
- DIM value(1) as INTEGER
- value = [$80]
- peripheral.writeCharacteristic(testChar, value, 0)
- END IF
- NEXT
- END IF
- END SUB
- // 设置界面
- SUB setUpGUI
- Graphics.setPixelGraphics(0)
- DIM title AS Label
- title = Graphics.newLabel(20, 20, Graphics.width - 40, 50)
- title.setText("TI Sensor Tag")
- title.setAlignment(2)
- title.setFont("Arial", 36, 1)
- LED1 = Graphics.newButton(Graphics.width / 2 - 200, Graphics.height / 2 - 40)
- LED1.setTitle("LED1")
- LED1.setBackgroundColor(1, 1, 1)
- LED1.setGradientColor(0.6, 0.6, 0.6)
- LED2 = Graphics.newButton(Graphics.width / 2 + 100, Graphics.height / 2 - 40)
- LED2.setTitle("LED2")
- LED2.setBackgroundColor(1, 1, 1)
- LED2.setGradientColor(0.6, 0.6, 0.6)
- quit = Graphics.newButton(Graphics.width - 92, Graphics.height - 57)
- quit.setTitle("Quit")
- quit.setBackgroundColor(1, 1, 1)
- quit.setGradientColor(0.6, 0.6, 0.6)
- System.showGraphics
- END SUB
- // 按键处理
- SUB touchUpInside (ctrl AS Button, time AS DOUBLE)
- IF ctrl = LED1 THEN
- DIM value(1) as INTEGER
- value = [$81]
- sensorTag.writeCharacteristic(testChar, value, 1)
- END IF
- IF ctrl = LED2 THEN
- DIM value(1) as INTEGER
- value = [$82]
- sensorTag.writeCharacteristic(testChar, value, 1)
- END IF
- IF ctrl = quit THEN
- DIM value(1) as INTEGER
- value = [0]
- sensorTag.writeCharacteristic(testChar, value, 1)
- STOP
- END IF
- END SUB
复制代码
|
|