Reputation: 11
I'm using the react-native-kontaktio
library to scan for beacons (BLE) in my React Native app. The library works correctly on Android devices, but I'm encountering issues with iOS.
Here are my platform and library versions:
react-native
: ^0.68.1react-native-kontaktio
: ^4.1.0I'm trying to listen for beacon events using the following code:
kontaktEmitter.addListener('didRangeBeacons', ({ beacons, region }) => {
// Event handling logic here
});
minSdkVersion = 21
compileSdkVersion = 34
targetSdkVersion = 34
I have handled permission in info.plist like this -->
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>'Description'</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>'Description'</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>'Description'</string>
<key>NSBluetoothAlwaysUsageDescription</key>
<string>Description</string>
code is like this -->
const ScanBeacons: React.FC = () => {
const [beaconsCount, setBeaconsCount] = useState(0);
const dispatch = useDispatch();
useEffect(() => {
Promise.resolve().then(beaconSetup);
return () => {
// remove event listeners
if (isAndroid) {
kontaktEmitter.removeAllListeners('beaconsDidUpdate');
} else {
kontaktEmitter.removeAllListeners('didDiscoverDevices');
kontaktEmitter.removeAllListeners('didRangeBeacons');
}
};
}, []);
const beaconSetup = async () => {
if (isAndroid) {
// Android
const granted = await requestLocationPermission();
if (granted) {
await connect();
await startScanning();
} else {
Alert.alert(
'Permission error',
'Location permission not granted. Cannot scan for beacons',
[{text: 'OK', onPress: () => console.log('OK Pressed')}],
{cancelable: false},
);
}
} else {
// iOS
await init()
.then(() => startDiscovery())
.catch((error: Error) => Alert.alert('error', error.message));
/**
* Will discover Kontakt.io beacons only
*/
await startDiscovery();
/**
* Works with any beacon(also virtual beacon, e.g. https://github.com/timd/MactsAsBeacon)
* Requires user to allow GPS Location (at least while in use)
*
* change to match your beacon values
*/
await startRangingBeaconsInRegion({
identifier: '',
// uuid: 'A4826DE4-1EA9-4E47-8321-CB7A61E4667E',
uuid: 'my uuid', // my beacon uuid
major: 1,
minor: 34,
});
}
// Add beacon listener
if (isAndroid) {
/* works with any beacon */
DeviceEventEmitter.addListener(
'beaconsDidUpdate',
async ({ beacons, region }) => {
setBeaconsCount(beacons?.length);
if(beacons?.length > 0){
dispatch(
setNearestBeacons({nearestBeacon: beacons[0]}),
);
if (isAndroid) {
kontaktEmitter.removeAllListeners('beaconsDidUpdate');
} else {
kontaktEmitter.removeAllListeners('didDiscoverDevices');
kontaktEmitter.removeAllListeners('didRangeBeacons');
}
}
},
);
} else {
const nearestPosition = await checkNearestPosition();
/* works with Kontakt.io beacons only */
kontaktEmitter.addListener('didDiscoverDevices', ({ beacons }) => {
});
/* works with any beacon */
kontaktEmitter.addListener('didRangeBeacons', ({ beacons, region }) => {
});
}
};
return (
<SafeAreaView>
<StatusBar barStyle="dark-content" />
<View style={styles.wrapper}>
<Text style={styles.title}>react-native-kontaktio Example</Text>
<Text>{`Check console.log statements (connected beacons count: ${beaconsCount})`}</Text>
</View>
</SafeAreaView>
);
};
Upvotes: 0
Views: 40