2017-06-01 70 views
0

我使用Android N設備(Moto G第5代)測試應用程序,並且始終未找到任何信標(beacons.size()== 0),但與其他具有較低API的設備工作正常......只能在此設備上失敗。掃描信標不適用於Android N設備

我檢查掃描限制比谷歌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界面應用程序的didDetermineStateForRegion()回調方法調用。

任何想法,爲什麼我有這個問題?使用Android N,我只測試過Moto G 5th ...但是通過其他設備(Android L,Android M ...),代碼正常工作。

----- ----- UPDATE

的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

你使用的是什麼庫版本?當掃描開始查看OS是否阻止掃描時,捕獲LogCat摘錄(包括系統日誌)可能會有所幫助。庫版本2.9+會根據Android N的需要自動減慢掃描週期。原因可能與您的想法不同。 – davidgyoung

+0

我使用的是版本2.9.2,但是如果我沒有檢查版本,這條消息會在logcat上出現好幾次:「應用程序掃描得太頻繁」。信標集合總是大小爲0.我用logcat中的一些消息編輯了帖子。 – adri1992

+0

哇...我發現了這個問題。這是具有中心角色的設備的位置設置被禁用...我可怕的錯誤。你能否回答解釋爲什麼位置設置(不僅僅是清單上的權限)應該被激活,以及是否可以避免?有沒有辦法讓信標掃描在沒有啓用位置設置的情況下工作? – adri1992

回答

1

與Android棉花糖(6.0)的位置必須設置以檢測接通啓動藍牙LE設備。這適用於藍牙信標以及其他類型的藍牙LE設備。谷歌做出了這一改變,因爲掃描藍牙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.