Android 8上的AltBeacon第一次运行时速度慢(应用程序没有响应)
只有在第一次启动应用程序(卸载/安装后),在它开始扫描信标后,才会出现“应用程序没有响应”的消息,仅出现在Android 8(三星A5和J5 Prime)上。删除了应用程序中的所有内容,只留下用于信标检测的代码,仍在发生。 我不确定这是图书馆的问题还是我使用它的方式的问题 从我的应用程序类调用BeaconConsumer(上下文)Android 8上的AltBeacon第一次运行时速度慢(应用程序没有响应),android,altbeacon,Android,Altbeacon,只有在第一次启动应用程序(卸载/安装后),在它开始扫描信标后,才会出现“应用程序没有响应”的消息,仅出现在Android 8(三星A5和J5 Prime)上。删除了应用程序中的所有内容,只留下用于信标检测的代码,仍在发生。 我不确定这是图书馆的问题还是我使用它的方式的问题 从我的应用程序类调用BeaconConsumer(上下文) public class BeaconConsumer implements BootstrapNotifier, org.altbeacon.beacon.Beac
public class BeaconConsumer implements BootstrapNotifier, org.altbeacon.beacon.BeaconConsumer {
BeaconManager beaconManager;
BeaconConsumer(Context applicationContext) {
beaconManager = BeaconManager.getInstanceForApplication(applicationContext);
beaconManager.getBeaconParsers().clear();
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=6548,i:4-25,p:1-1"));
beaconManager.setEnableScheduledScanJobs(false);
beaconManager.setBackgroundBetweenScanPeriod(15300);
beaconManager.setBackgroundScanPeriod(1200);
beaconManager.setForegroundBetweenScanPeriod(10000);
beaconManager.setForegroundScanPeriod(1200);
beaconManager.bind(this);
beaconManager.setBackgroundMode(false);
BeaconManager.setDebug(true);
Region region = new Region("backgroundRegion", null, null, null);
RegionBootstrap regionBootstrap = new RegionBootstrap(this, region);
}
public void onBeaconServiceConnect() {
beaconManager.removeAllRangeNotifiers();
beaconManager.addRangeNotifier((beacons, region) -> {
Log.d("TAG", "didRangeBeaconsInRegion CALLED with size: " + beacons.size() + " on region: " + region);
Iterator<Beacon> iterator = beacons.iterator();
while (iterator.hasNext()) {
Beacon beacon = iterator.next();
Formatter formatter = new Formatter();
for (byte b : beacon.getId1().toByteArray()) {
formatter.format("%02x", b);
}
String payload = formatter.toString();
Log.d("TAG", "Processing payload: " + payload + " rssi: " + beacon.getRssi());
}
});
try {
beaconManager.startRangingBeaconsInRegion(myRegion);
} catch (RemoteException e) {
Log.d("TAG", "Exception starting ranging beacons in region");
}
}
public void didEnterRegion(Region region) {
try {
beaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
Log.d("TAG", "Can't start ranging");
}
Intent intent = new Intent(mContext, BeaconService.class);
intent.setAction("com.test.altbeacontest.beaconreference.Service.action.Scan");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mContext.startForegroundService(intent);
} else {
mContext.startService(intent);
}
mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("TAG", "ServiceConnection - connected");
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d("TAG", "ServiceConnection - disconnected");
}
};
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
public void didExitRegion(Region region) {
Log.d("TAG", "I no longer see a beacon.");
insideRegion = false;
try {
beaconManager.stopRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
公共类BeaconConsumer实现BootstrapNotifier,org.altbeacon.beacon.BeaconConsumer{
BeaconManager BeaconManager;
BeaconConsumer(上下文应用程序上下文){
beaconManager=beaconManager.getInstanceForApplication(applicationContext);
beaconManager.getBeaconParsers().clear();
beaconManager.getBeaconParsers().add(新的BeaconParser().setBeaconLayout(“m:2-3=6548,i:4-25,p:1-1”);
beaconManager.setEnableScheduledScanJobs(false);
在扫描期间(15300)之间的beaconManager.backbackground;
beaconManager.setbackgroundscanneperiod(1200);
信标管理器。在扫描周期(10000)之间设置foreground;
beaconManager.setForegroundScanPeriod(1200);
beaconManager.bind(这个);
beaconManager.setBackgroundMode(假);
BeaconManager.setDebug(true);
区域区域=新区域(“背景区域”,null,null,null);
RegionBootstrap RegionBootstrap=新的RegionBootstrap(此,区域);
}
beacerviceconnect()上的公共无效{
beaconManager.removeAllRangeNotifiers();
beaconManager.addRangeNotifier((信标,区域)->{
Log.d(“TAG”,“didRangeBeaconsisnRegion”调用,区域:“+region”上的大小:“+beacons.size()+”);
迭代器迭代器=beacons.Iterator();
while(iterator.hasNext()){
信标=iterator.next();
格式化程序格式化程序=新格式化程序();
用于(字节b:beacon.getId1().toByteArray()){
格式化程序。格式(“%02x”,b);
}
字符串负载=格式化程序.toString();
Log.d(“TAG”,“Processing payload:”+payload+“rssi:”+beacon.getRssi());
}
});
试一试{
StarTrangBeaconRegion(我的地区)的beaconManager;
}捕获(远程异常){
Log.d(“标记”,“区域内开始测距的异常信标”);
}
}
公共区域(区域){
试一试{
StarTrangBeaconRegion(地区)信标管理人;
}捕获(远程异常){
Log.d(“标记”,“无法开始测距”);
}
意向意向=新意向(mContext,beacervice.class);
intent.setAction(“com.test.altbeacety.beaconreference.Service.action.Scan”);
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O){
mContext.startForegroundService(intent);
}否则{
mContext.startService(意图);
}
mConnection=newserviceconnection(){
@凌驾
服务连接上的公共无效(组件名称,IBinder服务){
Log.d(“标记”,“服务连接-已连接”);
}
@凌驾
ServiceDisconnected上的公共无效(组件名称){
Log.d(“标记”,“服务连接-断开”);
}
};
bindService(intent、mConnection、Context.BIND\u AUTO\u CREATE);
}
公共区域(区域){
Log.d(“TAG”,“我再也看不到灯塔了。”);
内部区域=错误;
试一试{
站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长;
}捕获(远程异常){
e、 printStackTrace();
}
}
}
我怀疑问题与以相同方法一起使用的这些操作有关:
beaconManager.bind(this);
...
RegionBootstrap regionBootstrap = new RegionBootstrap(this, region);
您真的不应该同时使用这两者——通常使用其中一种(绑定到BeaconManager)或使用RegionBootstrap。后一个对象本身在内部绑定
我将删除:RegionBootstrap RegionBootstrap=newregionbootstrap(这个,region)代码>并改为添加beaconManager.startMonitoringBeaconsInRegion(区域)代码>同时调用beaconManager.StartTrangBeaconsisnRegion(myRegion)代码>