-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestPermissions.ts
32 lines (31 loc) · 1.11 KB
/
requestPermissions.ts
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
import { Platform, PermissionsAndroid } from "react-native";
export async function requestPermissions(): Promise<boolean> {
let granted = false;
// Ask for permissions on Android
if (Platform.OS === "android") {
const bleScan = PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN;
if (Platform.Version <= 30 || !bleScan) {
// For SDK 30 and below
const fineLocation = PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION;
if (fineLocation) {
const status = await PermissionsAndroid.request(fineLocation);
granted = status === PermissionsAndroid.RESULTS.GRANTED;
}
} else {
// For SDK 31 and above
const bleConnect = PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT;
if (bleScan && bleConnect) {
const status = await PermissionsAndroid.requestMultiple([
bleScan,
bleConnect,
]);
granted =
status[bleScan] === PermissionsAndroid.RESULTS.GRANTED &&
status[bleConnect] === PermissionsAndroid.RESULTS.GRANTED;
}
}
} else if (Platform.OS === "ios") {
granted = true;
}
return granted;
}