Altbeacon - 监控或测距都没有错误

Altbeacon - Neither monitoring or ranging is working with no error

我的代码如下(摘自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确实被调用了!但没有别的被调用/打印。没有抛出任何错误。
我错过了什么?

同一个人 (Radius Networks) 开发的这个 app 可以检测到我的信标,为什么我的应用程序不能检测到我的信标?

我的 phone 是 Nexus 5 运行 Android 5.1。我正在使用 Android Beacon Library 2.1.4,我正在使用 Android Studio 1.2.1.1.
(我认为这不重要,但我的信标是 Roximity Model X)

我也试过只做监控,但也没有。

有趣的是,当我查看没有过滤器的 logcat 时,我可以看到 BtGatt 正在扫描并检测到信标:

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

您的监控和测距区域名称应该相同。 试试这个:

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

我的猜测是您正在尝试检测专有信标(例如 iBeacon)。由于该库是开源的,默认情况下它不会检测专有信标,因为这样做需要我们发布专有信标格式。这可能会导致法律问题。

修复很简单。您只需在 onCreate 方法中添加一行代码,即可设置专有信标类型所需的信标解析器。

Google 搜索 "getbeaconparsers",您将看到要添加的代码行。

    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"));
        ...
    }