-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
89 lines (72 loc) · 2.46 KB
/
App.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { StatusBar } from 'expo-status-bar';
import { Button, LogBox, PermissionsAndroid, StyleSheet, Text, View } from 'react-native';
import RNBluetoothClassic, {
BluetoothDevice
} from 'react-native-bluetooth-classic';
async function requestAccessFineLocationPermission() {
const grantedScan = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
{
title: 'Access fine location required for discovery',
message:
'In order to perform discovery, you must enable/allow ' +
'fine location access.',
buttonNeutral: 'Ask Me Later"',
buttonNegative: 'Cancel',
buttonPositive: 'OK'
}
)
const grantedConnection = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
{
title: 'Access fine location required for discovery',
message:
'In order to perform discovery, you must enable/allow ' +
'fine location access.',
buttonNeutral: 'Ask Me Later"',
buttonNegative: 'Cancel',
buttonPositive: 'OK'
}
)
return grantedScan === PermissionsAndroid.RESULTS.GRANTED && grantedConnection === PermissionsAndroid.RESULTS.GRANTED;
}
export default function App() {
const startDiscovery = async () => {
try {
const granted = await requestAccessFineLocationPermission();
if (!granted) {
throw new Error(`Access fine location was not granted`);
}
try {
const unpaired = await RNBluetoothClassic.startDiscovery();
//Récupère une liste d'objet qui correspond au nom "WGX_iBeacon"
console.log(await unpaired.find(e => e.name === "WGX_iBeacon"))
// const device = await RNBluetoothClassic.connectToDevice(unpaired.find(e => e.name === "WGX_iBeacon").address)
// console.log(device)
// const connected = await device.isConnected()
// console.log(connected)
// device.read().then((data) => console.log(data))
} finally {
}
} catch (err) {
console.log(err)
}
}
return (
<View style={styles.container}>
<Text>Hello</Text>
<StatusBar style="auto" />
<Button title='click moi' onPress={async () => {
const device = await startDiscovery()
}} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});