Ì 正在尝试与我的 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);
}
要检查的两件事:
确保您使用正确的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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句