5310|0

78

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

使用techBASIC直接在iPad上编写SensorTag应用 [复制链接]

本帖最后由 southwolf1813 于 2014-5-5 14:45 编辑

最近一直比较忙,好多时间都在外面跑,可是已经收了论坛的 SensorTag, 总不能不干活啊…

大家都知道安卓系统是基于linux的,完全可以拿来做一台小电脑来用,甚至可以装个开发编译环境,但是安卓对 BLE 的支持实在是不理想,而iOS虽然对BLE支持很好,但是没有苹果电脑就没法开发程序,怎么办呢?

前不久我找到了一款软件 techBASIC (收费的→_→),可以直接在iPad上开发一些简单的程序,包括BLE程序。对于像我这样有时候不方便开电脑的简直是神器啊…立马咬咬牙狠狠心掏出98大元买了一发。软件长得像这样:





虽然是古董级别的BASIC语言,虽然软件还是很简陋,不过至少满足了我的需求:不开电脑,开发 SensorTag 应用。

语法也比较简单,比如最简单的例子,扫描蓝牙设备并列出名称和RSSI信号强度
  1. DIM tag AS BLEPeripheral
  2. discoveredPeripheral = 0
  3. BLE.startBLE
  4. DIM uuid(0) AS STRING
  5. BLE.startScan(uuid)

  6. // 回调函数,发现 BLE 从设备时需要进行的操作
  7. SUB BLEDiscoveredPeripheral(time AS DOUBLE, peripheral AS BLEPeripheral,
  8. services() AS STRING, advertisements(,) AS STRING, rssi AS SINGLE)
  9. IF NOT discoveredPeripheral THEN
  10. discoveredPeripheral = 1
  11. somePeripheral = peripheral
  12. PRINT "Device: "; peripheral.bleName
复制代码
看过techBASIC的手册之后,发现这个软件还是能够实现一些基本的BLE应用,比如扫描、连接、查看Service,测量信号强度,控制BLE设备等。

我们来看一个操作 SensorTag LED的程序, 只用了100行左右的代码,实现了一个完整的BLE扫描、连接、Service/Characteristic操作和简单GUI界面,是不是方便多了!
  1. // 指定 Test Service 的 UUID
  2. DIM sensorTag AS BLEPeripheral

  3. DIM servicesHeader AS STRING, testService AS STRING
  4. servicesHeader = "-0451-4000-B000-000000000000"
  5. testService = "F000AA60" & servicesHeader

  6. // 扫描 BLE 外设
  7. BLE.startBLE
  8. DIM uuid(0) AS STRING
  9. BLE.startScan(uuid)
  10. DIM testChar AS BLECharacteristic

  11. DIM LED1 AS Button, LED2 AS Button, quit AS Button

  12. // 显示界面
  13. setUpGUI

  14. // 发现外设的回调函数
  15. SUB BLEDiscoveredPeripheral (time AS DOUBLE, peripheral AS BLEPeripheral, services() AS STRING, advertisements(,) AS STRING, rssi)
  16. IF peripheral.bleName = "TI BLE Sensor Tag" THEN
  17.   sensorTag = peripheral
  18.   BLE.connect(sensorTag)
  19.   BLE.stopScan
  20.   PRINT "Discovered SensorTag."
  21. END IF
  22. END SUB

  23. // 查找服务
  24. SUB BLEPeripheralInfo (time AS DOUBLE, peripheral AS BLEPeripheral, kind AS INTEGER, message AS STRING, err AS LONG)
  25. IF kind = 1 THEN
  26.   peripheral.discoverServices(uuid)
  27. ELSE IF kind = 2 OR kind = 3 THEN
  28.   BLE.connect(sensorTag)
  29. ELSE IF kind = 4 THEN
  30.   DIM availableServices(1) AS BLEService
  31.   availableServices = peripheral.services
  32.   FOR a = 1 TO UBOUND(availableServices, 1)
  33.     IF testService = availableServices(a).uuid THEN
  34.       PRINT "Test Service Found"
  35.       peripheral.discoverCharacteristics(uuid, availableServices(a))
  36.     END IF
  37.   NEXT
  38. END IF
  39. END SUB

  40. // 查找特征值
  41. SUB BLEServiceInfo (time AS DOUBLE, peripheral AS BLEPeripheral, service AS BLEService, kind AS INTEGER, message AS STRING, err AS LONG)
  42. IF kind = 1 THEN
  43.   DIM characteristics(1) AS BLECharacteristic
  44.   characteristics = service.characteristics
  45.   FOR i = 1 TO UBOUND(characteristics, 1)
  46.     IF characteristics(i).uuid = ("F000AA62" & servicesHeader) THEN
  47.       PRINT "Test Char Found"
  48.       testChar = characteristics(i)
  49.       DIM value(1) as INTEGER
  50.       value = [$80]
  51.       peripheral.writeCharacteristic(testChar, value, 0)
  52.     END IF
  53.   NEXT
  54. END IF
  55. END SUB

  56. // 设置界面
  57. SUB setUpGUI
  58. Graphics.setPixelGraphics(0)

  59. DIM title AS Label
  60. title = Graphics.newLabel(20, 20, Graphics.width - 40, 50)
  61. title.setText("TI Sensor Tag")
  62. title.setAlignment(2)
  63. title.setFont("Arial", 36, 1)

  64. LED1 = Graphics.newButton(Graphics.width / 2 - 200, Graphics.height / 2 - 40)
  65. LED1.setTitle("LED1")
  66. LED1.setBackgroundColor(1, 1, 1)
  67. LED1.setGradientColor(0.6, 0.6, 0.6)

  68. LED2 = Graphics.newButton(Graphics.width / 2 + 100, Graphics.height / 2 - 40)
  69. LED2.setTitle("LED2")
  70. LED2.setBackgroundColor(1, 1, 1)
  71. LED2.setGradientColor(0.6, 0.6, 0.6)

  72. quit = Graphics.newButton(Graphics.width - 92, Graphics.height - 57)
  73. quit.setTitle("Quit")
  74. quit.setBackgroundColor(1, 1, 1)
  75. quit.setGradientColor(0.6, 0.6, 0.6)

  76. System.showGraphics
  77. END SUB

  78. // 按键处理
  79. SUB touchUpInside (ctrl AS Button, time AS DOUBLE)
  80. IF ctrl = LED1 THEN
  81.   DIM value(1) as INTEGER
  82.   value = [$81]
  83.   sensorTag.writeCharacteristic(testChar, value, 1)
  84. END IF
  85. IF ctrl = LED2 THEN
  86.   DIM value(1) as INTEGER
  87.   value = [$82]
  88.   sensorTag.writeCharacteristic(testChar, value, 1)
  89. END IF
  90. IF ctrl = quit THEN
  91.   DIM value(1) as INTEGER
  92.   value = [0]
  93.   sensorTag.writeCharacteristic(testChar, value, 1)
  94.   STOP
  95. END IF
  96. END SUB
复制代码



此帖出自无线连接论坛
点赞 关注
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/9 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表