Skip to content

Commit

Permalink
android permissions util
Browse files Browse the repository at this point in the history
  • Loading branch information
arekkubaczkowski committed Mar 29, 2022
1 parent 3374942 commit 7a59020
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 59 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,32 @@ useEffect(() => {
}, []);
```

For convenience, Stripe Terminal SDK also provides an util that handles all needed Android permissions.
In order to use it follow below instrustions:

```tsx
import { requestNeededAndroidPermissions } from 'stripe-terminal-react-native';

try {
const granted = await requestNeededAndroidPermissions({
accessFineLocation: {
title: 'Location Permission Permission',
message: 'App needs access to your Location',
buttonPositive: 'Accept',
},
});
if (granted) {
// init SDK
} else {
console.error(
'Location and BT services are required in order to connect to a reader.'
);
}
} catch (e) {
console.error(e);
}
```

#### Manifest

To enable compatibility the library with the latest Android 12 please make sure that you add following requirements:
Expand Down
28 changes: 27 additions & 1 deletion docs/set-up-your-sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ useEffect(() => {
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'Location Permission Permission',
message: 'App needs access to your Location ',
message: 'App needs access to your Location',
buttonPositive: 'Accept',
}
);
Expand All @@ -102,6 +102,32 @@ useEffect(() => {
}, []);
```

For convenience, Stripe Terminal SDK also provides an util that handles all needed Android permissions.
In order to use it follow below instrustions:

```tsx
import { requestNeededAndroidPermissions } from 'stripe-terminal-react-native';

try {
const granted = await requestNeededAndroidPermissions({
accessFineLocation: {
title: 'Location Permission Permission',
message: 'App needs access to your Location',
buttonPositive: 'Accept',
},
});
if (granted) {
// init SDK
} else {
console.error(
'Location and BT services are required in order to connect to a reader.'
);
}
} catch (e) {
console.error(e);
}
```

#### Manifest

To enable compatibility the library with the latest Android 12 please make sure that you ad following requirements:
Expand Down
73 changes: 15 additions & 58 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ import {
TransitionPresets,
} from '@react-navigation/stack';
import HomeScreen from './screens/HomeScreen';
import {
Alert,
PermissionsAndroid,
Platform,
StatusBar,
StyleSheet,
} from 'react-native';
import { Alert, Platform, StatusBar, StyleSheet } from 'react-native';
import { colors } from './colors';
import { LogContext, Log, Event } from './components/LogContext';
import DiscoverReadersScreen from './screens/DiscoverReadersScreen';
Expand All @@ -27,11 +21,11 @@ import ReadReusableCardScreen from './screens/ReadReusableCardScreen';
import LogListScreen from './screens/LogListScreen';
import LogScreen from './screens/LogScreen';
import RegisterInternetReaderScreen from './screens/RegisterInternetReaderScreen';
import { isAndroid12orHigher } from './utils';
import {
Reader,
Location,
useStripeTerminal,
requestNeededAndroidPermissions,
} from 'stripe-terminal-react-native';
import { LogBox } from 'react-native';

Expand Down Expand Up @@ -107,50 +101,23 @@ export default function App() {
useEffect(() => {
async function handlePermissions() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'Location Permission',
message: 'App needs access to your Location ',
const granted = await requestNeededAndroidPermissions({
accessFineLocation: {
title: 'Location Permission Permission',
message: 'App needs access to your Location',
buttonPositive: 'Accept',
}
);

if (hasGrantedPermission(granted)) {
if (isAndroid12orHigher()) {
const grantedBT = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
{
title: 'BT Permission',
message: 'App needs access to Bluetooth ',
buttonPositive: 'Accept',
}
);

const grantedBTScan = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
{
title: 'BT Permission',
message: 'App needs access to Bluetooth ',
buttonPositive: 'Accept',
}
);
if (
hasGrantedPermission(grantedBT) &&
hasGrantedPermission(grantedBTScan)
) {
handlePermissionsSuccess();
return;
} else {
handlePermissionsError();
return;
}
}
},
});
if (granted) {
handlePermissionsSuccess();
} else {
handlePermissionsError();
console.error(
'Location and BT services are required in order to connect to a reader.'
);
}
} catch {}
} catch (e) {
console.error(e);
}
}
if (Platform.OS === 'android') {
handlePermissions();
Expand All @@ -160,12 +127,6 @@ export default function App() {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const handlePermissionsError = () => {
console.error(
'Location and BT services are required in order to connect to a reader.'
);
};

const handlePermissionsSuccess = async () => {
const { error } = await initStripe({
logLevel: 'verbose',
Expand All @@ -177,10 +138,6 @@ export default function App() {
}
};

const hasGrantedPermission = (status: string) => {
return status === PermissionsAndroid.RESULTS.GRANTED;
};

const addLogs = (newLog: Log) => {
const updateLog = (log: Log) =>
log.name === newLog.name
Expand Down
3 changes: 3 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ export {
withStripeTerminal,
WithStripeTerminalProps,
} from './components/withStripeTerminal';

// utils
export { requestNeededAndroidPermissions } from './utils/androidPermissionsUtils';
59 changes: 59 additions & 0 deletions src/utils/androidPermissionsUtils.ts
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;
};

0 comments on commit 7a59020

Please sign in to comment.