2018/6/7

BLE GATT Example for Android 6

Android 6 之後,BLE Scan 需要得到 Location 權限。
所以以前的 example 就要修改了,否則 scan 不出東西,logcat 還會出現 need permission COARSE_LOCATION or FINE_LOCATION

文章參考"BLE Scan Not Working"這裡

我也修改了一下,最少修改。放在: android-BluetoothLEGatt,branch : fixAndroid6
大概就是..
diff --git a/Application/src/main/AndroidManifest.xml b/Application/src/main/AndroidManifest.xml
index d3cf257..7979018 100644
--- a/Application/src/main/AndroidManifest.xml
+++ b/Application/src/main/AndroidManifest.xml
@@ -32,6 +32,7 @@
 
     <uses-permission android:name="android.permission.BLUETOOTH"/>
     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
+    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
 
     <application android:label="@string/app_name"
         android:icon="@drawable/ic_launcher"
diff --git a/Application/src/main/java/com/example/android/bluetoothlegatt/DeviceScanActivity.java b/Application/src/main/java/com/example/android/bluetoothlegatt/DeviceScanActivity.java
index 9b86f7a..7c654dc 100644
--- a/Application/src/main/java/com/example/android/bluetoothlegatt/DeviceScanActivity.java
+++ b/Application/src/main/java/com/example/android/bluetoothlegatt/DeviceScanActivity.java
@@ -16,6 +16,7 @@
 
 package com.example.android.bluetoothlegatt;
 
+import android.Manifest;
 import android.app.Activity;
 import android.app.ListActivity;
 import android.bluetooth.BluetoothAdapter;
@@ -46,6 +47,7 @@ public class DeviceScanActivity extends ListActivity {
     private BluetoothAdapter mBluetoothAdapter;
     private boolean mScanning;
     private Handler mHandler;
+    private static final int PERMISSION_REQUEST_CORASE_LOCATION = 7788;
 
     private static final int REQUEST_ENABLE_BT = 1;
     // Stops scanning after 10 seconds.
@@ -64,6 +66,8 @@ public class DeviceScanActivity extends ListActivity {
             finish();
         }
 
+        requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},PERMISSION_REQUEST_CORASE_LOCATION);
+
         // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
         // BluetoothAdapter through BluetoothManager.
         final BluetoothManager bluetoothManager =

沒有留言:

張貼留言