Skip to content

Instantly share code, notes, and snippets.

@jakalada
Created March 29, 2016 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakalada/256ecd63d454bc548ed21b508d2cb121 to your computer and use it in GitHub Desktop.
Save jakalada/256ecd63d454bc548ed21b508d2cb121 to your computer and use it in GitHub Desktop.
Raspberry Pi 3とGenuino (Arduino) 101でBLE通信する ref: http://qiita.com/jakalada/items/7ad4144efa2f5e109b89
peripheral discovered (xxxxxxxxxxxx with address <xx:xx:xx:xx:xx:xx, public>, connectable true, RSSI -68:
hello my local name is:
ButtonLE
can I interest you in any of the following advertised services:
["19b10010e8f2537e4f6cd104768a1214"]
$ sudo node peripheral-explorer.js xxxxxxxxxxxx
peripheral with ID xxxxxxxxxxxx found
Local Name = ButtonLE
Service Data =
Service UUIDs = 19b10010e8f2537e4f6cd104768a1214
services and characteristics:
1800 (Generic Access)
2a00 (Device Name)
properties read
value 47454e55494e4f203130312d38323841 | 'GENUINO 101-828A'
2a01 (Appearance)
properties read
value 0000 | ''
2a04 (Peripheral Preferred Connection Parameters)
properties read
value 4000780000005802 | '@xX'
1801 (Generic Attribute)
19b10010e8f2537e4f6cd104768a1214
19b10011e8f2537e4f6cd104768a1214
properties read, write
value 00 | ''
19b10012e8f2537e4f6cd104768a1214
properties read, notify
value 00 | ''
var noble = require('noble');
var ledServiceUuid = '19b10010e8f2537e4f6cd104768a1214';
var ledCharacteristicUuid = '19b10011e8f2537e4f6cd104768a1214';
var buttonCharacteristicUuid = '19b10012e8f2537e4f6cd104768a1214';
var peripheralIdOrAddress = process.argv[2].toLowerCase();
// Bluetoothデバイスの状態変更時の処理
noble.on('stateChange', function(state) {
if (state === 'poweredOn') {
// ペリフェラルのスキャンを開始
noble.startScanning();
} else {
noble.stopScanning();
}
});
// ペリフェラル発見時の処理
noble.on('discover', function(peripheral) {
if (peripheral.id === peripheralIdOrAddress || peripheral.address === peripheralIdOrAddress) {
// ペリフェラルのスキャンを停止
noble.stopScanning();
console.log('peripheral with ID ' + peripheral.id + ' found');
peripheral.once('disconnect', function() {
console.log('disconnected');
process.exit(0);
});
// ペリフェラルとの接続を開始
peripheral.connect(function(error) {
console.log('connected');
// サービス探索を開始
peripheral.discoverServices([], onServicesDiscovered);
});
}
});
// サービス発見時の処理
function onServicesDiscovered(error, services) {
console.log('services discovered');
services.forEach(function(service) {
if (service.uuid == ledServiceUuid) {
// キャラクタリスティック探索を開始
service.discoverCharacteristics([], onCharacteristicDiscovered);
}
});
}
// キャラクタリスティック発見時の処理
function onCharacteristicDiscovered(error, characteristics) {
console.log('characteristics discovered');
characteristics.forEach(function(characteristic) {
if (characteristic.uuid == ledCharacteristicUuid) {
characteristic.on('read', onLedCharacteristicRead);
} else if (characteristic.uuid == buttonCharacteristicUuid) {
characteristic.on('read', onButtonCharacteristicRead);
// データ更新通知の受け取りを開始
characteristic.notify(true, function(error) {
console.log('buttonCharacteristic notification on');
});
}
});
}
// ledCharacteristicのデータ読み出し時の処理
// (現在のこのコードでは呼ばれることがない想定です)
function onLedCharacteristicRead(data, isNotification) {
console.log('ledCharacteristic read response value: ', data.readInt8(0));
}
// buttonCharacteristicのデータ読み出し時、もしくは更新時の処理
function onButtonCharacteristicRead(data, isNotification) {
if (isNotification) {
// データ更新通知による呼び出し
console.log('buttonCharacteristic notification value: ', data.readInt8(0));
} else {
// データ読み出し時の呼び出し
console.log('buttonCharacteristic read response value: ', data.readInt8(0));
}
}
sudo node button_led_central.js xxxxxxxxxxxx
peripheral with ID xxxxxxxxxxxx found
connected
services discovered
characteristics discovered
buttonCharacteristic notification on
buttonCharacteristic notification value: 1
buttonCharacteristic notification value: 0
buttonCharacteristic notification value: 1
buttonCharacteristic notification value: 0
buttonCharacteristic notification value: 1
buttonCharacteristic notification value: 0
buttonCharacteristic notification value: 1
buttonCharacteristic notification value: 0
buttonCharacteristic notification value: 1
buttonCharacteristic notification value: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment