0

I'm using the standard example provided here, except I modified it to log that if nothing is found to make sure it's actually called.

@Override
public void onBeaconServiceConnect() {
    beaconManager.addRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            if (beacons.size() == 0) {
                Log.i("ActivityMain", "no beacons found");
            }
            if (beacons.size() > 0) {
                Log.i("ActivityMain", "The first beacon I see is about " + beacons.iterator().next().getDistance() + " meters away.");
            }
        }
    });

    try {
        beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

I have tried adding several additional parsers before binding and I have tried scanning without adding them:

beaconManager.getBeaconParsers().add(new BeaconParser(BeaconParser.ALTBEACON_LAYOUT));
    beaconManager.getBeaconParsers().add(new BeaconParser(BeaconParser.EDDYSTONE_TLM_LAYOUT));
    beaconManager.getBeaconParsers().add(new BeaconParser(BeaconParser.EDDYSTONE_UID_LAYOUT));
    beaconManager.getBeaconParsers().add(new BeaconParser(BeaconParser.EDDYSTONE_URL_LAYOUT));
    beaconManager.getBeaconParsers().add(new BeaconParser(BeaconParser.URI_BEACON_LAYOUT));
    beaconManager.getBeaconParsers().add(new BeaconParser().
            setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));

    beaconManager.bind(this);

I have specified the following permissions in the manifest:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

And there are multiple beacons in my room right now, both Eddystone and iBeacon. None of them are found after calling startRangingBeaconsInRegion(...)

D/BluetoothLeScanner: could not find callback wrapper shows up in logcat quite a lot, if that is any indicator. The thing is the callback seems to be found, because I know for sure that this piece of code

if (beacons.size() == 0) {
                    Log.i("ActivityMain", "no beacons found");
                }

is being executed once a second. I've tried this app Beacon Scanner which also uses AltBeacon and finds my beacons just fine. I've checked its source and it doesn't like there's anything going on that I'm not doing myself, albeit the other app is using kotlin and I'm using java.

I've tried doing this on both a Moto G3 and Galaxy S7 and had no luck with either of them.

What could be the cause of this?

user3340459
  • 435
  • 5
  • 13
  • Make sure you have successfully obtained location permission dynamically as described in my answer here: https://stackoverflow.com/a/33150570 – davidgyoung Jan 09 '18 at 03:41
  • @davidgyoung I have implemented your suggestion for both coarse and fine location permissions. LogCat now shows a new line every second: `D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5`, however `didRangeBeaconsInRegion` still returns an empty list every time. – user3340459 Jan 09 '18 at 12:49
  • To be clear, the location permission must be obtained dynamically by causing the operating system to pop a dialog to the user and having them actively click to agree to consent to letting the app use location. You can check if this has been successfully accomplished by the app by going to Settings -> Apps -> [Your App Name] -> Permissions and checking to see that the Location switch is in the on position. You also need to make sure the global phone location setting is in the on position at the top of Settings -> Location. – davidgyoung Jan 09 '18 at 17:00
  • @davidgyoungSettings -> Apps -> [My App] shows the location switch in the on position. Settings -> Location is turned on, mode says "high accuracy". My app is not listed under "recent location requests", but neither are any of the other beacon scanning apps I've tried. – user3340459 Jan 09 '18 at 17:24
  • OK, I agree the permissions sound like they are set properly now. I'm struggling to think what else might be wrong... are you *sure* that the Beacon Scanner app can detect beacons right now? It's possible that the bluetooth stack is in a bad state that might require a bluetooth on/off cycle, but if that is the case, it should be true for the Beacon Scanner app as well. – davidgyoung Jan 09 '18 at 21:32

0 Answers0