Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import { StyleSheet, Button } from 'react-native';
import React, { Component } from 'react';
import { Platform, View, Text } from 'react-native';
import { BleManager } from 'react-native-ble-plx';
export default class ComponentsScreen extends Component {
constructor() {
super()
this.manager = new BleManager();
}
componentWillMount() {
console.log('mounted');
if (Platform.OS === 'ios') {
console.log('ios');
const subscription = this.manager.onStateChange((state) => {
if (state === 'PoweredOn') {
console.log("inside onStateChange");
this.scanAndConnect();
subscription.remove();
}
}, true);
} else {
this.scanAndConnect();
console.log('not ios');
}
}
scanAndConnect() {
console.log('start scanning ...');
this.manager.startDeviceScan(null,
null, (error, device) => {
console.log(device)
if (error) {
console.log(error);
return
}
// if (device.name === 'TI BLE Sensor Tag' || device.name === 'SensorTag') {
// this.info("Connecting to TI Sensor")
// this.manager.stopDeviceScan()
// device.connect()
// .then((device) => {
// this.info("Discovering services and characteristics")
// return device.discoverAllServicesAndCharacteristics()
// })
// .then((device) => {
// this.info("Setting notifications")
// return this.setupNotifications(device)
// })
// .then(() => {
// this.info("Listening...")
// }, (error) => {
// this.error(error.message)
// })
// }
});
}
render() {
return (
<View>
<Button
onPress={() => { console.log('Scan') }}
title="Scan"></Button>
</View>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.