我想在我的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);
}
答案 0 :(得分: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/
如果您在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