找不到带有 altbeacon 库的信标

Can't find beacon with altbeacon library

Ì 正在尝试连接我的 android 应用程序中的信标。但我似乎找不到我的信标。我正在使用 Ibeacons。我正在使用 AltBeacon 库。 onBeaconServiceConnect 开始,然后是 didDetermineStateForRegion。但是 didEnterReion 永远不会被调用。这是我的代码:

public class ListScenarios extends AppCompatActivity implements BeaconConsumer, MonitorNotifier {

private static final String TAG = "ListScenarios";

private ListView listView;
public String persoonID;
public String adresID;

public Array beaconArray;
//private ArrayList<IBeacon> arrBeacons = new ArrayList<>();

private BeaconManager mBeaconManager;


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

    // Setup beaconmanager
    mBeaconManager = BeaconManager.getInstanceForApplication(this.getApplicationContext());
    // Detect iBeacon
    mBeaconManager.getBeaconParsers().add(new BeaconParser()
            .setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
    mBeaconManager.bind(this);



    //listview
    listView = (ListView) findViewById(R.id.list_scenario);

    //send request to load list
    getScenarios();
    getBeacons();



}


// MARK: - Bluetooth


@Override
public void onBeaconServiceConnect() {
    System.out.println("We are in onBeaconServiceConnect");
    // Set the two identifiers below to null to detect any beacon regardless of identifiers
    Identifier myBeaconNamespaceId = null;
    Identifier myBeaconInstanceId = null;
    Region region = new Region("my-beacon-region", myBeaconNamespaceId, myBeaconInstanceId, null);
    mBeaconManager.addMonitorNotifier(this);
    try {
        mBeaconManager.startMonitoringBeaconsInRegion(region);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

public void didEnterRegion(Region region) {
    System.out.println("We are in didEnterRegion");
    Log.d(TAG, "I detected a beacon in the region with namespace id " + region.getId1() +
            " and instance id: " + region.getId2());
}

public void didExitRegion(Region region) {
    System.out.println("We are in didExitRegion");
}

public void didDetermineStateForRegion(int state, Region region) {
    System.out.println("We are in didDetermineStateForRegion");
}

@Override
public void onPause() {
    super.onPause();
    mBeaconManager.unbind(this);
}

要检查两件事:

  1. 确保您使用了正确的 BeaconParser 表达式。问题中显示的表达式是针对 AltBeacon 的:"m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25。如果您尝试检测 iBeacon,则需要使用不同的表达式。这个网站有一个方便的参考:https://beaconlayout.wordpress.com/

  2. 如果您 运行 您的应用在 Android 6+ 上并且目标是 SDK 23 或更高版本,您需要为您的应用动态请求位置权限。如果您不这样做,您将无法检测到任何信息,您将在日志中看到:04-22 22:35:20.152 5158 5254 E BluetoothUtils: Permission denial: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results 您可以在此处阅读如何执行此操作:http://altbeacon.github.io/android-beacon-library/requesting_permission.html