Altbeacon 库:找不到信标

Altbeacon Library: can't find beacons

我已经使用了教程和 Android 信标库(示例代码)。 不幸的是,我的应用程序找不到我的 Beacons (Blueup)。 不知道是不是我的信标配置有误。

我用我的 Beacon 的 ID 测试了所有不同的 Beaconparser 表达式。最后,我决定将 Identifier.parse("...") 更改为 null,以查看所有信标。

因为我使用 Android 6+ 和 SDK 23,所以我添加了 ACCESS_COARSE_LOCATION 和蓝牙权限。

这是我收到的消息:"MonitoringActivity: I have just switched from seeing/not seeing beacons: 0"。

这是我的代码:

package com.example.smi.beacon03;

import android.app.Activity;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.Identifier;
import org.altbeacon.beacon.MonitorNotifier;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;

import java.util.Collection;

public class BeaconMainActivity extends Activity implements BeaconConsumer {

protected static final String TAG = "MonitoringActivity";
protected static final String TAG2 = "RangingActivity";
public static final String ALTBEACON = "m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25";
public static final String ALTBEACON2 = "m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25";
public static final String EDDYSTONE_TLM =  "x,s:0-1=feaa,m:2-2=20,d:3-3,d:4-5,d:6-7,d:8-11,d:12-15";
public static final String EDDYSTONE_UID = "s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19";
public static final String EDDYSTONE_URL = "s:0-1=feaa,m:2-2=10,p:3-3:-41,i:4-20v";
public static final String IBEACON = "m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24";


private BeaconManager beaconManager;

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

    beaconManager = BeaconManager.getInstanceForApplication(this);

    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.URI_BEACON_LAYOUT));

    beaconManager.bind(this);
}

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

@Override
public void onBeaconServiceConnect() {

    beaconManager.addMonitorNotifier(new MonitorNotifier() {
        @Override
        public void didEnterRegion(Region region) {
            Log.i(TAG, "I just saw an beacon for the first time!");
        }

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

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

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

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

@Override
public void onPointerCaptureChanged(boolean hasCapture) {

}
}

您必须在 class 的顶部添加所有已初始化的信标布局。 所以做这样的事情:

beaconManager.getBeaconParsers().add(BeaconParser().setBeaconLayout(ALTBEACON);
beaconManager.getBeaconParsers().add(BeaconParser().setBeaconLayout(ALTBEACON2);
beaconManager.getBeaconParsers().add(BeaconParser().setBeaconLayout(EDDYSTONE_TLM);
beaconManager.getBeaconParsers().add(BeaconParser().setBeaconLayout(EDDYSTONE_UID);
beaconManager.getBeaconParsers().add(BeaconParser().setBeaconLayout(EDDYSTONE_URL);
beaconManager.getBeaconParsers().add(BeaconParser().setBeaconLayout(IBEACON);