bluetooth.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  6. <meta name="HandheldFriendly" content="true">
  7. <meta name="MobileOptimized" content="320">
  8. <title>Bluetooth Document</title>
  9. <link rel="stylesheet" type="text/css" href="res/doc.css" charset="utf-8">
  10. <link rel="stylesheet" type="text/css" href="res/prettify.sons.css" charset="utf-8">
  11. <script type="text/javascript" src="res/doc.js" charset="utf-8"></script><script type="text/javascript" src="res/prettify.js" charset="utf-8"></script>
  12. </head>
  13. <body><div id="content" class="content">
  14. <h1><a name="plus.bluetooth">bluetooth</a></h1>
  15. <p>
  16. Bluetooth模块用于管理蓝牙设备,搜索附近蓝牙设备、实现简单数据传输等。</p>
  17. <h2>方法:</h2>
  18. <ul>
  19. <li>
  20. <a href="#plus.bluetooth.closeBluetoothAdapter">closeBluetoothAdapter</a>: 关闭蓝牙模块</li>
  21. <li>
  22. <a href="#plus.bluetooth.getBluetoothAdapterState">getBluetoothAdapterState</a>: 获取本机蓝牙适配器状态</li>
  23. <li>
  24. <a href="#plus.bluetooth.getBluetoothDevices">getBluetoothDevices</a>: 获取已搜索到的蓝牙设备</li>
  25. <li>
  26. <a href="#plus.bluetooth.getConnectedBluetoothDevices">getConnectedBluetoothDevices</a>: 根据uuid获取处于已连接的设备</li>
  27. <li>
  28. <a href="#plus.bluetooth.onBluetoothAdapterStateChange">onBluetoothAdapterStateChange</a>: 监听蓝牙适配器状态变化事件</li>
  29. <li>
  30. <a href="#plus.bluetooth.onBluetoothDeviceFound">onBluetoothDeviceFound</a>: 监听搜索到新设备的事件</li>
  31. <li>
  32. <a href="#plus.bluetooth.openBluetoothAdapter">openBluetoothAdapter</a>: 初始化蓝牙模块</li>
  33. <li>
  34. <a href="#plus.bluetooth.startBluetoothDevicesDiscovery">startBluetoothDevicesDiscovery</a>: 开始搜索附近的蓝牙设备</li>
  35. <li>
  36. <a href="#plus.bluetooth.stopBluetoothDevicesDiscovery">stopBluetoothDevicesDiscovery</a>: 停止搜寻附近的蓝牙外围设备</li>
  37. <li>
  38. <a href="#plus.bluetooth.closeBLEConnection">closeBLEConnection</a>: 断开与低功耗蓝牙设备的连接</li>
  39. <li>
  40. <a href="#plus.bluetooth.createBLEConnection">createBLEConnection</a>: 连接低功耗蓝牙设备</li>
  41. <li>
  42. <a href="#plus.bluetooth.getBLEDeviceCharacteristics">getBLEDeviceCharacteristics</a>: 获取蓝牙设备指定服务中所有特征值(characteristic)</li>
  43. <li>
  44. <a href="#plus.bluetooth.getBLEDeviceServices">getBLEDeviceServices</a>: 获取蓝牙设备的所有服务(service)</li>
  45. <li>
  46. <a href="#plus.bluetooth.notifyBLECharacteristicValueChange">notifyBLECharacteristicValueChange</a>: 启用低功耗蓝牙设备特征值变化时的notify功能,订阅特征值</li>
  47. <li>
  48. <a href="#plus.bluetooth.onBLECharacteristicValueChange">onBLECharacteristicValueChange</a>: 监听低功耗蓝牙设备的特征值变化事件</li>
  49. <li>
  50. <a href="#plus.bluetooth.onBLEConnectionStateChange">onBLEConnectionStateChange</a>: 监听低功耗蓝牙设备连接状态变化事件</li>
  51. <li>
  52. <a href="#plus.bluetooth.readBLECharacteristicValue">readBLECharacteristicValue</a>: 读取低功耗蓝牙设备指定特征值的二进制数据值</li>
  53. <li>
  54. <a href="#plus.bluetooth.writeBLECharacteristicValue">writeBLECharacteristicValue</a>: 向低功耗蓝牙设备指定特征值写入二进制数据</li>
  55. </ul>
  56. <h2>对象:</h2>
  57. <ul>
  58. <li>
  59. <a href="#plus.bluetooth.BluetoothDeviceInfo">BluetoothDeviceInfo</a>: 蓝牙设备信息</li>
  60. <li>
  61. <a href="#plus.bluetooth.BluetoothService">BluetoothService</a>: 蓝牙设备服务信息</li>
  62. <li>
  63. <a href="#plus.bluetooth.Bluetoothcharacteristic">Bluetoothcharacteristic</a>: 蓝牙设备特征值</li>
  64. <li>
  65. <a href="#plus.bluetooth.BluetoothcharacteristicProperties">BluetoothcharacteristicProperties</a>: 蓝牙设备特征值支持的操作类型</li>
  66. </ul>
  67. <h2>回调方法:</h2>
  68. <ul>
  69. <li>
  70. <a href="#plus.bluetooth.BluetoothSuccessCallback">BluetoothSuccessCallback</a>: 成功回调函数</li>
  71. <li>
  72. <a href="#plus.bluetooth.BluetoothFailCallback">BluetoothFailCallback</a>: 失败回调函数</li>
  73. <li>
  74. <a href="#plus.bluetooth.BluetoothCompleteCallback">BluetoothCompleteCallback</a>: 操作完成回调函数</li>
  75. <li>
  76. <a href="#plus.bluetooth.BluetoothAdapterStateChangeCallback">BluetoothAdapterStateChangeCallback</a>: 蓝牙适配器状态变化事件回调函数</li>
  77. <li>
  78. <a href="#plus.bluetooth.BluetoothDeviceFoundCallback">BluetoothDeviceFoundCallback</a>: 蓝牙适配器搜索到新设备事件回调函数</li>
  79. <li>
  80. <a href="#plus.bluetooth.BLEConnectionStateChangeCallback">BLEConnectionStateChangeCallback</a>: 低功耗蓝牙设备连接状态变化事件回调函数</li>
  81. <li>
  82. <a href="#plus.bluetooth.BLECharacteristicValueChangeCallback">BLECharacteristicValueChangeCallback</a>: 低功耗蓝牙设备的特征值变化事件回调函数</li>
  83. </ul>
  84. <h2>权限:</h2>
  85. <p>5+功能模块(permissions)</p>
  86. <pre class="prettyprint linenums">
  87. {
  88. // ...
  89. "permissions":{
  90. // ...
  91. "Bluetooth": {
  92. "description": "Bluetooth"
  93. }
  94. }
  95. }
  96. </pre>
  97. <h1><a name="plus.bluetooth.BluetoothDeviceInfo">BluetoothDeviceInfo</a></h1>
  98. <p>蓝牙设备信息</p>
  99. <pre class="prettyprint linenums">
  100. interface BluetoothDeviceInfo {
  101. readonly attribute String name;
  102. readonly attribute String deviceId;
  103. readonly attribute String RSSI;
  104. readonly attribute ArrayBuffer advertisData;
  105. readonly attribute Array&lt;String&gt; advertisServiceUUIDs;
  106. readonly attribute String localName;
  107. readonly attribute JSON serviceData;
  108. }
  109. </pre>
  110. <h2>属性:</h2>
  111. <ul>
  112. <li>name: <em>(<font class="type">String</font>
  113. 类型
  114. )</em>蓝牙设备名称<br><p>
  115. 某些设备可能没有此字段值。
  116. </p>
  117. </li>
  118. <li>deviceId: <em>(<font class="type">String</font>
  119. 类型
  120. )</em>蓝牙设备的id<br>
  121. </li>
  122. <li>RSSI: <em>(<font class="type">String</font>
  123. 类型
  124. )</em>蓝牙设备的信号强度<br>
  125. </li>
  126. <li>advertisData: <em>(<font class="type">ArrayBuffer</font>
  127. 类型
  128. )</em>蓝牙设备的广播数据段中的ManufacturerData数据段<br>
  129. </li>
  130. <li>advertisServiceUUIDs: <em>(<font class="type">Array</font>[
  131. <font class="type">String</font>
  132. ]
  133. 类型
  134. )</em>蓝牙设备的广播数据段中的ServiceUUIDs数据段<br>
  135. </li>
  136. <li>localName: <em>(<font class="type">String</font>
  137. 类型
  138. )</em>蓝牙设备的广播数据段中的LocalName数据段<br>
  139. </li>
  140. <li>serviceData: <em>(<font class="type">JSON</font>
  141. 类型
  142. )</em>蓝牙设备的广播数据段中的ServiceData数据段<br>
  143. </li>
  144. </ul>
  145. <h1><a name="plus.bluetooth.BluetoothService">BluetoothService</a></h1>
  146. <p>蓝牙设备服务信息</p>
  147. <pre class="prettyprint linenums">
  148. interface BluetoothService {
  149. readonly attribute String uuid;
  150. readonly attribute Boolean isPrimary;
  151. }
  152. </pre>
  153. <h2>属性:</h2>
  154. <ul>
  155. <li>uuid: <em>(<font class="type">String</font>
  156. 类型
  157. )</em>蓝牙设备服务的uuid<br>
  158. </li>
  159. <li>isPrimary: <em>(<font class="type">Boolean</font>
  160. 类型
  161. )</em>是否为设备的主服务<br>
  162. </li>
  163. </ul>
  164. <h1><a name="plus.bluetooth.Bluetoothcharacteristic">Bluetoothcharacteristic</a></h1>
  165. <p>蓝牙设备特征值</p>
  166. <pre class="prettyprint linenums">
  167. interface Bluetoothcharacteristic {
  168. readonly attribute String uuid;
  169. readonly attribute BluetoothcharacteristicProperties properties;
  170. }
  171. </pre>
  172. <h2>属性:</h2>
  173. <ul>
  174. <li>uuid: <em>(<font class="type">String</font>
  175. 类型
  176. )</em>蓝牙设备特征值的uuid<br>
  177. </li>
  178. <li>properties: <em>(<a href="#plus.bluetooth.BluetoothcharacteristicProperties">BluetoothcharacteristicProperties</a>
  179. 类型
  180. )</em>设备特征值支持的操作类型<br>
  181. </li>
  182. </ul>
  183. <h1><a name="plus.bluetooth.BluetoothcharacteristicProperties">BluetoothcharacteristicProperties</a></h1>
  184. <p>蓝牙设备特征值支持的操作类型</p>
  185. <pre class="prettyprint linenums">
  186. interface BluetoothcharacteristicProperties {
  187. readonly attribute Boolean read;
  188. readonly attribute Boolean write;
  189. readonly attribute Boolean notify;
  190. readonly attribute Boolean indicate;
  191. }
  192. </pre>
  193. <h2>属性:</h2>
  194. <ul>
  195. <li>read: <em>(<font class="type">Boolean</font>
  196. 类型
  197. )</em>特征值是否支持read操作<br>
  198. </li>
  199. <li>write: <em>(<font class="type">Boolean</font>
  200. 类型
  201. )</em>特征值是否支持write操作<br>
  202. </li>
  203. <li>notify: <em>(<font class="type">Boolean</font>
  204. 类型
  205. )</em>特征值是否支持notify操作<br>
  206. </li>
  207. <li>indicate: <em>(<font class="type">Boolean</font>
  208. 类型
  209. )</em>特征值是否支持indicate操作<br>
  210. </li>
  211. </ul>
  212. <h1><a name="plus.bluetooth.BluetoothSuccessCallback">BluetoothSuccessCallback</a></h1>
  213. <p>成功回调函数</p>
  214. <pre class="prettyprint linenums">
  215. void onSuccess(JSON event){
  216. }
  217. </pre>
  218. <h2>说明:</h2>
  219. <p class="des">
  220. 不同接口触发的成功回调参数event包含的属性存在差异,具体参考对应的接口描述说明。
  221. </p>
  222. <h2>参数:</h2>
  223. <ul><li>event:
  224. <em>(
  225. <font class="type">JSON</font>
  226. )
  227. 必选 </em>回调参数<br>
  228. 回调参数包含的属性由调用接口决定,具体参考对应的接口描述说明。
  229. </li></ul>
  230. <h2>返回值:</h2>
  231. <font class="type">void</font>
  232. : 无<h1><a name="plus.bluetooth.BluetoothFailCallback">BluetoothFailCallback</a></h1>
  233. <p>失败回调函数</p>
  234. <pre class="prettyprint linenums">
  235. function void onFail(Exception error){
  236. // Handle error
  237. var code = error.code; // 错误编码
  238. var message = error.message; // 错误描述信息
  239. }
  240. </pre>
  241. <h2>参数:</h2>
  242. <ul><li>error:
  243. <em>(
  244. <font class="type">Exception</font>
  245. )
  246. 必选 </em>回调参数,错误信息<br>
  247. 可通过error.code(Number类型)获取错误编码;
  248. 可通过error.message(String类型)获取错误描述信息。
  249. </li></ul>
  250. <h2>返回值:</h2>
  251. <font class="type">void</font>
  252. : 无<h1><a name="plus.bluetooth.BluetoothCompleteCallback">BluetoothCompleteCallback</a></h1>
  253. <p>操作完成回调函数</p>
  254. <pre class="prettyprint linenums">
  255. function void onComplete(JSON event){
  256. }
  257. </pre>
  258. <h2>说明:</h2>
  259. <p class="des">
  260. 调用成功或失败都会触发此回调。
  261. </p>
  262. <h2>参数:</h2>
  263. <ul><li>event:
  264. <em>(
  265. <font class="type">JSON</font>
  266. )
  267. 可选 </em>回调参数<br>
  268. 调用成功时回调参数与BluetoothSuccessCallback一致,调用失败时回调参数与BluetoothFailCallback一致。
  269. </li></ul>
  270. <h2>返回值:</h2>
  271. <font class="type">void</font>
  272. : 无<h1><a name="plus.bluetooth.BluetoothAdapterStateChangeCallback">BluetoothAdapterStateChangeCallback</a></h1>
  273. <p>蓝牙适配器状态变化事件回调函数</p>
  274. <pre class="prettyprint linenums">
  275. function void onStateChange(JSON event){
  276. // event.available
  277. // event.discovering
  278. }
  279. </pre>
  280. <h2>说明:</h2>
  281. <p class="des">
  282. 蓝牙适配器状态发生变化时触发此回调。
  283. </p>
  284. <h2>参数:</h2>
  285. <ul><li>event:
  286. <em>(
  287. <font class="type">JSON</font>
  288. )
  289. 可选 </em>返回参数<br>
  290. 回调函数参数event对象包括以下属性:
  291. available - Boolean,蓝牙适配器是否可用;
  292. discovering - Boolean,蓝牙适配器是否处于搜索状态。
  293. </li></ul>
  294. <h2>返回值:</h2>
  295. <font class="type">void</font>
  296. : 无<h1><a name="plus.bluetooth.BluetoothDeviceFoundCallback">BluetoothDeviceFoundCallback</a></h1>
  297. <p>蓝牙适配器搜索到新设备事件回调函数</p>
  298. <pre class="prettyprint linenums">
  299. function void onDeviceFound(JSON event){
  300. // event.devices
  301. }
  302. </pre>
  303. <h2>说明:</h2>
  304. <p class="des">
  305. 搜索到蓝牙设备时触发此回调。
  306. </p>
  307. <h2>参数:</h2>
  308. <ul><li>event:
  309. <em>(
  310. <font class="type">JSON</font>
  311. )
  312. 可选 </em>返回参数<br>
  313. 回调函数参数event对象包括以下属性:
  314. devices - Array&lt;BluetoothDeviceInfo&gt;,设备列表信息。
  315. </li></ul>
  316. <h2>返回值:</h2>
  317. <font class="type">void</font>
  318. : 无<h1><a name="plus.bluetooth.BLEConnectionStateChangeCallback">BLEConnectionStateChangeCallback</a></h1>
  319. <p>低功耗蓝牙设备连接状态变化事件回调函数</p>
  320. <pre class="prettyprint linenums">
  321. function void onStateChange(JSON event){
  322. // event.deviceId
  323. // event.connected
  324. }
  325. </pre>
  326. <h2>说明:</h2>
  327. <p class="des">
  328. 蓝牙设备连接状态发生变化时触发此回调。
  329. </p>
  330. <h2>参数:</h2>
  331. <ul><li>event:
  332. <em>(
  333. <font class="type">JSON</font>
  334. )
  335. 可选 </em>返回参数<br>
  336. 回调函数参数event对象包括以下属性:
  337. deviceId - String类型,蓝牙设备id;
  338. connected - Boolean类型,是否处于已连接状态。
  339. </li></ul>
  340. <h2>返回值:</h2>
  341. <font class="type">void</font>
  342. : 无<h1><a name="plus.bluetooth.BLECharacteristicValueChangeCallback">BLECharacteristicValueChangeCallback</a></h1>
  343. <p>低功耗蓝牙设备的特征值变化事件回调函数</p>
  344. <pre class="prettyprint linenums">
  345. function void onValueChange(JSON event){
  346. // event.deviceId
  347. // event.serviceId
  348. // event.characteristicId
  349. // event.value
  350. }
  351. </pre>
  352. <h2>说明:</h2>
  353. <p class="des">
  354. 蓝牙设备对应的特征值发生变化时触发此回调。
  355. </p>
  356. <h2>参数:</h2>
  357. <ul><li>event:
  358. <em>(
  359. <font class="type">JSON</font>
  360. )
  361. 可选 </em>返回参数<br>
  362. 回调函数参数event对象包括以下属性:
  363. deviceId - String类型,蓝牙设备id;
  364. serviceId - String类型,蓝牙服务的uuid;
  365. characteristicId - String类型,蓝牙特征值的uuid;
  366. value - ArrayBuffer类型,特征值的最新值。
  367. </li></ul>
  368. <h2>返回值:</h2>
  369. <font class="type">void</font>
  370. : 无<br><br>
  371. </div></body>
  372. </html>