Last active
June 22, 2023 11:27
-
-
Save Naveen-spritle/7adcdcd90941749cf36c68ae68a4ed16 to your computer and use it in GitHub Desktop.
Checking the Android version to perform the appropriate permissions for appropriate Android version by using "expo-device" library
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
const [accessPermission, setAccessPermission] = useState(false); | |
const [askAgain, setAskAgain] = useState({}); | |
//PERMISSION FOR ACCESS BLUETOOTH | |
useEffect(() => { | |
if (Number(Device.osVersion) >= 12) { // Device is import from "expo-device" library for check the device OS version | |
const result = PermissionsAndroid.requestMultiple([ | |
PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT, | |
PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN, | |
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, | |
]); | |
result | |
.then((res) => { | |
// console.log(res); | |
let permission = 0; | |
for (let key in res) { | |
if (res[key] == "granted") { | |
permission++; | |
} else if (res[key] == "never_ask_again") { | |
return permissionNeverAskAgain(); | |
} else { | |
return setAskAgain({}); | |
} | |
} | |
if (permission == 3) { | |
setAccessPermission(true); | |
} else { | |
permissionNeverAskAgain(); | |
} | |
}) | |
.catch((e) => { | |
alert("Something went wrong in bluetooth"); | |
}); | |
} else if (Number(Device.osVersion) >= 10) { | |
let result = PermissionsAndroid.request( | |
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION | |
); | |
result | |
.then((response) => { | |
console.log(response); | |
response == "granted" && setAccessPermission(true); | |
response == "denied" && setAskAgain({}); | |
if (response == "never_ask_again") { | |
permissionNeverAskAgain(); | |
} | |
}) | |
.catch((error) => { | |
alert("Something went wrong"); | |
}); | |
} else { | |
let result = PermissionsAndroid.request( | |
PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION | |
); | |
result | |
.then((response) => { | |
console.log(response); | |
response == "granted" && setAccessPermission(true); | |
response == "denied" && setAskAgain({}); | |
if (response == "never_ask_again") { | |
permissionNeverAskAgain(); | |
} | |
}) | |
.catch((error) => { | |
alert("Something went wrong"); | |
}); | |
} | |
}, [askAgain]); | |
function permissionNeverAskAgain() { | |
setAccessPermission(false); | |
alert("Need permission for bluetooth access"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment