ecBLEApp.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. const logEnable = false
  2. let isAndroid = false
  3. let ecBluetoothAdapterStateChangeCallback = () => {}
  4. let ecBluetoothDeviceFoundCallback = () => {}
  5. let ecBLEConnectionStateChangeCallback = () => {}
  6. let ecBLECharacteristicValueChangeCallback = () => {}
  7. let ecDeviceId = ''
  8. let ecGattServerUUID = ''
  9. const ecGattServerUUIDOption1 = '0000FFF0-0000-1000-8000-00805F9B34FB'
  10. const ecGattServerUUIDOption2 = 'FFF0'
  11. const yiyuanUUIDOption1 = '0000F536-0000-1000-8000-00805F9B34FB';
  12. const yiyuanUUIDOption2 = 'F536';
  13. let ecGattCharacteristicWriteUUID = ''
  14. const ecGattCharacteristicWriteUUIDOption1 = '0000FFF2-0000-1000-8000-00805F9B34FB'
  15. const ecGattCharacteristicWriteUUIDOption2 = 'FFF2'
  16. const yiyuancharacterID1 = '0000FF15-0000-1000-8000-00805F9B34FB'
  17. const yiyuancharacterID2 = 'FF15'
  18. import i18 from '@/utils/i18.js'
  19. const log = data => {
  20. if (logEnable) {
  21. console.log('[eciot]:' + JSON.stringify(data))
  22. }
  23. }
  24. const onBluetoothAdapterStateChange = cb => {
  25. ecBluetoothAdapterStateChangeCallback = cb
  26. }
  27. const _openBluetoothAdapter = () => {
  28. return new Promise(function(resolve, reject) {
  29. uni.openBluetoothAdapter({
  30. success(res) {
  31. log(res)
  32. resolve({
  33. ok: true,
  34. errCode: 0,
  35. errMsg: ''
  36. })
  37. },
  38. fail(res) {
  39. log(res)
  40. // {"errMsg":"openBluetoothAdapter:fail not available","code":10001}
  41. resolve({
  42. ok: false,
  43. errCode: res.code,
  44. errMsg: res.errMsg,
  45. })
  46. },
  47. })
  48. })
  49. }
  50. uni.onBluetoothAdapterStateChange(res => {
  51. log(res)
  52. // {"discovering":true,"available":true}
  53. if (!res.available) {
  54. ecBluetoothAdapterStateChangeCallback({
  55. ok: false,
  56. errCode: 30005,
  57. errMsg: i18('蓝牙适配器不可用'),
  58. })
  59. }
  60. })
  61. const openBluetoothAdapter = async () => {
  62. await _openBluetoothAdapter()
  63. const systemInfo = uni.getSystemInfoSync()
  64. log(systemInfo)
  65. if (systemInfo.platform.toLowerCase() === 'android') {
  66. isAndroid = true
  67. }
  68. const systemSetting = uni.getSystemSetting()
  69. log(systemSetting)
  70. const appAuthorizeSetting = uni.getAppAuthorizeSetting()
  71. log(appAuthorizeSetting)
  72. if (!systemSetting.bluetoothEnabled) {
  73. ecBluetoothAdapterStateChangeCallback({
  74. ok: false,
  75. errCode: 30001,
  76. errMsg: i18('请打开系统蓝牙开关'),
  77. })
  78. return
  79. }
  80. if (isAndroid && !systemSetting.locationEnabled) {
  81. ecBluetoothAdapterStateChangeCallback({
  82. ok: false,
  83. errCode: 30002,
  84. errMsg: i18('请打开系统定位开关'),
  85. })
  86. return
  87. }
  88. if (isAndroid && (appAuthorizeSetting.locationAuthorized!=='authorized')) {
  89. ecBluetoothAdapterStateChangeCallback({
  90. ok: false,
  91. errCode: 30003,
  92. errMsg: i18('请打开应用定位权限,允许应用使用您的位置信息'),
  93. })
  94. return
  95. }
  96. const openRes = await _openBluetoothAdapter()
  97. ecBluetoothAdapterStateChangeCallback(openRes)
  98. }
  99. uni.onBluetoothDeviceFound(res => {
  100. // log(res)
  101. const device = res.devices[0]
  102. const name = device.name ? device.name : device.localName
  103. if (!name) {
  104. return
  105. }
  106. let id = device.deviceId
  107. let rssi = device.RSSI
  108. ecBluetoothDeviceFoundCallback({
  109. id,
  110. name,
  111. rssi
  112. })
  113. })
  114. const onBluetoothDeviceFound = cb => {
  115. ecBluetoothDeviceFoundCallback = cb
  116. }
  117. const startBluetoothDevicesDiscovery = () => {
  118. uni.startBluetoothDevicesDiscovery({
  119. //services: [ecServerId],
  120. allowDuplicatesKey: true,
  121. // powerLevel: 'high',
  122. complete(res) {
  123. log(res)
  124. },
  125. })
  126. }
  127. const stopBluetoothDevicesDiscovery = () => {
  128. uni.stopBluetoothDevicesDiscovery({
  129. complete(res) {
  130. log(res)
  131. },
  132. })
  133. }
  134. const onBLEConnectionStateChange = cb => {
  135. ecBLEConnectionStateChangeCallback = cb
  136. }
  137. const _createBLEConnection = () => {
  138. return new Promise(function(resolve, reject) {
  139. uni.createBLEConnection({
  140. deviceId: ecDeviceId,
  141. success(res) {
  142. log(res)
  143. resolve({
  144. ok: true,
  145. errCode: 0,
  146. errMsg: ''
  147. })
  148. },
  149. fail(res) {
  150. log(res)
  151. resolve({
  152. ok: false,
  153. errCode: res.code,
  154. errMsg: res.errMsg,
  155. })
  156. },
  157. })
  158. })
  159. }
  160. const getBLEDeviceServices = () => {
  161. return new Promise(function(resolve, reject) {
  162. setTimeout(()=>{
  163. uni.getBLEDeviceServices({
  164. deviceId: ecDeviceId,
  165. success(res) {
  166. log(res)
  167. resolve({
  168. ok: true,
  169. errCode: 0,
  170. errMsg: '',
  171. services: res.services,
  172. })
  173. },
  174. fail(res) {
  175. log(res)
  176. resolve({
  177. ok: false,
  178. errCode: res.code,
  179. errMsg: res.errMsg
  180. })
  181. },
  182. })
  183. },1200)//之前是800ms,要休眠到1200ms
  184. })
  185. }
  186. const getBLEDeviceCharacteristics = serviceId => {
  187. return new Promise(function(resolve, reject) {
  188. uni.getBLEDeviceCharacteristics({
  189. deviceId: ecDeviceId,
  190. serviceId,
  191. success(res) {
  192. log(res)
  193. resolve({
  194. ok: true,
  195. errCode: 0,
  196. errMsg: '',
  197. characteristics: res.characteristics,
  198. })
  199. },
  200. fail(res) {
  201. log(res)
  202. resolve({
  203. ok: false,
  204. errCode: res.code,
  205. errMsg: res.errMsg
  206. })
  207. },
  208. })
  209. })
  210. }
  211. const notifyBLECharacteristicValueChange = (serviceId, characteristicId) => {
  212. return new Promise(function(resolve, reject) {
  213. uni.notifyBLECharacteristicValueChange({
  214. state: true,
  215. deviceId: ecDeviceId,
  216. serviceId,
  217. characteristicId,
  218. success(res) {
  219. log(res)
  220. resolve({
  221. ok: true,
  222. errCode: 0,
  223. errMsg: ''
  224. })
  225. },
  226. fail(res) {
  227. log(res)
  228. resolve({
  229. ok: false,
  230. errCode: res.code,
  231. errMsg: res.errMsg
  232. })
  233. },
  234. })
  235. })
  236. }
  237. const setBLEMTU = mtu => {
  238. return new Promise(function(resolve, reject) {
  239. setTimeout(()=>{
  240. uni.setBLEMTU({
  241. deviceId: ecDeviceId,
  242. mtu,
  243. success(res) {
  244. log(res)
  245. resolve({
  246. ok: true,
  247. errCode: 0,
  248. errMsg: ''
  249. })
  250. },
  251. fail(res) {
  252. log(res)
  253. resolve({
  254. ok: false,
  255. errCode: res.code,
  256. errMsg: res.errMsg
  257. })
  258. },
  259. })
  260. },500)
  261. })
  262. }
  263. uni.onBLEConnectionStateChange(async res => {
  264. log(res)
  265. // {"deviceId":"EC:22:05:13:78:49","connected":true}
  266. if (res.connected) {
  267. console.log("connected successfully")
  268. const servicesResult = await getBLEDeviceServices()
  269. console.log(servicesResult)
  270. if (!servicesResult.ok) {
  271. ecBLEConnectionStateChangeCallback(servicesResult)
  272. closeBLEConnection()
  273. return
  274. }
  275. for (const service of servicesResult.services) {
  276. var isRightService = false;
  277. if ((service.uuid.toUpperCase() === ecGattServerUUIDOption1) ||
  278. (service.uuid.toUpperCase() === ecGattServerUUIDOption2)) {
  279. ecGattServerUUID = service.uuid
  280. isRightService = true;
  281. }
  282. if ((service.uuid.toUpperCase() === yiyuanUUIDOption1) ||
  283. (service.uuid.toUpperCase() === yiyuanUUIDOption2)) {
  284. ecGattServerUUID = service.uuid
  285. isRightService = true;
  286. }
  287. if(!isRightService){
  288. continue;
  289. }
  290. console.log("uuid:"+ecGattServerUUID)
  291. const characteristicsResult = await getBLEDeviceCharacteristics(
  292. service.uuid
  293. )
  294. if (!characteristicsResult.ok) {
  295. ecBLEConnectionStateChangeCallback(characteristicsResult)
  296. closeBLEConnection()
  297. return
  298. }
  299. console.log(characteristicsResult)
  300. for (const characteristic of characteristicsResult.characteristics) {
  301. if (
  302. characteristic.properties &&
  303. characteristic.properties.notify
  304. ) {
  305. const notifyResult =
  306. await notifyBLECharacteristicValueChange(
  307. service.uuid,
  308. characteristic.uuid
  309. )
  310. if (!notifyResult.ok) {
  311. ecBLEConnectionStateChangeCallback({
  312. ok: false,
  313. errCode: 30000,
  314. errMsg: 'notify error',
  315. })
  316. closeBLEConnection()
  317. return
  318. }
  319. }
  320. /**
  321. * 14:49:25.419 uuid:0000FFF0-0000-1000-8000-00805F9B34FB at utils/ecBLE/ecBLEApp.js:298
  322. * 14:49:25.427 characteristic.uuid:0000FFF2-0000-1000-8000-00805F9B34FB at utils/ecBLE/ecBLEApp.js:327
  323. * 14:49:25.443 characteristic.uuid:0000FFF1-0000-1000-8000-00805F9B34FB at utils/ecBLE/ecBLEApp.js:327
  324. */
  325. console.log("characteristic.uuid:"+characteristic.uuid)
  326. if ((characteristic.uuid.toUpperCase() ===
  327. ecGattCharacteristicWriteUUIDOption1) ||
  328. (characteristic.uuid.toUpperCase() ===
  329. ecGattCharacteristicWriteUUIDOption2)) {
  330. ecGattCharacteristicWriteUUID = characteristic.uuid
  331. }
  332. if ((characteristic.uuid.toUpperCase() ===
  333. yiyuancharacterID1) ||
  334. (characteristic.uuid.toUpperCase() ===
  335. yiyuancharacterID2)) {
  336. ecGattCharacteristicWriteUUID = characteristic.uuid
  337. }
  338. console.log("rghtCID:"+ecGattCharacteristicWriteUUID);
  339. }
  340. }
  341. if (isAndroid) {
  342. await setBLEMTU(247)
  343. }
  344. ecBLEConnectionStateChangeCallback({
  345. ok: true,
  346. errCode: 0,
  347. errMsg: '',
  348. })
  349. } else {
  350. ecBLEConnectionStateChangeCallback({
  351. ok: false,
  352. errCode: 0,
  353. errMsg: 'disconnect',
  354. })
  355. }
  356. })
  357. const createBLEConnection = async id => {
  358. ecDeviceId = id
  359. const res = await _createBLEConnection()
  360. if (!res.ok) {
  361. ecBLEConnectionStateChangeCallback(res)
  362. }
  363. }
  364. const closeBLEConnection = () => {
  365. uni.closeBLEConnection({
  366. deviceId: ecDeviceId,
  367. complete(res) {
  368. log(res)
  369. },
  370. })
  371. }
  372. uni.onBLECharacteristicValueChange(res => {
  373. log(res)
  374. let x = new Uint8Array(res.value)
  375. log(x)
  376. let str = utf8BytesToStr(x)
  377. let strHex = ''
  378. for (let i = 0; i < x.length; i++) {
  379. strHex = strHex + x[i].toString(16).padStart(2, '0').toUpperCase()
  380. }
  381. log(str)
  382. log(strHex)
  383. ecBLECharacteristicValueChangeCallback(str, strHex)
  384. })
  385. const onBLECharacteristicValueChange = cb => {
  386. ecBLECharacteristicValueChangeCallback = cb
  387. }
  388. const _writeBLECharacteristicValue = buffer => {
  389. return new Promise(function(resolve, reject) {
  390. uni.writeBLECharacteristicValue({
  391. deviceId: ecDeviceId,
  392. serviceId: ecGattServerUUID,
  393. characteristicId: ecGattCharacteristicWriteUUID,
  394. value: buffer,
  395. writeType: 'writeNoResponse',
  396. success(res) {
  397. log(res)
  398. resolve({
  399. ok: true,
  400. errCode: 0,
  401. errMsg: ''
  402. })
  403. },
  404. fail(res) {
  405. log(res)
  406. resolve({
  407. ok: false,
  408. errCode: res.code,
  409. errMsg: res.errMsg
  410. })
  411. },
  412. })
  413. })
  414. }
  415. const writeBLECharacteristicValue = async (str, isHex) => {
  416. if (str.length === 0)
  417. return {
  418. ok: false,
  419. errCode: 30000,
  420. errMsg: 'data is null'
  421. }
  422. let buffer
  423. if (isHex) {
  424. buffer = new ArrayBuffer(str.length / 2)
  425. let x = new Uint8Array(buffer)
  426. for (let i = 0; i < x.length; i++) {
  427. x[i] = parseInt(str.substr(2 * i, 2), 16)
  428. }
  429. } else {
  430. buffer = new Uint8Array(strToUtf8Bytes(str)).buffer
  431. }
  432. return await _writeBLECharacteristicValue(buffer)
  433. }
  434. const utf8BytesToStr = utf8Bytes => {
  435. let unicodeStr = ''
  436. for (let pos = 0; pos < utf8Bytes.length;) {
  437. let flag = utf8Bytes[pos]
  438. let unicode = 0
  439. if (flag >>> 7 === 0) {
  440. unicodeStr += String.fromCharCode(utf8Bytes[pos])
  441. pos += 1
  442. }
  443. // else if ((flag & 0xFC) === 0xFC) {
  444. // unicode = (utf8Bytes[pos] & 0x3) << 30
  445. // unicode |= (utf8Bytes[pos + 1] & 0x3F) << 24
  446. // unicode |= (utf8Bytes[pos + 2] & 0x3F) << 18
  447. // unicode |= (utf8Bytes[pos + 3] & 0x3F) << 12
  448. // unicode |= (utf8Bytes[pos + 4] & 0x3F) << 6
  449. // unicode |= (utf8Bytes[pos + 5] & 0x3F)
  450. // unicodeStr += String.fromCharCode(unicode)
  451. // pos += 6
  452. // }
  453. // else if ((flag & 0xF8) === 0xF8) {
  454. // unicode = (utf8Bytes[pos] & 0x7) << 24
  455. // unicode |= (utf8Bytes[pos + 1] & 0x3F) << 18
  456. // unicode |= (utf8Bytes[pos + 2] & 0x3F) << 12
  457. // unicode |= (utf8Bytes[pos + 3] & 0x3F) << 6
  458. // unicode |= (utf8Bytes[pos + 4] & 0x3F)
  459. // unicodeStr += String.fromCharCode(unicode)
  460. // pos += 5
  461. // }
  462. else if ((flag & 0xf0) === 0xf0) {
  463. unicode = (utf8Bytes[pos] & 0xf) << 18
  464. unicode |= (utf8Bytes[pos + 1] & 0x3f) << 12
  465. unicode |= (utf8Bytes[pos + 2] & 0x3f) << 6
  466. unicode |= utf8Bytes[pos + 3] & 0x3f
  467. unicodeStr += String.fromCharCode(unicode)
  468. pos += 4
  469. } else if ((flag & 0xe0) === 0xe0) {
  470. unicode = (utf8Bytes[pos] & 0x1f) << 12
  471. unicode |= (utf8Bytes[pos + 1] & 0x3f) << 6
  472. unicode |= utf8Bytes[pos + 2] & 0x3f
  473. unicodeStr += String.fromCharCode(unicode)
  474. pos += 3
  475. } else if ((flag & 0xc0) === 0xc0) {
  476. //110
  477. unicode = (utf8Bytes[pos] & 0x3f) << 6
  478. unicode |= utf8Bytes[pos + 1] & 0x3f
  479. unicodeStr += String.fromCharCode(unicode)
  480. pos += 2
  481. } else {
  482. unicodeStr += String.fromCharCode(utf8Bytes[pos])
  483. pos += 1
  484. }
  485. }
  486. return unicodeStr
  487. }
  488. const strToUtf8Bytes = str => {
  489. let bytes = []
  490. for (let i = 0; i < str.length; ++i) {
  491. let code = str.charCodeAt(i)
  492. if (code >= 0x10000 && code <= 0x10ffff) {
  493. bytes.push((code >> 18) | 0xf0) // 第一个字节
  494. bytes.push(((code >> 12) & 0x3f) | 0x80)
  495. bytes.push(((code >> 6) & 0x3f) | 0x80)
  496. bytes.push((code & 0x3f) | 0x80)
  497. } else if (code >= 0x800 && code <= 0xffff) {
  498. bytes.push((code >> 12) | 0xe0)
  499. bytes.push(((code >> 6) & 0x3f) | 0x80)
  500. bytes.push((code & 0x3f) | 0x80)
  501. } else if (code >= 0x80 && code <= 0x7ff) {
  502. bytes.push((code >> 6) | 0xc0)
  503. bytes.push((code & 0x3f) | 0x80)
  504. } else {
  505. bytes.push(code)
  506. }
  507. }
  508. return bytes
  509. }
  510. export default {
  511. onBluetoothAdapterStateChange,
  512. openBluetoothAdapter,
  513. onBluetoothDeviceFound,
  514. startBluetoothDevicesDiscovery,
  515. stopBluetoothDevicesDiscovery,
  516. onBLEConnectionStateChange,
  517. createBLEConnection,
  518. closeBLEConnection,
  519. onBLECharacteristicValueChange,
  520. writeBLECharacteristicValue,
  521. }