-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
161 lines (152 loc) · 4.5 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { StatusBar } from "expo-status-bar";
import React, { useEffect, useState, useMemo } from "react";
import { Button, Text, View } from "react-native";
import { BleManager } from "react-native-ble-plx";
import { requestMultiple, PERMISSIONS } from "react-native-permissions";
import styles from "./styles";
const App = () => {
const manager = new BleManager();
const [devices, setDevices] = useState([]);
const [connectedDeviceId, setConnectedDeviceId] = useState("");
let timer;
// Request Android Permissions
// TODO: Add iOS Permissions too
const requestPermission = async () => {
try {
const granted = await requestMultiple([
PERMISSIONS.ANDROID.BLUETOOTH_CONNECT,
PERMISSIONS.ANDROID.BLUETOOTH_ADVERTISE,
PERMISSIONS.ANDROID.BLUETOOTH_SCAN,
PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION,
PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION,
]).then((statuses) => {
// status after permission check.
});
} catch (err) {
console.warn(err);
}
};
const scanAndConnect = () => {
manager.startDeviceScan(
null,
{ allowDuplicates: false },
(error, device) => {
if (error) {
console.log({ error });
// Handle error (scanning will be stopped automatically)
return;
}
// Check if it is a device you are looking for based on advertisement data
// or other criteria.
if (device.name !== undefined && device.name !== null) {
console.log("device name " + device.name);
setDevices((deviceList) => {
return deviceList.indexOf((dev) => dev?.id === device?.id) === -1
? [...deviceList, device]
: deviceList;
});
}
if (
device.name === "TI BLE Sensor Tag" ||
device.name === "SensorTag"
) {
// Stop scanning as it's not necessary if you are scanning for one device.
manager.stopDeviceScan();
// Proceed with connection.
}
}
);
// TODO: emove the below once we have the intended device details/id.
};
const getDetails = (device) => {
console.log("Get Details function start");
if (device && device?.id !== null) {
manager
.connectToDevice(device?.id)
.then(() => {
setConnectedDeviceId(device?.id);
console.log(device?.name + " connected !!!");
manager
.characteristicsForDevice(device?.id)
.then((device) => {
console.log({ device });
})
.catch((error) => {
console.log("get all details error", error);
});
manager
.servicesForDevice(device?.id)
.then((device) => {
console.log({ device });
})
.catch((error) => {
console.log("get service error", error);
});
})
.catch((error) => {
console.log("connection error", error);
});
}
};
useEffect(() => {
requestPermission();
const subscription = manager.onStateChange((state) => {
if (state === "PoweredOn") {
scanAndConnect();
subscription.remove();
timer = setTimeout(() => manager.stopDeviceScan(), 2000);
}
}, true);
return () => {
subscription.remove();
clearTimeout(timer);
};
}, []);
const deviceConnectionText = useMemo(
(device) => {
if (connectedDeviceId === device?.id) {
return null;
} else {
return (
<Button
style={styles.connectButton}
color={"#bce4f5"}
onPress={() => getDetails(dev)}
title={"connect"}
/>
);
}
},
[devices, connectedDeviceId]
);
const deviceInfo = useMemo(() => {
if (devices && devices?.length === 0) {
return null;
}
try {
return (
<View>
{devices?.map((dev, index) => {
return (
<View key={dev?.id + index} style={styles.deviceInfo}>
<Text style={styles.deviceInfoText}>{dev.name}</Text>
{deviceConnectionText}
</View>
);
})}
</View>
);
} catch (error) {
console.log(error);
return null;
}
}, [devices, getDetails]);
return (
<View style={styles.container}>
<StatusBar style="auto" />
<Text style={styles.header}>Discoverables Devices :</Text>
{deviceInfo}
</View>
);
};
export default App;