Created
August 8, 2018 01:29
-
-
Save inderpreet/460031cdeff7c40ea3e95380073df6a9 to your computer and use it in GitHub Desktop.
This file contains 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 scanningTimeout = 2000; // one second | |
var scanningRepeat = scanningTimeout + 8000; // Repeat scanning after 10 seconds for new peripherals. | |
noble.on('stateChange', function(state) { | |
if (state === 'poweredOn') { | |
// | |
// Once the BLE radio has been powered on, it is possible | |
// to begin scanning for services. Pass an empty array to | |
// scan for all services (uses more time and power). | |
// | |
console.log('scanning...'); | |
noble.startScanning(); | |
} else { | |
noble.stopScanning(); | |
} | |
}) | |
// Checking, Scanning, stopping repeatedly | |
setInterval( function(){ | |
if(noble.state==='poweredOn'){ | |
noble.startScanning(); | |
console.log('Starting Scan...'); | |
setTimeout(function(){ | |
noble.stopScanning(); | |
console.log('Stopping Scan...'); | |
}, scanningTimeout) | |
} | |
}, scanningRepeat); | |
noble.on('discover', function(peripheral) { | |
var advertisement = peripheral.advertisement; | |
var localName = advertisement.localName; | |
var txPowerLevel = advertisement.txPowerLevel; | |
var manufacturerData = advertisement.manufacturerData; | |
var serviceData = advertisement.serviceData; | |
var serviceUuids = advertisement.serviceUuids; | |
console.log('Found peripheral :'); | |
//console.log(peripheral.id); | |
console.log(localName); | |
console.log('\n'); | |
}); // End on Noble Discover! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment