nobleWrapper.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. const resp = require('../util/resp')
  2. const noble = require('noble');
  3. let inRange = {};
  4. let blueDeviceList = []
  5. let blueCharacteristics = []
  6. let connectedCharacteristic = null;
  7. let on_data_callback = null;
  8. noble.on('discover', function (peripheral) {
  9. if(peripheral.addressType != 'public') {
  10. return;
  11. }
  12. inRange[peripheral.id] = {
  13. peripheral: peripheral
  14. };
  15. var blueDevice = {
  16. 'id': peripheral.id,
  17. 'localName': peripheral.advertisement.localName,
  18. 'serviceUuids': JSON.stringify(peripheral.advertisement.serviceUuids),
  19. 'address': peripheral.address,
  20. 'addressType': peripheral.addressType,
  21. 'connectable': peripheral.connectable,
  22. 'rssi': peripheral.rssi,
  23. }
  24. blueDeviceList.push(blueDevice)
  25. });
  26. function onServicesAndCharacteristicsDiscovered(error, services, characteristics) {
  27. if(connectedCharacteristic ==null) {
  28. console.log('Discovered services and characteristics');
  29. const echoCharacteristic = characteristics[0];
  30. // data callback receives notifications
  31. echoCharacteristic.on('data', (data, isNotification) => {
  32. if (on_data_callback) {
  33. on_data_callback(data)
  34. }
  35. });
  36. // subscribe to be notified whenever the peripheral update the characteristic
  37. echoCharacteristic.subscribe(error => {
  38. if (error) {
  39. console.error('Error subscribing to echoCharacteristic');
  40. } else {
  41. console.log('Subscribed for echoCharacteristic notifications');
  42. }
  43. });
  44. connectedCharacteristic = echoCharacteristic
  45. }
  46. }
  47. module.exports = {
  48. scan: function (resp_callback) {
  49. try {
  50. blueDeviceList = []
  51. blueCharacteristics = []
  52. setTimeout(() => {
  53. noble.stopScanning()
  54. resp_callback(resp.ok_resp(blueDeviceList))
  55. }, 3000);
  56. noble.startScanning()
  57. } catch (e) {
  58. resp_callback(resp.fail_resp(e.message))
  59. }
  60. },
  61. // 连接蓝牙设备
  62. connectDevice:function (args,callBack) {
  63. let peripheral = inRange[args.id].peripheral
  64. on_data_callback = args.on_data_cb;
  65. peripheral.connect(error => {
  66. if(error == null) {
  67. callBack(resp.ok_resp('success'))
  68. } else {
  69. callBack(resp.fail_resp('error'))
  70. }
  71. })
  72. peripheral.on('disconnect', () => {
  73. console.log('disconnect')
  74. // callBack(resp.fail_resp('connectError'))
  75. });
  76. },
  77. // 断开蓝牙设备
  78. disConnectDevice:function (id,callBack) {
  79. let peripheral = inRange[id].peripheral
  80. peripheral.disconnect(error=>{
  81. if(error == null) {
  82. callBack(resp.ok_resp('success'))
  83. } else {
  84. callBack(resp.fail_resp('error'))
  85. }
  86. })
  87. },
  88. getCharList:function (id,callBack) {
  89. let peripheral = inRange[id].peripheral
  90. peripheral.discoverServices([], function (error, services) {
  91. for (let serviceIndex = 0; serviceIndex < services.length; serviceIndex++) {
  92. (function (service) {
  93. service.discoverCharacteristics([], function (error, characteristics) {
  94. for (let charIndex = 0; charIndex < characteristics.length; charIndex++) {
  95. let c = characteristics[charIndex];
  96. let info = {
  97. id: peripheral.id,
  98. uuid: service.uuid,
  99. name: service.name,
  100. characteristic: {
  101. 'uuid': c.uuid,
  102. 'properties': c.properties
  103. }
  104. }
  105. callBack(info)
  106. }
  107. })
  108. })(services[serviceIndex])
  109. }
  110. })
  111. },
  112. setUp:function (row) {
  113. const serviceUUIDs = [row.uuid];
  114. const characteristicUUIDs = [row.characteristic.uuid];
  115. let peripheral = inRange[row.id].peripheral
  116. peripheral.discoverSomeServicesAndCharacteristics(
  117. serviceUUIDs,
  118. characteristicUUIDs,
  119. onServicesAndCharacteristicsDiscovered
  120. );
  121. },
  122. disSetUp:function () {
  123. if(connectedCharacteristic != null) {
  124. connectedCharacteristic.unsubscribe(error =>{
  125. if(error ==null) {
  126. console.log('unsubscribe,success!')
  127. connectedCharacteristic = null
  128. }
  129. })
  130. }
  131. },
  132. write: function (msg, resp_callback) {
  133. try {
  134. connectedCharacteristic.write(msg);
  135. } catch (e) {
  136. resp_callback(resp.fail_resp(e.message))
  137. }
  138. }
  139. }