forked from sanctuarycomputer/react-native-bluetooth-status
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppClass.js
47 lines (41 loc) · 1.2 KB
/
AppClass.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import React from 'react';
import {SafeAreaView, Text, StatusBar, Platform, Button} from 'react-native';
import {
BluetoothStatus,
useBluetoothStatus,
} from 'react-native-bluetooth-status';
class AppClass extends React.Component {
state = {
bluetoothState: undefined,
};
componentDidMount() {
this.checkInitialBluetoothState();
BluetoothStatus.addListener(isEnabled => {
this.setState({bluetoothState: isEnabled ? 'On' : 'Off'});
});
}
async checkInitialBluetoothState() {
const isEnabled = await BluetoothStatus.state();
this.setState({bluetoothState: isEnabled ? 'On' : 'Off'});
}
async toggleBluetooth() {
const isEnabled = await BluetoothStatus.state();
BluetoothStatus.enable(!isEnabled);
}
render() {
const {bluetoothState} = this.state;
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<Text>New tester is here!</Text>
{bluetoothState !== undefined && <Text>{bluetoothState}</Text>}
{Platform.OS === 'android' && (
<Button title="Toggle BT" onPress={() => this.toggleBluetooth()} />
)}
</SafeAreaView>
</>
);
}
}
export default AppClass;