Skip to content

Instantly share code, notes, and snippets.

@jacobrosenthal
Last active September 15, 2015 23:48
Show Gist options
  • Save jacobrosenthal/85bb514ade5b62d4b02a to your computer and use it in GitHub Desktop.
Save jacobrosenthal/85bb514ade5b62d4b02a to your computer and use it in GitHub Desktop.
Example React Native BLE
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
'use strict';
var React = require('react-native');
var {
AppRegistry,
Text,
View
} = React;
var noble = require('react-native-ble');
var bleqr = React.createClass({
getInitialState: function() {
return {
};
},
componentWillMount: function(){
noble.startScanning();
noble.on('stateChange', this._onStateChange);
noble.on('discover', this._onPeripheralFound);
},
componentWillUnMount: function(){
noble.stopScannning();
},
render: function() {
return (
<View>
</View>
);
},
_onStateChange: function(state) {
console.log('_onStateChange', state);
// if (state === 'poweredOn') {
// noble.startScanning();
// } else {
// noble.stopScanning();
// }
},
_onPeripheralFound: function(peripheral) {
console.log('peripheral discovered (' + peripheral.id +
' with address <' + peripheral.address + ', ' + peripheral.addressType + '>,' +
' connectable ' + peripheral.connectable + ',' +
' RSSI ' + peripheral.rssi + ':');
console.log('\thello my local name is:');
console.log('\t\t' + peripheral.advertisement.localName);
console.log('\tcan I interest you in any of the following advertised services:');
console.log('\t\t' + JSON.stringify(peripheral.advertisement.serviceUuids));
var serviceData = peripheral.advertisement.serviceData;
if (serviceData && serviceData.length) {
console.log('\there is my service data:');
for (var i in serviceData) {
console.log('\t\t' + JSON.stringify(serviceData[i].uuid) + ': ' + JSON.stringify(serviceData[i].data.toString('hex')));
}
}
if (peripheral.advertisement.manufacturerData) {
console.log('\there is my manufacturer data:');
console.log('\t\t' + JSON.stringify(peripheral.advertisement.manufacturerData.toString('hex')));
}
if (peripheral.advertisement.txPowerLevel !== undefined) {
console.log('\tmy TX power level is:');
console.log('\t\t' + peripheral.advertisement.txPowerLevel);
}
console.log();
}
});
AppRegistry.registerComponent('bleqr', () => bleqr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment