blueTouth.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. var bleno = require('bleno');
  2. var serviceUuids = ['fffffffffffffffffffffffffffffff0']
  3. function Utf8ArrayToStr(array) {
  4. var out, i, len, c;
  5. var char2, char3;
  6. out = "";
  7. len = array.length;
  8. i = 0;
  9. while(i < len) {
  10. c = array[i++];
  11. switch(c >> 4)
  12. {
  13. case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  14. // 0xxxxxxx
  15. out += String.fromCharCode(c);
  16. break;
  17. case 12: case 13:
  18. // 110x xxxx 10xx xxxx
  19. char2 = array[i++];
  20. out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
  21. break;
  22. case 14:
  23. // 1110 xxxx 10xx xxxx 10xx xxxx
  24. char2 = array[i++];
  25. char3 = array[i++];
  26. out += String.fromCharCode(((c & 0x0F) << 12) |
  27. ((char2 & 0x3F) << 6) |
  28. ((char3 & 0x3F) << 0));
  29. break;
  30. }
  31. }
  32. return out;
  33. }
  34. var blueServerStatus = ''
  35. try {
  36. bleno.on('stateChange', function(state) {
  37. console.log('on -> stateChange: ' + state);
  38. if (state === 'poweredOn') {
  39. bleno.startAdvertising('semsx', serviceUuids);
  40. } else {
  41. bleno.stopAdvertising();
  42. }
  43. });
  44. bleno.on('advertisingStart', function(error) {
  45. console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success'));
  46. if (!error) {
  47. console.log('start');
  48. blueServerStatus = 'deviceSuccess'
  49. var Descriptor = bleno.Descriptor;
  50. var descriptor = new Descriptor({
  51. uuid: '212112',
  52. value: '123123' // static value, must be of type Buffer or string if set
  53. });
  54. var Characteristic = bleno.Characteristic;
  55. var characteristic = new Characteristic({
  56. uuid: 'fffffffffffffffffffffffffffffff1', // or 'fff1' for 16-bit
  57. properties: ['read', 'write','notify','writeWithoutResponse'], // can be a combination of 'read', 'write', 'writeWithoutResponse', 'notify', 'indicate'
  58. value: null, // optional static value, must be of type Buffer - for read only characteristics
  59. descriptors: [
  60. // see Descriptor for data type
  61. descriptor
  62. ],
  63. onWriteRequest: function (data, offset, withoutResponse,callBack) {
  64. ioNew.sockets.emit('sendBlueData',data);
  65. //var obj = JSON.parse( Utf8ArrayToStr(new Uint8Array(data)))
  66. //console.log(withoutResponse)
  67. callBack()
  68. },
  69. onReadRequest: function (offset, callBack) {
  70. var result = characteristic.RESULT_SUCCESS;
  71. var data = new Buffer('connectTest');
  72. callBack(result,data)
  73. sendMessageHandle = callBack
  74. }
  75. });
  76. messageObj = characteristic
  77. var PrimaryService = bleno.PrimaryService;
  78. var primaryService = new PrimaryService({
  79. uuid: 'fffffffffffffffffffffffffffffff0', // or 'fff0' for 16-bit
  80. characteristics: [
  81. // see Characteristic for data type
  82. characteristic
  83. ]
  84. });
  85. bleno.setServices(primaryService, function (error) {
  86. if (error) {
  87. console.log(error);
  88. }
  89. });
  90. } else {
  91. }
  92. });
  93. bleno.on('disconnect',function (clientAddress) {
  94. ioNew.sockets.emit('setBlueToothDevice','');
  95. ioNew.sockets.emit('sendBlueData','');
  96. })
  97. bleno.on('accept',function (clientAddress) {
  98. ioNew.sockets.emit('setBlueToothDevice',clientAddress);
  99. })
  100. bleno.on('advertisingStartError', function(error) {
  101. console.log(error)
  102. });
  103. }catch (err){
  104. // 判断用户是否开启蓝牙 或者是堵塞
  105. if(err.message == 'No compatible USB Bluetooth 4.0 device found!') {
  106. // 没有插蓝牙适配器
  107. blueServerStatus = 'noUsb'
  108. } else {
  109. // 驱动错误
  110. blueServerStatus = 'deviceError'
  111. }
  112. }
  113. var sendMessageHandle = null
  114. var messageObj = null
  115. var ioNew = null
  116. module.exports = {
  117. // 蓝牙 传输数据
  118. sendData:function (buffer) {
  119. if(sendMessageHandle) {
  120. var result =messageObj.RESULT_SUCCESS;
  121. sendMessageHandle(result,buffer)
  122. }
  123. },
  124. getBlueServerStatus:function () {
  125. return blueServerStatus
  126. },
  127. setIo:function(data) {
  128. ioNew = data
  129. }
  130. }