blueTouth.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 ioNew = null
  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. io.sockets.emit('blueConnect', 'success');
  48. console.log('start');
  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. var obj = JSON.parse( Utf8ArrayToStr(new Uint8Array(data)))
  65. //console.log(withoutResponse)
  66. callBack()
  67. },
  68. onReadRequest: function (offset, callBack) {
  69. var result = characteristic.RESULT_SUCCESS;
  70. //var data = new Buffer('321');
  71. sendMessageHandle = callBack
  72. }
  73. });
  74. messageObj = characteristic
  75. var PrimaryService = bleno.PrimaryService;
  76. var primaryService = new PrimaryService({
  77. uuid: 'fffffffffffffffffffffffffffffff0', // or 'fff0' for 16-bit
  78. characteristics: [
  79. // see Characteristic for data type
  80. characteristic
  81. ]
  82. });
  83. bleno.setServices(primaryService, function (error) {
  84. if (error) {
  85. console.log(error);
  86. }
  87. console.log('success');
  88. });
  89. } else {
  90. }
  91. });
  92. bleno.on('disconnect',function (clientAddress) {
  93. console.log('disconnectAddress')
  94. })
  95. bleno.on('accept',function (clientAddress) {
  96. console.log('connectAddress')
  97. })
  98. bleno.on('advertisingStartError', function(error) {
  99. console.log(error)
  100. });
  101. }catch (err){
  102. // 判断用户是否开启蓝牙 或者是堵塞
  103. if(err.message == 'No compatible USB Bluetooth 4.0 device found!') {
  104. // 没有插设备
  105. }
  106. console.log(err.message)
  107. }
  108. var sendMessageHandle = null
  109. var messageObj = null
  110. module.exports = {
  111. sendData:function (buffer) {
  112. if(sendMessageHandle) {
  113. var result =messageObj.RESULT_SUCCESS;
  114. var data = new Buffer('woshi zhangxiaoyu');
  115. sendMessageHandle(result,data)
  116. }
  117. },
  118. setIo:function (data) {
  119. ioNew = data
  120. }
  121. }