-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3374942
commit 7a59020
Showing
5 changed files
with
130 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { PermissionsAndroid, Platform } from 'react-native'; | ||
|
||
const defaultFineLocationParams = { | ||
title: 'Location Permission', | ||
message: 'App needs access to your Location', | ||
buttonPositive: 'Accept', | ||
}; | ||
|
||
type PermissionsProps = { | ||
accessFineLocation?: { | ||
title: string; | ||
message: string; | ||
buttonPositive: string; | ||
}; | ||
}; | ||
|
||
const isAndroid12orHigher = () => | ||
Platform.OS === 'android' && Platform.Version >= 31; | ||
|
||
export async function requestNeededAndroidPermissions({ | ||
accessFineLocation = defaultFineLocationParams, | ||
}: PermissionsProps | undefined = {}): Promise<boolean> { | ||
let hasGrantedLocationPermissions = false; | ||
let hasGrantedBluetoothPermissions = false; | ||
|
||
const grantedFineLocation = await PermissionsAndroid.request( | ||
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, | ||
accessFineLocation || defaultFineLocationParams | ||
); | ||
|
||
if (hasGrantedPermission(grantedFineLocation)) { | ||
hasGrantedLocationPermissions = true; | ||
|
||
if (isAndroid12orHigher()) { | ||
const grantedBT = await PermissionsAndroid.request( | ||
// BLUETOOTH_CONNECT doesn't support customization | ||
PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT | ||
); | ||
|
||
const grantedBTScan = await PermissionsAndroid.request( | ||
// BLUETOOTH_SCAN doesn't support customization | ||
PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN | ||
); | ||
if ( | ||
hasGrantedPermission(grantedBT) && | ||
hasGrantedPermission(grantedBTScan) | ||
) { | ||
hasGrantedBluetoothPermissions = true; | ||
} | ||
} else { | ||
hasGrantedBluetoothPermissions = true; | ||
} | ||
} | ||
return hasGrantedBluetoothPermissions && hasGrantedLocationPermissions; | ||
} | ||
|
||
const hasGrantedPermission = (status: string) => { | ||
return status === PermissionsAndroid.RESULTS.GRANTED; | ||
}; |