Advertisement
Guest User

Nissei bluetooth

a guest
Dec 6th, 2019
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.80 KB | None | 0 0
  1. //var noble = require('noble');
  2. var noble = require('../../node_modules/noble/index');
  3.  
  4. // UUID
  5. var UUID_BLESERIAL_SERVICE = '1ef19620-a803-4af0-ae95-4b4b0aa26f29'.replace(/\-/g, '');
  6. var UUID_BLESERIAL_RX = '1ef19621-a803-4af0-ae95-4b4b0aa26f29'.replace(/\-/g, '');
  7. var UUID_BLESERIAL_TX = '1ef19622-a803-4af0-ae95-4b4b0aa26f29'.replace(/\-/g, '');
  8. // Command
  9. var buf_cnkey = new Buffer([0xE7,0xFD,0x33,0x35,0x36,0x30,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xB6]);
  10. var buf_E7620101 = new Buffer([0xE7,0x62,0x01,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]);
  11.  
  12. // 各CHARACTERISTICSをUUIDから判断してcharacteristic格納する変数
  13. var CHARACTERISTICS_BLESERIAL_SERVICE;
  14. var CHARACTERISTICS_BLESERIAL_RX;
  15. var CHARACTERISTICS_BLESERIAL_TX;
  16. //
  17. var str1,str2,str3,str4;
  18.  
  19. // LED ON/OFF
  20. //var led_toggle = false;
  21.  
  22. // 状態がパワーONだったらスキャンに移行
  23. noble.on('stateChange', function(state) {
  24. console.log('on -> stateChange: ' + state);
  25.  
  26. if (state === 'poweredOn') {
  27. noble.startScanning();
  28. } else {
  29. noble.stopScanning();
  30. }
  31. });
  32.  
  33. noble.on('scanStart', function() {
  34. console.log('on -> scanStart');
  35. });
  36.  
  37. noble.on('scanStop', function() {
  38. console.log('on -> scanStop');
  39. });
  40.  
  41. // discover 機器が発見されたら
  42. noble.on('discover', function(peripheral) {
  43. console.log('on -> discover: ' + peripheral);
  44. // まずスキャンをとめる
  45. noble.stopScanning();
  46.  
  47. // 接続時のイベント
  48. peripheral.on('connect', function() {
  49. console.log('on -> connect');
  50. this.discoverServices();
  51. });
  52. // 切断時のイベント
  53. peripheral.on('disconnect', function() {
  54. console.log('on -> disconnect');
  55. });
  56.  
  57. // 見つけたサービス(機器)へのアクセス
  58. peripheral.on('servicesDiscover', function(services) {
  59. for(i = 0; i < services.length; i++) {
  60. // サービスがUUID_BLESERIAL_SERVICEと一致した時だけ処理
  61. if(services[i]['uuid'] == UUID_BLESERIAL_SERVICE){
  62. // サービスのcharacteristic捜索
  63. services[i].on('includedServicesDiscover', function(includedServiceUuids) {
  64. console.log('on -> service included services discovered [' + includedServiceUuids + ']');
  65. this.discoverCharacteristics();
  66. });
  67. // characteristic取得イベント
  68. services[i].on('characteristicsDiscover', function(characteristics) {
  69. // characteristics配列から必要なCHARACTERISTICSをUUIDから判断してcharacteristic格納
  70. for(j = 0; j < characteristics.length; j++) {
  71. // 入力 characteristic
  72. if( UUID_BLESERIAL_RX == characteristics[j].uuid ){
  73. console.log("CHARACTERISTICS_BLESERIAL_RX exist!!");
  74. CHARACTERISTICS_BLESERIAL_RX = characteristics[j];
  75. }
  76. // 出力 characteristic
  77. // デジタル入出力を担当するPIO全部に出力するようにお願いする
  78. if( UUID_BLESERIAL_TX == characteristics[j].uuid ) {
  79. console.log("CHARACTERISTICS_BLESERIAL_TX exist!!");
  80. CHARACTERISTICS_BLESERIAL_TX = characteristics[j];
  81. // CNKEY
  82. str1=peripheral.id;
  83. console.log('str1=' + str1);
  84. str2=str1.substring(4, 6)+str1.substring(6, 8)+str1.substring(8, 10)+str1.substring(10, 12);
  85. console.log('str2=' + str2);
  86. //var a = Long.parseLong(str2,16);
  87. var a = parseInt(str2,16);
  88. console.log('a=' + a);
  89. str3=String(a);
  90. console.log('str3=' + str3);
  91. str4=str3.substring(str3.length-4);
  92. console.log('str4=' + str4);
  93. buf_cnkey[2]=str4.charCodeAt(0);
  94. buf_cnkey[3]=str4.charCodeAt(1);
  95. buf_cnkey[4]=str4.charCodeAt(2);
  96. buf_cnkey[5]=str4.charCodeAt(3);
  97. // チェックサム
  98. buf_cnkey[19]=convCheckSum(buf_cnkey);
  99. console.log('convCheckSum=' + buf_cnkey[19]);
  100. CHARACTERISTICS_BLESERIAL_TX.write(buf_cnkey, false, function(error){
  101. console.log('error:' + error);
  102. });
  103. // チェックサム
  104. buf_E7620101[19]=convCheckSum(buf_E7620101);
  105. console.log('convCheckSum=' + buf_E7620101[19]);
  106. CHARACTERISTICS_BLESERIAL_TX.write(buf_E7620101, false, function(error){
  107. console.log('error:' + error);
  108. });
  109. }
  110. }
  111.  
  112. // 実際の点滅させるところ
  113. setInterval(function(){
  114. // ANALOG_READ
  115. CHARACTERISTICS_BLESERIAL_RX.read(function(error, data) {
  116. if (data[1] == 0x61) {
  117. console.log( 'stsBO:' + data[2]);
  118. console.log( 'PUL:' + data[3]);
  119. console.log( 'SPO2:' + data[4]);
  120. console.log( 'BAT:' + data[5]);
  121. console.log( 'P:' + data[6]);
  122. console.log( '////////////');
  123. }
  124.  
  125. if (data[1] == 0x63) {
  126. console.log( 'PI:' + data[2]);
  127. console.log( 'PUL:' + data[3]);
  128. console.log( 'SPO2:' + data[4]);
  129. console.log( 'BAT:' + data[5]);
  130. console.log( 'P:' + data[6]);
  131. console.log( '////////////');
  132. }
  133. });
  134. }, 100);
  135.  
  136. });
  137. services[i].discoverIncludedServices();
  138. }
  139. }
  140.  
  141. });
  142.  
  143. // 機器との接続開始
  144. peripheral.connect();
  145. });
  146.  
  147.  
  148. function convCheckSum(buf) {
  149. var sumData = 0;
  150. var i;
  151. for(i = 0; i < 19; i++) {
  152. sumData += buf[i];
  153. }
  154. sumData = 0xFF - (0xa5 + sumData) + 1;
  155. return sumData;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement