2016-03-14 88 views
1

這裏是我用來檢測進入區域的事件示例代碼:AltBeacon圖書館後臺服務

public class BeaconApplication extends android.app.Application implements BootstrapNotifier { 

    private static final String TAG = "TAGTAG"; 

    @Override public void onCreate() { 
     super.onCreate(); 
     Log.d(TAG, "App started up"); 
     new BackgroundPowerSaver(this); 
     new RegionBootstrap(this, new Region(getPackageName(), null, null, null)); 
    } 

    @Override public void didDetermineStateForRegion(int arg0, Region arg1) { 
     Log.d(TAG, "didDetermineStateForRegion"); 
    } 

    @Override public void didEnterRegion(Region arg0) { 
     Log.d(TAG, "didEnterRegion"); 
    } 

    @Override public void didExitRegion(Region arg0) { 
     Log.d(TAG, "didExitRegion"); 
    } 
} 

,如果我用下面的build.gradle配置,然後一切正常像預期中的問題

compileSdkVersion 21 
    buildToolsVersion "21.0.0" 
    defaultConfig { 
     applicationId "com.test" 
     minSdkVersion 18 
     targetSdkVersion 21 
     versionCode 1 
     versionName "1.0" 
    } 

的logcat:

d/TAGTAG:應用程序啓動

d/TAGTAG:didDetermineStateForRegion

d/TAGTAG:didEnterRegion

但是,如果我改變compileSdkVersion實際版本再沒有什麼工作

compileSdkVersion 23 
buildToolsVersion "23.0.2" 
defaultConfig { 
    applicationId "com.test" 
    minSdkVersion 18 
    targetSdkVersion 23 
    versionCode 1 
    versionName "1.0" 
} 

的logcat:

D/TAGTAG:App started up

回答

4

從Android 6.0 Marshmallow開始,掃描藍牙LE設備(包括信標)的應用程序必須獲得動態位置許可才能允許這樣做。對於傳統用途,運行在該目標上的舊版Android SDK(在API 23之前)上運行的應用程序仍然可以掃描藍牙LE設備,但只能在前臺使用。如果您的目標是SDK 21而不是SDK 23,則這是您的應用程序可以正常工作的原因。

要在定位SDK 23時解決此問題,您只需添加動態權限請求。

private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1; 
    ... 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     ... 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      // Android M Permission check  
      if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {   
       final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
       builder.setTitle("This app needs location access"); 
       builder.setMessage("Please grant location access so this app can detect beacons."); 
       builder.setPositiveButton(android.R.string.ok, null); 
       builder.setOnDismissListener(new DialogInterface.OnDismissListener() { 
         @Override? 
         public void onDismiss(DialogInterface dialog) { 
           requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);?    
         } 
       }); 
       builder.show(); 
      } 
     } 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, 
              String permissions[], int[] grantResults) { 
     switch (requestCode) { 
      case PERMISSION_REQUEST_COARSE_LOCATION: { 
       if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        Log.d(TAG, "coarse location permission granted"); 
       } else { 
        final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
        builder.setTitle("Functionality limited"); 
        builder.setMessage("Since location access has not been granted, this app will not be able to discover beacons when in the background."); 
        builder.setPositiveButton(android.R.string.ok, null); 
        builder.setOnDismissListener(new DialogInterface.OnDismissListener() { 

         @Override 
         public void onDismiss(DialogInterface dialog) { 
         } 

        }); 
        builder.show(); 
       } 
       return; 
      } 
     } 
    } 

如何做到這一點的詳細說明可在這裏:https://altbeacon.github.io/android-beacon-library/requesting_permission.html

我寫了一篇博客文章中針對此話題在這裏:http://developer.radiusnetworks.com/2015/09/29/is-your-beacon-app-ready-for-android-6.html

編輯:作爲@ Near1999在下面評論指出,除非定位服務在設置中打開,否則一些Android 5+版本也不會檢測BLE設備。顯然,這個限制也僅適用於定位SDK 23+。在這裏看到更多的信息:https://github.com/AltBeacon/android-beacon-library/issues/301

+0

對於測試,我允許通過應用程序信息的位置權限。動態權限和應用信息 - >權限有什麼區別嗎? – Near1999

+0

我不確定你用什麼工具來設置App Info - >權限,但我懷疑它只是在AndroidManifest.xml中設置聲明的權限。你也必須這樣做,但從Android 6.0開始這是不夠的。某些權限還需要運行時的動態請求。 – davidgyoung

+0

我在菜單中拿着應用程序圖標,將它拖到頂部的「應用程序信息」,然後選擇「權限」,並在那裏啓用位置權限 – Near1999