ble.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. const handle = require("./handle");
  2. const {buf2hex, hex2ab} = require("./buffer");
  3. // 需要匹配的相机特征值列表
  4. const CAMERA_MANUFACTURER_LOOKUP = ["2d010300"];
  5. class BLE{
  6. constructor(){
  7. // this代表实例对象
  8. this.isInit= false;
  9.  }
  10. async initBle(option){
  11. let [err,ok] = await handle(wx.openBluetoothAdapter(option));
  12. if(err){
  13. throw err;
  14. }
  15. return 0
  16. }
  17. search(onSearchCallback){
  18. // wx.onBluetoothDeviceFound((res) => {
  19. // res.devices.forEach((device) => {
  20. // // 这里可以做一些过滤
  21. // console.log('Device Found', device)
  22. // })
  23. // // 找到要搜索的设备后,及时停止扫描
  24. // wx.stopBluetoothDevicesDiscovery()
  25. // })
  26. console.log("搜索蓝牙中:");
  27. setTimeout(() => {
  28. console.log("test");
  29. }, 2500);
  30. wx.onBluetoothDeviceFound(function (res) {
  31. var bleArray = res.devices;
  32. //这里会收到周边搜索到的蓝牙
  33. console.log("\n\nfind devices ----");
  34. // console.log(res);
  35. // console.log(res.devices);
  36. // 对
  37. for (let index = 0; index < bleArray.length; index++) {
  38. const ble = bleArray[index];
  39. if(ble.advertisData){
  40. // 解析到特征值设备
  41. let advertisData = buf2hex(ble.advertisData);
  42. console.log(advertisData);
  43. console.log(ble);
  44. if(CAMERA_MANUFACTURER_LOOKUP.find(codeStr=>advertisData.startsWith(codeStr))){
  45. console.log('找到匹配到的设备');
  46. // 找到相机
  47. onSearchCallback({
  48. ...ble,
  49. hexAdvertisData: advertisData
  50. })
  51. }
  52. }
  53. }
  54. });
  55. wx.startBluetoothDevicesDiscovery({
  56. success(res) {
  57. console.log("开始搜索蓝牙:", res)
  58. },
  59. fail(res) {
  60. console.log(res)
  61. }
  62. })
  63. }
  64. onSearch(){
  65. console.log('on serarch');
  66. }
  67. async stopSearch(){
  68. let [err,res] = await handle(wx.stopBluetoothDevicesDiscovery());
  69. if(err){
  70. throw err;
  71. }
  72. return res;
  73. }
  74. connectDev(deviceId){
  75. return new Promise((resolve,reject)=>{
  76. console.log(`连接到:${deviceId}`);
  77. wx.createBLEConnection({
  78. deviceId:deviceId,
  79. success:function(res){
  80. resolve(res);
  81. },
  82. fail:function(err){
  83. console.error(err);
  84. reject(err);
  85. }
  86. })
  87. })
  88. }
  89. // 获取蓝牙服务
  90. getBleServices(devId){
  91. let self = this;
  92. return new Promise((resolve,reject)=>{
  93. wx.getBLEDeviceServices({
  94. //蓝牙设备ID
  95. deviceId: devId,
  96. success: async function (res) {
  97. let services = res.services;
  98. console.log(services)
  99. for (let i = 0; i < services.length; i++) {
  100. console.log(services[i].uuid);
  101. let [err,res] = await handle(self.getServerCharacteristics(devId,services[i].uuid));
  102. if(err){
  103. console.log(`获取服务特征值失败 服务id:${services[i].uuid}`);
  104. return reject(err);
  105. }
  106. services[i].characteristics = res;
  107. }
  108. console.log('--------');
  109. resolve(services);
  110. //获取蓝牙设备服务UUID成功
  111. },
  112. fail(res) {
  113. //获取蓝牙设备服务失败
  114. reject(res);
  115. }
  116. })
  117. })
  118. }
  119. // 获取蓝牙服务特征值
  120. getServerCharacteristics(devId,bleServiceUUID){
  121. let self = this;
  122. return new Promise((resolve,reject)=>{
  123. wx.getBLEDeviceCharacteristics({
  124. //蓝牙设备ID
  125. deviceId: devId,
  126. //蓝牙服务ID
  127. serviceId: bleServiceUUID,
  128. success: function (res) {
  129. console.log(res);
  130. res.characteristics.forEach(async c=>{
  131. // console.log(c);
  132. if(c.properties.notify){
  133. console.log(`监听${bleServiceUUID}的${c.uuid.substr(0,8)}`);
  134. let [err,res] = await handle(self.listenData(devId,bleServiceUUID,c.uuid));
  135. if(err){
  136. console.error(err);
  137. console.log(`添加监听${bleServiceUUID}的${c.uuid.substr(0,8)}失败`);
  138. }
  139. console.log(res);
  140. }
  141. })
  142. resolve(res.characteristics);
  143. },
  144. fail(err){
  145. reject(err);
  146. }
  147. });
  148. });
  149. }
  150. // 向指定特征发送数据
  151. sendData(devId,serviceId,characteristicId,hexStr){
  152. console.log(hexStr);
  153. hexStr = hex2ab(hexStr);
  154. console.log(hexStr);
  155. return new Promise((resolve,reject)=>{
  156. wx.writeBLECharacteristicValue({
  157. //蓝牙设备ID
  158. deviceId: devId,
  159. //蓝牙服务ID
  160. serviceId: serviceId,
  161. //写特征值ID
  162. characteristicId: characteristicId,
  163. //数据ArrayBuffer
  164. value: hexStr,
  165. success(res) {
  166. //发送蓝牙数据成功
  167. resolve(res);
  168. },
  169. fail(res) {
  170. //发送蓝牙数据失败
  171. reject(res);
  172. }
  173. })
  174. })
  175. }
  176. // 监听数据
  177. listenData(devId,serviceId,uuid){
  178. return new Promise((resolve,reject)=>{
  179. wx.notifyBLECharacteristicValueChange({
  180. state: true,
  181. //蓝牙设备ID
  182. deviceId: devId,
  183. //蓝牙服务ID
  184. serviceId: serviceId,
  185. //特征值ID
  186. characteristicId: uuid,
  187. success: function (res) {
  188. //开启通知成功
  189. wx.onBLECharacteristicValueChange(function (res) {
  190. //这里坐等数据过来,res.value
  191. console.log('got data ');
  192. console.log(res);
  193. console.log(buf2hex(res.value));
  194. console.log('got data ');
  195. })
  196. resolve(res);
  197. },
  198. fail: function (res) {
  199. //开启通知失败
  200. reject(res);
  201. }
  202. });
  203. })
  204. }
  205. //
  206. }
  207. module.exports = ()=> {
  208. return new BLE();
  209. }