react-native-beacons-manager icon indicating copy to clipboard operation
react-native-beacons-manager copied to clipboard

Unable to detect the beacons

Open Srikanth-Enuguru opened this issue 11 months ago • 9 comments

Version

1.2.9

Platform

Android

OS version

android 12

Steps to reproduce

import Beacons from '@mindsoftcreative/react-native-beacons-manager';

function App(): JSX.Element {
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const [state, setState] = useState([]);

  useEffect(() => {
    requestLocationPermission();
    Beacons.init();
    Beacons.detectIBeacons();

    const beaconsServiceDidConnect = Beacons.BeaconsEventEmitter.addListener(
      'beaconServiceConnected',
      () => {
        console.log('service connected');
        startRangingAndMonitoring();
      },
    );

    const beaconsDidRange = DeviceEventEmitter.addListener(
      'beaconsDidRange',
      data => {
        console.log('beaconsDidRange beacons!', data);
      },
    );

    const regionDidEnter = DeviceEventEmitter.addListener(
      'regionDidEnter',
      data => {
        console.log('regionDidEnter beacons!', data);
      },
    );

    return () => {
      console.debug('[app] main component unmounting. Removing listeners...');
      stopRangingAndMonitoring();
      beaconsServiceDidConnect.remove();
      beaconsDidRange.remove();
      regionDidEnter.remove();
    };
  }, []);

  const startRangingAndMonitoring = async () => {
    try {
      const data1 = await Beacons.startRangingBeaconsInRegion('REGION3');
      console.log('Beacons ranging started successfully', data1);
      const data2 = await Beacons.startMonitoringForRegion('REGION3');
      console.log('Beacons monitoring started successfully', data2);
    } catch (error) {
      console.log('error', error);
      throw error;
    }
  };

  const stopRangingAndMonitoring = async () => {
    try {
      await Beacons.stopRangingBeaconsInRegion('REGION3');
      console.log('Beacons ranging stopped successfully');
      await Beacons.stopMonitoringForRegion('REGION3');
      console.log('Beacons monitoring stopped successfully');
    } catch (error) {
      throw error;
    }
  };

  const requestLocationPermission = async () => {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        {
          title: 'Location Permission',
          message:
            'This example app needs to access your location in order to use bluetooth beacons.',
          buttonNeutral: 'Ask Me Later',
          buttonNegative: 'Cancel',
          buttonPositive: 'OK',
        },
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        return true;
      } else {
        // permission denied
        return false;
      }
    } catch (err) {
      console.warn(err);
      return false;
    }
  };

  

  return (
    <SafeAreaView style={styles.sectionContainer}>
      <View>
        <Text>Hello</Text>
      </View>
    </SafeAreaView>
  );
}

Actual behavior

Console output: Found beacons! {"beacons": [], "identifier": "REGION3", "uuid": ""}

Please help me. Am I missing anything here?

Srikanth-Enuguru avatar Jul 26 '23 15:07 Srikanth-Enuguru

I don't have android 12 device, to test but since this library is based on android beacon library, check their docs regarding necessary changes for android 12+.

Another thing, for beacons ranging to work, you have to enable the device's location

sashko9807 avatar Jul 27 '23 07:07 sashko9807

@sashko9807 Thank you for the response. I already added the permissions in manifest file.

I enabled device's location also. But still unable to detect the beacons.

Srikanth-Enuguru avatar Jul 27 '23 08:07 Srikanth-Enuguru

@sashko9807 Thank you for the response. I already added the permissions in manifest file.

I enabled device's location also. But still unable to detect the beacons.

Has the location permission been granted at runtime(e.g. prompting user to grant location permission when landing the page)? Other than that, I have no idea, why it doesn't work.

In summary the flow of my usage is this: User opens the screen, which ranges for beacon -> Prompt user to grant location permission(if not granted already) -> prompt user to turn on location(if turned off) -> Start ranging for beacons.

sashko9807 avatar Jul 27 '23 09:07 sashko9807

@sashko9807 Yes. Location permission is being granted at runtime only. Please refer to the below screenshots.

I observed this error in console: error uniqueId may not be null

WhatsApp Image 2023-07-27 at 3 17 56 PM 2-4f6d-958b-f0711fa96e58)

WhatsApp Image 2023-07-27 at 3 17 57 PM

Srikanth-Enuguru avatar Jul 27 '23 09:07 Srikanth-Enuguru

@sashko9807 Yes. Location permission is being granted at runtime only. Please refer to the below screenshots.

I observed this error in console: error uniqueId may not be null

Try updating the altbeacon library to a version 2.19.5 or 2.19.6, if it is not updated already. You can do that from node_modules/@mindsoftcreative/react-native-beacons-manager/android/build.gradle Then

dependencies {
     ....
    implementation 'org.altbeacon:android-beacon-library:2.19.5'
}

If that doesn't work, I am out of ideas

sashko9807 avatar Jul 27 '23 10:07 sashko9807

@sashko9807 I tried after updating the altbeacon library to a version 2.19.5. Still I'm facing the same issue. beacons are detected.

{"beacons": [], "identifier": "REGION3", "uuid": ""}

Srikanth-Enuguru avatar Jul 27 '23 10:07 Srikanth-Enuguru

@MacKentoch @mozart27 @Jake-Young @dmontecillo Please help me in fixing this issue.

Srikanth-Enuguru avatar Jul 27 '23 11:07 Srikanth-Enuguru

@Srikanth-Enuguru this helped me!

interstates21 avatar Sep 18 '23 23:09 interstates21

I am recommending to use https://github.com/JanSneeuw/react-native-beacon-radar

yasintz avatar Jan 06 '24 18:01 yasintz