我正在开发一个应用程序,用户只有在打开移动应用程序时才会登录到该位置。我跟踪了https://altbeacon.github.io/android-beacon-library/samples.html处的示例代码,只有当用户单击MainActivity上的后退按钮并再次打开应用程序时,才会设置扫描信号。尽管在没有重新启动的情况下设备非常靠近设备,但应用无法检测到信标。
这是我的MainActivity代码:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, BeaconConsumer {
BeaconManager beaconManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// some code //
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.bind(this);
}
public void onBeaconServiceConnect() {
final String module_code = "FI 3011_ITO P02"; // TODO: replace with latest lecture
final UUID uuid = UUID.nameUUIDFromBytes(module_code.getBytes());
beaconManager.addRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {
for (Beacon beacon : collection) {
// Do something
}
}
});
try {
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
Region region = new Region(uuid.toString(), Identifier.fromUuid(uuid), Identifier.fromInt(1), Identifier.fromInt(2));
beaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (beaconManager.isBound(this)) beaconManager.unbind(this);
}
}
有人可以建议我如何解决这个问题?
祝你好运