Altbeacon - Neither monitoring or ranging is working with no error

688 Views Asked by At

My code is as follows (taken from here)

public class MainActivity extends ActionBarActivity implements ROXIMITYEngineListener, BeaconConsumer {
    private final String TAG = "MainActivity";

    private BeaconManager beaconManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        beaconManager = BeaconManager.getInstanceForApplication(this.getApplicationContext());
        beaconManager.bind(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        beaconManager.unbind(this);
    }

    @Override
    public void onBeaconServiceConnect() {
        Log.i(TAG, "onBeaconServiceConnect");

        beaconManager.setMonitorNotifier(new MonitorNotifier() {
            @Override
            public void didEnterRegion(Region region) {
                Log.i(TAG, "I just saw an beacon for the first time!");
                Toast.makeText(MainActivity.this, "I just saw an beacon for the first time!", Toast.LENGTH_LONG).show();
            }

            @Override
            public void didExitRegion(Region region) {
                Log.i(TAG, "I no longer see an beacon");
                Toast.makeText(MainActivity.this, "I no longer see an beacon", Toast.LENGTH_LONG).show();
            }

            @Override
            public void didDetermineStateForRegion(int state, Region region) {
                Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+state);
                Toast.makeText(MainActivity.this, "I have just switched from seeing/not seeing beacons: "+state, Toast.LENGTH_LONG).show();
            }
        });

        beaconManager.setRangeNotifier(new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                if (beacons.size() > 0) {
                    Log.i(TAG, "The first beacon I see is about " + beacons.iterator().next().getDistance() + " meters away.");
                    Toast.makeText(MainActivity.this, "The first beacon I see is about " + beacons.iterator().next().getDistance() + " meters away.", Toast.LENGTH_LONG).show();
                }
            }
        });

        try {
            beaconManager.startMonitoringBeaconsInRegion(new Region("uniqueid1", null, null, null));
            beaconManager.startRangingBeaconsInRegion(new Region("uniqueid2", null, null, null));
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
        }
    }
}

onBeaconServiceConnected was indeed called! But nothing else was called/ printed. No error was thrown.
What am I missing?

This app by the same people (Radius Networks) can detect my beacon so why can't my app do the same?

My phone is Nexus 5 running Android 5.1. I'm using the Android Beacon Library 2.1.4 and I'm using Android Studio version 1.2.1.1.
(I don't think it matters but my beacon is Roximity Model X)

I've also tried doing only monitoring but it doesn't either.

Interestingly, when I look at the logcat with no filter, I can see BtGatt doing the scan and detect the beacon:

06-11 17:06:01.413    2828-2922/? D/BtGatt.ScanManager﹕ configureRegularScanParams() - queue=1
06-11 17:06:01.413    2828-2922/? D/BtGatt.ScanManager﹕ configureRegularScanParams() - ScanSetting Scan mode=2 mLastConfiguredScanSetting=-2147483648
06-11 17:06:02.515    2828-2845/? D/BtGatt.GattService﹕ stopScan() - queue size =1
06-11 17:06:02.517    2828-2922/? D/BtGatt.ScanManager﹕ stop scan
06-11 17:06:02.520    2828-2922/? D/BtGatt.ScanManager﹕ configureRegularScanParams() - queue=0
06-11 17:06:02.520    2828-2922/? D/BtGatt.ScanManager﹕ configureRegularScanParams() - ScanSetting Scan mode=-2147483648 mLastConfiguredScanSetting=2
06-11 17:06:02.520    2828-2922/? D/BtGatt.ScanManager﹕ configureRegularScanParams() - queue emtpy, scan stopped
06-11 17:06:02.521    2828-3062/? D/BtGatt.GattService﹕ unregisterClient() - clientIf=5
06-11 17:06:02.527    2828-3062/? D/BtGatt.GattService﹕ registerClient() - UUID=a6ef652b-0423-4049-8079-8c784d8a22ec
06-11 17:06:02.527    2828-2900/? D/BtGatt.GattService﹕ onClientRegistered() - UUID=a6ef652b-0423-4049-8079-8c784d8a22ec, clientIf=5
06-11 17:06:02.527    2902-2937/? D/BluetoothLeScanner﹕ onClientRegistered() - status=0 clientIf=5
06-11 17:06:02.529    2828-2905/? D/BtGatt.GattService﹕ start scan with filters
06-11 17:06:02.530    2828-2922/? D/BtGatt.ScanManager﹕ handling starting scan
06-11 17:06:02.532    2828-2922/? D/BtGatt.ScanManager﹕ configureRegularScanParams() - queue=1
06-11 17:06:02.532    2828-2922/? D/BtGatt.ScanManager﹕ configureRegularScanParams() - ScanSetting Scan mode=2 mLastConfiguredScanSetting=-2147483648
06-11 17:06:03.634    2828-2845/? D/BtGatt.GattService﹕ stopScan() - queue size =1
06-11 17:06:03.636    2828-2922/? D/BtGatt.ScanManager﹕ stop scan
3

There are 3 best solutions below

4
On BEST ANSWER

My guess is that you are trying to detect a proprietary beacon (e.g. an iBeacon). Because the library is open source, it will not detect proprietary beacons by default because doing so would require us to publish the proprietary beacon format. That can cause legal problems.

The fix is easy. You just need to add one line of code to your onCreate method that sets the beacon parser needed for your proprietary beacon type.

Do a Google search for "getbeaconparsers" and you will see the line of code to add.

0
On
    private BeaconManager beaconManager;
    @Override
    protected void onCreate(Bundle savedInstanceState {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
        beaconManager = BeaconManager.getInstanceForApplication(this);
//layout of ibeacon        
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
        ...
    }
3
On

Your monitoring and ranging region name should be same. Try this :

beaconManager.startMonitoringBeaconsInRegion(new Region("uniqueid1", null, null, null));
beaconManager.startRangingBeaconsInRegion(new Region("uniqueid1", null, null, null));