我正在使用Android N设备(Moto G第5代)测试应用程序,但它找不到任何信标(beacons.size()== 0总是),但是其他具有较低API的设备工作正常.. 。只有这台设备失败。
我检查了扫描限制,而不是谷歌在Android N中添加的:
我们已经改变了从DP4开始的BLE扫描行为。我们会阻止 30秒内启动和停止扫描的应用程序超过5次 秒。对于长时间运行的扫描,我们会将它们转换为机会主义 扫描。
这是代码:
public void startBeaconScan() {
Log.d(TAG, "App started up");
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(iBeaconLayout));
long timeBetweenScans = 1100;
long timeScanPeriod = 500;
if(Build.VERSION.SDK_INT > 23){ //CHECK IF NOUGAT OR MORE
timeBetweenScans = 5000;
timeScanPeriod = 15000;
}
beaconManager.setForegroundBetweenScanPeriod(timeBetweenScans);
beaconManager.setForegroundScanPeriod(timeScanPeriod);
beaconManager.setBackgroundBetweenScanPeriod(timeBetweenScans);
beaconManager.setBackgroundScanPeriod(timeScanPeriod);
beaconManager.setRegionStatePeristenceEnabled(true);
region = new Region("myMonitoringUniqueId", Identifier.parse("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6"), null, null);
new RegionBootstrap(this, region);
}
从BootstrapNotifier接口的Application的didDetermineStateForRegion()回调方法调用此函数。
知道为什么我遇到这个问题?使用Android N,我只测试过Moto G 5th ......但是对于其他设备(Android L,Android M ......),代码运行正常。
-----更新-----
Logcat与应用程序开始:
06-01 15:51:05.183 12251-12251/? D/MyApplication: App started up
06-01 15:51:05.187 12251-12251/? D/BeaconParser: Parsing beacon layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
06-01 15:51:05.189 12251-12251/? D/BeaconParser: Parsing beacon layout: m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
06-01 15:51:05.200 21464-4646/? I/PBSessionCacheImpl: Deleted sessionId[4000692901285] from persistence.
06-01 15:51:05.202 21464-4534/? V/ConnectivityManager: isActiveNetworkMetered() returns:false
06-01 15:51:05.207 21464-4534/? V/ConnectivityManager: isActiveNetworkMetered() returns:false
06-01 15:51:05.211 21464-21464/? W/SearchService: Abort, client detached.
06-01 15:51:05.216 21464-5529/? E/ContentStoreEUAS: Failed to commit the deferred actions
06-01 15:51:05.351 12251-12267/? D/NetworkSecurityConfig: No Network Security Config specified, using platform default
06-01 15:51:05.366 12251-12251/? W/BluetoothCrashResolver: Can't read macs from BluetoothCrashResolverState.txt
06-01 15:51:05.371 12251-12251/? W/ModelSpecificDistanceCalculator: Cannot find match for this device. Using default
06-01 15:51:05.371 12251-12251/? W/ModelSpecificDistanceCalculator: Cannot find match for this device. Using default
06-01 15:56:17.210 16969-16969/? D/MyApplication: Got a didDetermineStateForRegion call: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
答案 0 :(得分:1)
必须在设置中启用Android Marshmallow(6.0)位置才能检测蓝牙LE设备。这适用于蓝牙信标以及其他类型的蓝牙LE设备。 Google进行了此更改,因为扫描蓝牙LE设备可能会用于推断您的位置。同样的限制不适用于6.0之前的设备。
一种可能的解决方案是在6.0+设备上detect if location is off并提示用户将其打开。
值得注意的是,如果您的应用针对SDK 23+,则还必须获取android.permission.ACCESS_FINE_LOCATION或android.permission.ACCESS_COARSE_LOCATION的运行时权限。 See here for more info.