Created
April 2, 2013 00:16
-
-
Save sandeepmistry/5288912 to your computer and use it in GitHub Desktop.
Modified test2.js from @mrose17
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var noble = require('noble'); | |
var main = function() { | |
noble.on('stateChange', function(state) { | |
console.log('stateChange: ' + state); | |
if (state === 'poweredOn') noble.startScanning(); else noble.stopScanning(); | |
}); | |
noble.on('peripheralDiscover', function(peripheral) { | |
if (!!peripheral) peripheral_init(peripheral); else console.log('null peripheral'); | |
}); | |
}; | |
var peripheral_init = function(peripheral) { | |
var ble = {}; | |
peripheral.on('connect', function() { | |
console.log('peripheralConnect: ' + peripheral.uuid); | |
peripheral.updateRssi(); | |
}); | |
peripheral.on('disconnect', function() { | |
console.log('peripheralDisconnect: ' + peripheral.uuid); | |
console.log(JSON.stringify(ble)); | |
}); | |
peripheral.on('rssiUpdate', function(rssi) { | |
console.log('peripheralRssiUpdate: ' + rssi); | |
peripheral.discoverServices(); | |
}); | |
peripheral.on('servicesDiscover', function(services) { | |
var i, s; | |
// var includedServicesDiscover = function(includedServiceUuids) { | |
// console.log('includedServiceUuids'); console.log(includedServiceUuids); console.log('+++'); | |
// this.discoverCharacteristics(); | |
// }; | |
var characteristicsDiscover = function(characteristics) { | |
// return function(characteristics) { | |
var c, j; | |
for (j = 0; j < characteristics.length; j++) { | |
c = characteristics[j]; | |
console.log(c.uuid); | |
// service.characteristics[c.uuid] = { properties: c.properties, descriptors: {} }; | |
// c.on('descriptorsDiscover', descriptorsDiscover(service.characteristics[c.uuid])); | |
// c.discoverDescriptors(); | |
} | |
// }; | |
}; | |
// var descriptorsDiscover = function(characteristic) { | |
// return function(descriptors) { | |
// var d, k; | |
// for (k = 0; k < descriptors.length; k++) { | |
// d = descriptors[k]; | |
// characteristic.descriptors[d.uuid] = { properties : d.properties, descriptors: d.descriptors }; | |
// } | |
// }; | |
// }; | |
for (i = 0; i < services.length; i++) { | |
s = services[i]; | |
if (!s.uuid) continue; | |
ble[s.uuid] = { includedServiceUuids: {}, characteristics: {} }; | |
//s.on('includedServicesDiscover', includedServicesDiscover); | |
s.on('characteristicsDiscover', characteristicsDiscover); | |
//s.discoverIncludedServices(); | |
s.discoverCharacteristics(); | |
} | |
}); | |
peripheral.connect(); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment