bluetooth.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. Page({
  2. data: {
  3. devid: '0D9C82AD-1CC0-414D-9526-119E08D28124',
  4. serid: 'FEE7',
  5. notifyId: '36F6',
  6. writeId: '36F5',
  7. charid: '',
  8. alldev: [{ deviceId: '' }],
  9. },
  10. //获取本机蓝牙开关状态
  11. openBluetoothAdapter() {
  12. my.openBluetoothAdapter({
  13. success: res => {
  14. if (!res.isSupportBLE) {
  15. my.alert({ content: '抱歉,您的手机蓝牙暂不可用' });
  16. return;
  17. }
  18. my.alert({ content: '初始化成功!' });
  19. },
  20. fail: error => {
  21. my.alert({ content: JSON.stringify(error) });
  22. },
  23. });
  24. },
  25. closeBluetoothAdapter() {
  26. my.closeBluetoothAdapter({
  27. success: () => {
  28. my.alert({ content: '关闭蓝牙成功!' });
  29. },
  30. fail: error => {
  31. my.alert({ content: JSON.stringify(error) });
  32. },
  33. });
  34. },
  35. getBluetoothAdapterState() {
  36. my.getBluetoothAdapterState({
  37. success: res => {
  38. if (!res.available) {
  39. my.alert({ content: '抱歉,您的手机蓝牙暂不可用' });
  40. return;
  41. }
  42. my.alert({ content: JSON.stringify(res) });
  43. },
  44. fail: error => {
  45. my.alert({ content: JSON.stringify(error) });
  46. },
  47. });
  48. },
  49. //扫描蓝牙设备
  50. startBluetoothDevicesDiscovery() {
  51. my.startBluetoothDevicesDiscovery({
  52. allowDuplicatesKey: false,
  53. success: () => {
  54. my.onBluetoothDeviceFound({
  55. success: res => {
  56. // my.alert({content:'监听新设备'+JSON.stringify(res)});
  57. var deviceArray = res.devices;
  58. for (var i = deviceArray.length - 1; i >= 0; i--) {
  59. var deviceObj = deviceArray[i];
  60. //通过设备名称或者广播数据匹配目标设备,然后记录deviceID后面使用
  61. if (deviceObj.name == this.data.name) {
  62. my.alert({ content: '目标设备被找到' });
  63. my.offBluetoothDeviceFound();
  64. this.setData({
  65. deviceId: deviceObj.deviceId,
  66. });
  67. break;
  68. }
  69. }
  70. },
  71. fail: error => {
  72. my.alert({ content: '监听新设备失败' + JSON.stringify(error) });
  73. },
  74. });
  75. },
  76. fail: error => {
  77. my.alert({ content: '启动扫描失败' + JSON.stringify(error) });
  78. },
  79. });
  80. },
  81. //停止扫描
  82. stopBluetoothDevicesDiscovery() {
  83. my.stopBluetoothDevicesDiscovery({
  84. success: res => {
  85. my.offBluetoothDeviceFound();
  86. my.alert({ content: '操作成功!' });
  87. },
  88. fail: error => {
  89. my.alert({ content: JSON.stringify(error) });
  90. },
  91. });
  92. },
  93. //获取正在连接中的设备
  94. getConnectedBluetoothDevices() {
  95. my.getConnectedBluetoothDevices({
  96. success: res => {
  97. if (res.devices.length === 0) {
  98. my.alert({ content: '没有在连接中的设备!' });
  99. return;
  100. }
  101. my.alert({ content: JSON.stringify(res) });
  102. devid = res.devices[0].deviceId;
  103. },
  104. fail: error => {
  105. my.alert({ content: JSON.stringify(error) });
  106. },
  107. });
  108. },
  109. //获取所有搜索到的设备
  110. getBluetoothDevices() {
  111. my.getBluetoothDevices({
  112. success: res => {
  113. my.alert({ content: JSON.stringify(res) });
  114. },
  115. fail: error => {
  116. my.alert({ content: JSON.stringify(error) });
  117. },
  118. });
  119. },
  120. bindKeyInput(e) {
  121. this.setData({
  122. devid: e.detail.value,
  123. });
  124. },
  125. //连接设备
  126. connectBLEDevice() {
  127. my.connectBLEDevice({
  128. deviceId: this.data.devid,
  129. success: res => {
  130. my.alert({ content: '连接成功' });
  131. },
  132. fail: error => {
  133. my.alert({ content: JSON.stringify(error) });
  134. },
  135. });
  136. },
  137. //断开连接
  138. disconnectBLEDevice() {
  139. my.disconnectBLEDevice({
  140. deviceId: this.data.devid,
  141. success: () => {
  142. my.alert({ content: '断开连接成功!' });
  143. },
  144. fail: error => {
  145. my.alert({ content: JSON.stringify(error) });
  146. },
  147. });
  148. },
  149. //获取连接设备的server,必须要再连接状态状态之下才能获取
  150. getBLEDeviceServices() {
  151. my.getConnectedBluetoothDevices({
  152. success: res => {
  153. if (res.devices.length === 0) {
  154. my.alert({ content: '没有已连接的设备' });
  155. return;
  156. }
  157. my.getBLEDeviceServices({
  158. deviceId: this.data.devid,
  159. success: res => {
  160. my.alert({ content: JSON.stringify(res) });
  161. this.setData({
  162. serid: res.services[0].serviceId,
  163. });
  164. },
  165. fail: error => {
  166. my.alert({ content: JSON.stringify(error) });
  167. },
  168. });
  169. },
  170. });
  171. },
  172. //获取连接设备的charid,必须要再连接状态状态之下才能获取(这里分别筛选出读写特征字)
  173. getBLEDeviceCharacteristics() {
  174. my.getConnectedBluetoothDevices({
  175. success: res => {
  176. if (res.devices.length === 0) {
  177. my.alert({ content: '没有已连接的设备' });
  178. return;
  179. }
  180. this.setData({
  181. devid: res.devices[0].deviceId,
  182. });
  183. my.getBLEDeviceCharacteristics({
  184. deviceId: this.data.devid,
  185. serviceId: this.data.serid,
  186. success: res => {
  187. my.alert({ content: JSON.stringify(res) });
  188. //特征字对象属性见文档,根据属性匹配读写特征字并记录,然后后面读写使用
  189. this.setData({
  190. charid: res.characteristics[0].characteristicId,
  191. });
  192. },
  193. fail: error => {
  194. my.alert({ content: JSON.stringify(error) });
  195. },
  196. });
  197. },
  198. });
  199. },
  200. //读写数据
  201. readBLECharacteristicValue() {
  202. my.getConnectedBluetoothDevices({
  203. success: res => {
  204. if (res.devices.length === 0) {
  205. my.alert({ content: '没有已连接的设备' });
  206. return;
  207. }
  208. this.setData({
  209. devid: res.devices[0].deviceId,
  210. });
  211. my.readBLECharacteristicValue({
  212. deviceId: this.data.devid,
  213. serviceId: this.data.serid,
  214. characteristicId: this.data.notifyId,
  215. //1、安卓读取服务
  216. // serviceId:'0000180d-0000-1000-8000-00805f9b34fb',
  217. // characteristicId:'00002a38-0000-1000-8000-00805f9b34fb',
  218. success: res => {
  219. my.alert({ content: JSON.stringify(res) });
  220. },
  221. fail: error => {
  222. my.alert({ content: '读取失败' + JSON.stringify(error) });
  223. },
  224. });
  225. },
  226. });
  227. },
  228. writeBLECharacteristicValue() {
  229. my.getConnectedBluetoothDevices({
  230. success: res => {
  231. if (res.devices.length === 0) {
  232. my.alert({ content: '没有已连接的设备' });
  233. return;
  234. }
  235. this.setData({
  236. devid: res.devices[0].deviceId,
  237. });
  238. my.writeBLECharacteristicValue({
  239. deviceId: this.data.devid,
  240. serviceId: this.data.serid,
  241. characteristicId: this.data.charid,
  242. //安卓写入服务
  243. //serviceId:'0000180d-0000-1000-8000-00805f9b34fb',
  244. //characteristicId:'00002a39-0000-1000-8000-00805f9b34fb',
  245. value: 'ABCD',
  246. success: res => {
  247. my.alert({ content: '写入数据成功!' });
  248. },
  249. fail: error => {
  250. my.alert({ content: JSON.stringify(error) });
  251. },
  252. });
  253. },
  254. });
  255. },
  256. notifyBLECharacteristicValueChange() {
  257. my.getConnectedBluetoothDevices({
  258. success: res => {
  259. if (res.devices.length === 0) {
  260. my.alert({ content: '没有已连接的设备' });
  261. return;
  262. }
  263. this.setData({
  264. devid: res.devices[0].deviceId,
  265. });
  266. my.notifyBLECharacteristicValueChange({
  267. state: true,
  268. deviceId: this.data.devid,
  269. serviceId: this.data.serid,
  270. characteristicId: this.data.notifyId,
  271. success: () => {
  272. //监听特征值变化的事件
  273. my.onBLECharacteristicValueChange({
  274. success: res => {
  275. // my.alert({content: '特征值变化:'+JSON.stringify(res)});
  276. my.alert({ content: '得到响应数据 = ' + res.value });
  277. },
  278. });
  279. my.alert({ content: '监听成功' });
  280. },
  281. fail: error => {
  282. my.alert({ content: '监听失败' + JSON.stringify(error) });
  283. },
  284. });
  285. },
  286. });
  287. },
  288. offBLECharacteristicValueChange() {
  289. my.offBLECharacteristicValueChange();
  290. },
  291. //其他事件
  292. bluetoothAdapterStateChange() {
  293. my.onBluetoothAdapterStateChange({
  294. success: res => {
  295. my.alert({ content: '本机蓝牙状态变化:' + JSON.stringify(res) });
  296. },
  297. fail: error => {
  298. my.alert({ content: JSON.stringify(error) });
  299. },
  300. });
  301. },
  302. offBluetoothAdapterStateChange() {
  303. my.offBluetoothAdapterStateChange();
  304. },
  305. BLEConnectionStateChanged() {
  306. my.onBLEConnectionStateChanged({
  307. success: res => {
  308. my.alert({ content: '连接状态变化:' + JSON.stringify(res) });
  309. },
  310. fail: error => {
  311. my.alert({ content: JSON.stringify(error) });
  312. },
  313. });
  314. },
  315. offBLEConnectionStateChanged() {
  316. my.offBLEConnectionStateChanged({
  317. success: res => {
  318. my.alert({ content: '取消监听成功!' });
  319. },
  320. });
  321. },
  322. });