UNPKG

react-native-ble

Version:

Central role BLE for react native noble api surface

98 lines (87 loc) 2.76 kB
/** * Sample React Native App * https://github.com/facebook/react-native * @flow */ import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View } from 'react-native'; import noble from 'react-native-ble'; class advertisement_discovery extends Component { render() { return ( <View style={styles.container}> <Text style={styles.welcome}> Welcome to React Native! </Text> <Text style={styles.instructions}> To get started, edit index.android.js </Text> <Text style={styles.instructions}> Double tap R on your keyboard to reload,{'\n'} Shake or press menu button for dev menu </Text> </View> ); } componentWillMount() { noble.on('stateChange', this._onStateChange); noble.on('discover', this._onDiscover); } _onStateChange(state) { if (state === 'poweredOn') { noble.startScanning(); } else { noble.stopScanning(); } } _onDiscover(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(); } } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, welcome: { fontSize: 20, textAlign: 'center', margin: 10, }, instructions: { textAlign: 'center', color: '#333333', marginBottom: 5, }, }); AppRegistry.registerComponent('advertisement_discovery', () => advertisement_discovery);