前提・実現したいこと
Raspberry piにて開発を行っています。
現在、2台のRaspberry piを使用しており、片方のRaspberry piはBLEのperipheral(送信)の処理を、もう片方のRaspberry piではcentral(受信)の処理をnode.jsにて実装しようとしております。
peripheral側ではblenoを、central側ではnobleを使用しています。
発生している問題・エラーメッセージ
現在、central側でプログラムを実行したとき、peripheral側のuuidを識別子、接続するところまでは出来ているのですが、serviceを読み取ろうとしたときに、serviceがundefinedになっています。
/home/pi/beacon/central.js:41 deviceInformationService.discoverCharacteristics(SERVICE_CHARACTERISTIC_UUID, function(characteristics) { ^ TypeError: Cannot read property 'discoverCharacteristics' of undefined at /home/pi/beacon/central.js:41:38 at Peripheral.<anonymous> (/home/pi/beacon/node_modules/noble/lib/peripheral.js:73:7) at Object.onceWrapper (events.js:315:30) at emitOne (events.js:116:13) at Peripheral.emit (events.js:211:7) at Noble.onServicesDiscover (/home/pi/beacon/node_modules/noble/lib/noble.js:251:16) at emitTwo (events.js:126:13) at NobleBindings.emit (events.js:214:7) at NobleBindings.onServicesDiscovered (/home/pi/beacon/node_modules/noble/lib/hci-socket/bindings.js:305:8) at emitTwo (events.js:126:13)
該当のソースコード
javascript
1//Central 2var noble = require('noble'); 3 4var DEVICE_NAME = "Peripheral"; //検索したいデバイス名 5var SERVICE_UUID = "180F"; //検索したいサービスのUUID 6var SERVICE_CHARACTERISTIC_UUID = "2A19"; //検索したキャラクスティックのUUID 7 8//start ble 9noble.on('stateChange', function(state) { 10 if (state === 'poweredOn') { 11 noble.startScanning(); 12 } else { 13 noble.stopScanning(); 14 } 15}); 16 17//search ble 18noble.on('discover', function(peripheral) { 19 //output seach device 20 console.log("DEVICE_NAME: " + peripheral.advertisement.localName); 21 console.log("UUID: " + peripheral.uuid); 22 console.log("RSSI: " + peripheral.rssi); 23 console.log(); 24 25 //equals devicename 26 if(peripheral.advertisement.localName == DEVICE_NAME){ 27 console.log("find"); 28 noble.stopScanning(); 29 30 //connect 31 peripheral.connect(function(error){ 32 console.log("connected to peripheral:" + peripheral.uuid); 33 34 //find service 35 peripheral.discoverServices(null, function(error,services) { 36 for (var i in services) { 37 console.log(' ' + i + ' uuid: ' + services[i].uuid); 38 } 39 console.log(services); 40 var deviceInformationService = services[0]; 41 //find CharacteristicService 42 deviceInformationService.discoverCharacteristics(SERVICE_CHARACTERISTIC_UUID, function(characteristics) { 43 44 //get notify data 45 characteristics[0].on('data', function(data, isNotification) { 46 var result = ""; 47 for(var i=0; i<data.length; i++){ 48 result += data[i] + ','; 49 } 50 console.log(result); 51 }); 52 53 //notify? 54 characteristics[0].subscribe(function(error) { 55 console.log('notify'); 56 }); 57 58 }); 59 }); 60 }); 61 //disconnect 62 peripheral.disconnect(function(error){ 63 console.log("disconnected from peripheral"); 64 console.log("Start Scaning"); 65 noble.startScanning(); 66 }); 67 } 68});
//peripheral var bleno = require('bleno'); var name = 'Peripheral'; var serviceUuids = ['180F']; var primaryService = new bleno.PrimaryService({ uuid: '180F', characteristics: [ new bleno.Characteristic({ uuid: '2A19', properties: ['read'], value: new Buffer([100]) }) ] }); bleno.on('stateChange', function(state) { console.log('stateChange: '+state); if (state === 'poweredOn') { bleno.startAdvertising(name, serviceUuids, function(error){ if (error) console.error(error); }); } else { bleno.stopAdvertising(); } }); bleno.on('advertisingStart', function(error){ if (!error) { console.log('start advertising...'); bleno.setServices([primaryService]); } else { console.error(error); } });
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー