Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to detect the beacons #266

Open
Srikanth-Enuguru opened this issue Jul 26, 2023 · 9 comments
Open

Unable to detect the beacons #266

Srikanth-Enuguru opened this issue Jul 26, 2023 · 9 comments

Comments

@Srikanth-Enuguru
Copy link

Srikanth-Enuguru commented Jul 26, 2023

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?

@sashko9807
Copy link

sashko9807 commented Jul 27, 2023

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

@Srikanth-Enuguru
Copy link
Author

Srikanth-Enuguru commented Jul 27, 2023

@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.

@sashko9807
Copy link

@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.

@Srikanth-Enuguru
Copy link
Author

Srikanth-Enuguru commented Jul 27, 2023

@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

@sashko9807
Copy link

@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

@Srikanth-Enuguru
Copy link
Author

@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
Copy link
Author

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

@interstates21
Copy link

@Srikanth-Enuguru this helped me!

@yasintz
Copy link

yasintz commented Jan 6, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants