Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //var noble = require('noble');
- var noble = require('../../node_modules/noble/index');
- // UUID
- var UUID_BLESERIAL_SERVICE = '1ef19620-a803-4af0-ae95-4b4b0aa26f29'.replace(/\-/g, '');
- var UUID_BLESERIAL_RX = '1ef19621-a803-4af0-ae95-4b4b0aa26f29'.replace(/\-/g, '');
- var UUID_BLESERIAL_TX = '1ef19622-a803-4af0-ae95-4b4b0aa26f29'.replace(/\-/g, '');
- // Command
- 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]);
- 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]);
- // 各CHARACTERISTICSをUUIDから判断してcharacteristic格納する変数
- var CHARACTERISTICS_BLESERIAL_SERVICE;
- var CHARACTERISTICS_BLESERIAL_RX;
- var CHARACTERISTICS_BLESERIAL_TX;
- //
- var str1,str2,str3,str4;
- // LED ON/OFF
- //var led_toggle = false;
- // 状態がパワーONだったらスキャンに移行
- noble.on('stateChange', function(state) {
- console.log('on -> stateChange: ' + state);
- if (state === 'poweredOn') {
- noble.startScanning();
- } else {
- noble.stopScanning();
- }
- });
- noble.on('scanStart', function() {
- console.log('on -> scanStart');
- });
- noble.on('scanStop', function() {
- console.log('on -> scanStop');
- });
- // discover 機器が発見されたら
- noble.on('discover', function(peripheral) {
- console.log('on -> discover: ' + peripheral);
- // まずスキャンをとめる
- noble.stopScanning();
- // 接続時のイベント
- peripheral.on('connect', function() {
- console.log('on -> connect');
- this.discoverServices();
- });
- // 切断時のイベント
- peripheral.on('disconnect', function() {
- console.log('on -> disconnect');
- });
- // 見つけたサービス(機器)へのアクセス
- peripheral.on('servicesDiscover', function(services) {
- for(i = 0; i < services.length; i++) {
- // サービスがUUID_BLESERIAL_SERVICEと一致した時だけ処理
- if(services[i]['uuid'] == UUID_BLESERIAL_SERVICE){
- // サービスのcharacteristic捜索
- services[i].on('includedServicesDiscover', function(includedServiceUuids) {
- console.log('on -> service included services discovered [' + includedServiceUuids + ']');
- this.discoverCharacteristics();
- });
- // characteristic取得イベント
- services[i].on('characteristicsDiscover', function(characteristics) {
- // characteristics配列から必要なCHARACTERISTICSをUUIDから判断してcharacteristic格納
- for(j = 0; j < characteristics.length; j++) {
- // 入力 characteristic
- if( UUID_BLESERIAL_RX == characteristics[j].uuid ){
- console.log("CHARACTERISTICS_BLESERIAL_RX exist!!");
- CHARACTERISTICS_BLESERIAL_RX = characteristics[j];
- }
- // 出力 characteristic
- // デジタル入出力を担当するPIO全部に出力するようにお願いする
- if( UUID_BLESERIAL_TX == characteristics[j].uuid ) {
- console.log("CHARACTERISTICS_BLESERIAL_TX exist!!");
- CHARACTERISTICS_BLESERIAL_TX = characteristics[j];
- // CNKEY
- str1=peripheral.id;
- console.log('str1=' + str1);
- str2=str1.substring(4, 6)+str1.substring(6, 8)+str1.substring(8, 10)+str1.substring(10, 12);
- console.log('str2=' + str2);
- //var a = Long.parseLong(str2,16);
- var a = parseInt(str2,16);
- console.log('a=' + a);
- str3=String(a);
- console.log('str3=' + str3);
- str4=str3.substring(str3.length-4);
- console.log('str4=' + str4);
- buf_cnkey[2]=str4.charCodeAt(0);
- buf_cnkey[3]=str4.charCodeAt(1);
- buf_cnkey[4]=str4.charCodeAt(2);
- buf_cnkey[5]=str4.charCodeAt(3);
- // チェックサム
- buf_cnkey[19]=convCheckSum(buf_cnkey);
- console.log('convCheckSum=' + buf_cnkey[19]);
- CHARACTERISTICS_BLESERIAL_TX.write(buf_cnkey, false, function(error){
- console.log('error:' + error);
- });
- // チェックサム
- buf_E7620101[19]=convCheckSum(buf_E7620101);
- console.log('convCheckSum=' + buf_E7620101[19]);
- CHARACTERISTICS_BLESERIAL_TX.write(buf_E7620101, false, function(error){
- console.log('error:' + error);
- });
- }
- }
- // 実際の点滅させるところ
- setInterval(function(){
- // ANALOG_READ
- CHARACTERISTICS_BLESERIAL_RX.read(function(error, data) {
- if (data[1] == 0x61) {
- console.log( 'stsBO:' + data[2]);
- console.log( 'PUL:' + data[3]);
- console.log( 'SPO2:' + data[4]);
- console.log( 'BAT:' + data[5]);
- console.log( 'P:' + data[6]);
- console.log( '////////////');
- }
- if (data[1] == 0x63) {
- console.log( 'PI:' + data[2]);
- console.log( 'PUL:' + data[3]);
- console.log( 'SPO2:' + data[4]);
- console.log( 'BAT:' + data[5]);
- console.log( 'P:' + data[6]);
- console.log( '////////////');
- }
- });
- }, 100);
- });
- services[i].discoverIncludedServices();
- }
- }
- });
- // 機器との接続開始
- peripheral.connect();
- });
- function convCheckSum(buf) {
- var sumData = 0;
- var i;
- for(i = 0; i < 19; i++) {
- sumData += buf[i];
- }
- sumData = 0xFF - (0xa5 + sumData) + 1;
- return sumData;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement