BluetoothAdapter在Android6.0/7.0+以上startDiscovery不能发现蓝牙设备问题

转载自:https://blog.csdn.net/zhangphil/article/details/77896924

BluetoothAdapter在Android6.0+以上startDiscovery不能发现蓝牙设备问题


问题的重要原因之一是Android 6.0+,Android 7.0+的权限问题引起的。在Android 4.0+上运行良好的蓝牙代码,在高版本运行异常。比如BluetoothAdapter的startDiscovery虽然启动了发现蓝牙任务,但是不能发现蓝牙设备。解决问题是针对最新高版本的Android系统增加权限申请。现在给出一个完整例子。

activity_main.xml:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     tools:context="zhangphil.bluetooth.MainActivity">  
  8.   
  9.     <Button  
  10.         android:id="@+id/init"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="初始化蓝牙设备" />  
  14.   
  15.     <Button  
  16.         android:id="@+id/discovery"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:text="发现设备" />  
  20.   
  21.     <Button  
  22.         android:id="@+id/enable_discovery"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:text="使自身可被其他蓝牙设备发现" />  
  26.   
  27.     <ListView  
  28.         android:layout_width="match_parent"  
  29.         android:layout_height="wrap_content"  
  30.         android:id="@+id/listView">  
  31.     </ListView>  
  32. </LinearLayout>  



测试的MainActivity.java:

[java] view plain copy
  1. package zhangphil.bluetooth;  
  2.   
  3. import android.Manifest;  
  4. import android.app.Activity;  
  5. import android.bluetooth.BluetoothAdapter;  
  6. import android.bluetooth.BluetoothDevice;  
  7. import android.content.BroadcastReceiver;  
  8. import android.content.Context;  
  9. import android.content.Intent;  
  10. import android.content.IntentFilter;  
  11. import android.content.pm.PackageManager;  
  12. import android.os.Build;  
  13. import android.os.Bundle;  
  14. import android.util.Log;  
  15. import android.view.View;  
  16. import android.widget.ArrayAdapter;  
  17. import android.widget.ListView;  
  18.   
  19. public class MainActivity extends Activity implements View.OnClickListener {  
  20.     private final int REQUEST_ENABLE_BT = 0xa01;  
  21.     private final int PERMISSION_REQUEST_COARSE_LOCATION = 0xb01;  
  22.   
  23.     private String TAG = "zhangphil";  
  24.   
  25.     private ArrayAdapter<String> mAdapter;  
  26.     private BluetoothAdapter mBluetoothAdapter;  
  27.   
  28.     // 广播接收发现蓝牙设备  
  29.     private BroadcastReceiver mReceiver = new BroadcastReceiver() {  
  30.   
  31.         @Override  
  32.         public void onReceive(Context context, Intent intent) {  
  33.             String action = intent.getAction();  
  34.   
  35.             if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {  
  36.                 Log.d(TAG, "开始扫描...");  
  37.             }  
  38.   
  39.             if (BluetoothDevice.ACTION_FOUND.equals(action)) {  
  40.                 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
  41.                 if (device != null) {  
  42.                     // 添加到ListView的Adapter。  
  43.                     mAdapter.add("设备名:" + device.getName() + "\n设备地址:" + device.getAddress());  
  44.                     mAdapter.notifyDataSetChanged();  
  45.                 }  
  46.             }  
  47.   
  48.             if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {  
  49.                 Log.d(TAG, "扫描结束.");  
  50.             }  
  51.         }  
  52.     };  
  53.   
  54.     @Override  
  55.     protected void onCreate(Bundle savedInstanceState) {  
  56.         super.onCreate(savedInstanceState);  
  57.         setContentView(R.layout.activity_main);  
  58.   
  59.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {  
  60.             if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {  
  61.                 requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);  
  62.             }  
  63.         }  
  64.   
  65.   
  66.         // 注册广播接收器。  
  67.         // 接收蓝牙发现  
  68.         IntentFilter filterFound = new IntentFilter(BluetoothDevice.ACTION_FOUND);  
  69.         registerReceiver(mReceiver, filterFound);  
  70.   
  71.         IntentFilter filterStart = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);  
  72.         registerReceiver(mReceiver, filterStart);  
  73.   
  74.         IntentFilter filterFinish = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);  
  75.         registerReceiver(mReceiver, filterFinish);  
  76.   
  77.         mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1);  
  78.         ((ListView) findViewById(R.id.listView)).setAdapter(mAdapter);  
  79.   
  80.         findViewById(R.id.init).setOnClickListener(this);  
  81.         findViewById(R.id.discovery).setOnClickListener(this);  
  82.         findViewById(R.id.enable_discovery).setOnClickListener(this);  
  83.     }  
  84.   
  85.     @Override  
  86.     public void onClick(View view) {  
  87.         switch (view.getId()) {  
  88.             case R.id.init:  
  89.                 init();  
  90.   
  91.             case R.id.discovery:  
  92.                 discovery();  
  93.   
  94.             case R.id.enable_discovery:  
  95.                 enable_discovery();  
  96.         }  
  97.     }  
  98.   
  99.     // 初始化蓝牙设备  
  100.     private void init() {  
  101.         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
  102.   
  103.         // 检查设备是否支持蓝牙设备  
  104.         if (mBluetoothAdapter == null) {  
  105.             Log.d(TAG, "设备不支持蓝牙");  
  106.   
  107.             // 不支持蓝牙,退出。  
  108.             return;  
  109.         }  
  110.   
  111.         // 如果用户的设备没有开启蓝牙,则弹出开启蓝牙设备的对话框,让用户开启蓝牙  
  112.         if (!mBluetoothAdapter.isEnabled()) {  
  113.             Log.d(TAG, "请求用户打开蓝牙");  
  114.   
  115.             Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
  116.             startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);  
  117.             // 接下去,在onActivityResult回调判断  
  118.         }  
  119.     }  
  120.   
  121.     // 启动蓝牙发现...  
  122.     private void discovery() {  
  123.         if (mBluetoothAdapter == null) {  
  124.             init();  
  125.         }  
  126.   
  127.         mBluetoothAdapter.startDiscovery();  
  128.     }  
  129.   
  130.     // 可选方法,非必需  
  131.     // 此方法使自身的蓝牙设备可以被其他蓝牙设备扫描到,  
  132.     // 注意时间阈值。0 - 3600 秒。  
  133.     // 通常设置时间为120秒。  
  134.     private void enable_discovery() {  
  135.         Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);  
  136.   
  137.         // 第二个参数可设置的范围是0~3600秒,在此时间区间(窗口期)内可被发现  
  138.         // 任何不在此区间的值都将被自动设置成120秒。  
  139.         discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 3600);  
  140.   
  141.         startActivity(discoverableIntent);  
  142.     }  
  143.   
  144.     @Override  
  145.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  146.         super.onActivityResult(requestCode, resultCode, data);  
  147.   
  148.         if (requestCode == REQUEST_ENABLE_BT) {  
  149.             if (resultCode == RESULT_OK) {  
  150.                 Log.d(TAG, "打开蓝牙成功!");  
  151.             }  
  152.   
  153.             if (resultCode == RESULT_CANCELED) {  
  154.                 Log.d(TAG, "放弃打开蓝牙!");  
  155.             }  
  156.   
  157.         } else {  
  158.             Log.d(TAG, "蓝牙异常!");  
  159.         }  
  160.     }  
  161.   
  162.     @Override  
  163.     protected void onDestroy() {  
  164.         super.onDestroy();  
  165.         unregisterReceiver(mReceiver);  
  166.     }  
  167.   
  168.   
  169.     @Override  
  170.     public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {  
  171.         switch (requestCode) {  
  172.             case PERMISSION_REQUEST_COARSE_LOCATION:  
  173.                 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {  
  174.   
  175.                 }  
  176.   
  177.                 break;  
  178.         }  
  179.     }  
  180. }  


不要忘记增加权限:

[html] view plain copy
  1. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
  2.    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
  3.    <uses-permission android:name="android.permission.BLUETOOTH" />  
  4.    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />  



代码运行结果:

BluetoothAdapter在Android6.0/7.0+以上startDiscovery不能发现蓝牙设备问题