tools.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import errToString from "./error";
  2. import {logs} from '../util'
  3. import {promisify} from '../uniFunction'
  4. import {ab2hex} from "@/utils/ble/hex";
  5. export const _onBluetoothAdapterStateChange = (callback) => {
  6. /* #ifndef H5 */
  7. uni.onBluetoothAdapterStateChange(res => {
  8. // 处理蓝牙开启与关闭问题
  9. callback(res)
  10. })
  11. /* #endif */
  12. }
  13. export const _onBLEConnectionStateChange = (callback) => {
  14. /* #ifndef H5 */
  15. uni.onBLEConnectionStateChange(res => {
  16. // 该方法回调中可以用于处理连接意外断开等异常情况
  17. callback(res)
  18. })
  19. /* #endif */
  20. }
  21. export const _onBLECharacteristicValueChange = (callback) => {
  22. /* #ifndef H5 */
  23. uni.onBLECharacteristicValueChange(res => {
  24. res.str = ab2hex(res.value)
  25. callback(res)
  26. })
  27. /* #endif */
  28. }
  29. export const _openAdapter = () => {
  30. logs(`准备初始化蓝牙适配器...`);
  31. return promisify(uni.openBluetoothAdapter).then((res) => {
  32. logs(`✔ 适配器初始化成功!`, res);
  33. return res;
  34. }, (err) => {
  35. throw `初始化失败!${errToString(err)}`
  36. })
  37. }
  38. /**
  39. * @param data
  40. */
  41. export const _startSearch = (data = {}) => {
  42. logs(`准备搜寻附近的蓝牙外围设备...`);
  43. /* #ifndef H5 */
  44. return promisify(uni.startBluetoothDevicesDiscovery, Object.assign({
  45. interval: 10
  46. }, data)).then(
  47. (res) => {
  48. logs(`✔ 搜索成功!`, res);
  49. return res
  50. },
  51. (err) => {
  52. throw `搜索蓝牙失败!${errToString(err)}`
  53. }
  54. );
  55. /* #endif */
  56. }
  57. /**
  58. * 停止蓝牙搜索
  59. * @returns {PromiseLike<[null, any] | [(string|string), null]> | Promise<[null, any] | [(string|string), null]>}
  60. * @private
  61. */
  62. export const _stopSearchBluetooth = () => {
  63. logs(`停止查找新设备...`);
  64. /* #ifdef H5 */
  65. throw `停止查询失败!H5不能操作`
  66. /* #endif */
  67. return uni.stopBluetoothDevicesDiscovery().then(
  68. (res) => {
  69. logs(`✔ 停止查找设备成功!`);
  70. return res
  71. },
  72. (err) => {
  73. throw `停止查询失败!${errToString(err)}`
  74. }
  75. );
  76. }
  77. /**
  78. * 连接设备
  79. * @param deviceId
  80. * @returns {Promise<[null, unknown] | [null, any] | [(string|string), null]>}
  81. * @private
  82. */
  83. export const _connectBlue = (deviceId) => {
  84. logs(`准备连接设备...`);
  85. /* #ifndef H5 */
  86. return promisify(uni.createBLEConnection, {
  87. deviceId,
  88. }).then(
  89. (res) => {
  90. logs(`✔ 连接蓝牙成功!`);
  91. return true
  92. },
  93. (err) => {
  94. if (err.errCode == 10010) {
  95. logs(`✔ 连接蓝牙成功!`);
  96. return true
  97. } else {
  98. throw `连接蓝牙失败! ${errToString(err)}`
  99. }
  100. }
  101. );
  102. /* #endif */
  103. }
  104. /**
  105. * 关闭设备蓝牙
  106. * @param deviceId
  107. * @returns {Promise<[null, unknown] | [(string|string), null]>}
  108. * @private
  109. */
  110. export const _closeBLEConnection = (deviceId) => {
  111. logs(`断开蓝牙连接...`)
  112. /* #ifndef H5 */
  113. return promisify(uni.closeBLEConnection, {
  114. deviceId,
  115. }).then(
  116. (res) => {
  117. logs(`✔ 断开蓝牙成功!`);
  118. return true
  119. },
  120. (err) => {
  121. throw `断开蓝牙连接失败! ${errToString(err)}`
  122. }
  123. );
  124. /* #endif */
  125. }
  126. /**
  127. * 关闭蓝牙
  128. * @returns {Promise<[null, unknown] | [(string|string), null]>}
  129. * @private
  130. */
  131. export const _closeBLEAdapter = () => {
  132. logs(`释放蓝牙适配器...`)
  133. return promisify(uni.closeBluetoothAdapter).then(
  134. (res) => {
  135. logs(`✔ 释放适配器成功!`)
  136. return res
  137. },
  138. (err) => {
  139. throw `释放适配器失败! ${errToString(err)}`
  140. }
  141. );
  142. }
  143. /**
  144. * 获取设备服务列表
  145. * @param deviceId
  146. * @returns {Promise<[null, unknown] | [(string|string), null]>}
  147. * @private
  148. */
  149. export const _getBLEServices = (deviceId) => {
  150. logs(`获取蓝牙设备所有服务...`)
  151. return promisify(uni.getBLEDeviceServices, {
  152. deviceId
  153. }).then(res => {
  154. logs(`✔ 获取service成功!`)
  155. return res
  156. }, err => {
  157. throw `获取service失败! ${errToString(err)}`
  158. })
  159. }
  160. /**
  161. * 获取服务通道
  162. * @param deviceId
  163. * @param serviceId
  164. * @returns {Promise<[null, unknown] | [(string|string), null]>}
  165. * @private
  166. */
  167. export const _getCharacteristics = (deviceId, serviceId) => {
  168. let msg = null
  169. if (!deviceId) msg = `服务通道错误!deviceId为空`
  170. if (!serviceId) msg = `服务通道错误!serviceId为空`
  171. if (msg != null) {
  172. console.error(`获取特征失败,${msg}`)
  173. throw msg
  174. }
  175. logs(`开始获取特征值...`);
  176. return promisify(uni.getBLEDeviceCharacteristics, {
  177. deviceId,
  178. serviceId,
  179. }).then(
  180. (res) => {
  181. logs(`✔ 获取特征值成功!`);
  182. return res
  183. },
  184. (err) => {
  185. throw `获取特征值失败! ${errToString(err)}`
  186. }
  187. );
  188. }
  189. // 订阅特征值
  190. export const _notifyBLECharacteristicValueChange = (deviceId, serviceId, characteristicId) => {
  191. return promisify(uni.notifyBLECharacteristicValueChange, {
  192. deviceId,
  193. serviceId,
  194. characteristicId,
  195. state: true
  196. }).then(res => {
  197. logs(`✔ 订阅notify成功!`)
  198. return true
  199. }, err => {
  200. throw `订阅notify失败! ${errToString(err)}`
  201. })
  202. }
  203. export const _writeBLECharacteristicValue = (deviceId, serviceId, characteristicId, buffer) => {
  204. return promisify(uni.writeBLECharacteristicValue, {
  205. deviceId,
  206. serviceId,
  207. characteristicId,
  208. value: buffer,
  209. }).then(res => {
  210. return res
  211. }, err => {
  212. throw {
  213. code: err.errCode,
  214. msg: `写入数据失败! ${errToString(err)}`
  215. }
  216. })
  217. }