Android 6.0 Bluetooth LE can't scan
開心的將Nexus 7 二代升級到6.0, 悲劇隨即發生,為什麼我的藍牙都scan不到其他裝置, 拿其他的設備(5.0)藍牙scan 都是正常的, 上網搜尋了一下,這才發現Android 6.0 有在這部分做一些變更, 以下為解決方法:
Quick Start Enable access coarse location permission 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 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 to 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(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 @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 ; } } }
AndroidManifest.xml Add permission:
access coarse location
access fine location
1 2 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Android 6.0 changes Access to Hardware Identifier To access the hardware identifiers of nearby external devices via Bluetooth and Wi-Fi scans, your app must now have the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions:
WifiManager.getScanResults()
BluetoothDevice.ACTION_FOUND
BluetoothLeScanner.startScan()
Reference: http://developer.android.com/intl/ko/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id
<
Hexo Push to Github
Android getActionBar() return null
>