当前位置:   article > 正文

Android蓝牙开发(1):发现设备失败,startDiscovery()返回结果为false_android startdiscovery 返回false

android startdiscovery 返回false

原因:需要配置相关权限,并且位置权限需要进行动态申请

Android官方蓝牙指南

https://developer.android.google.cn/guide/topics/connectivity/bluetooth#SettingUp

配置权限:AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET" />
    <!-- 蓝牙权限 -->
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <!-- google在android6.0之后,为了更好的保护用户的数据安全,所有需要访问硬件唯一标识符的地方都需要申请位置权限 -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在代码中动态申请位置权限:MainActivity.java

ps:关于动态申请权限的成功/失败处理自行百度

        //判断是否有访问位置的权限,没有权限,直接申请位置权限
        if ((checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
                || (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 200);
        }
  • 1
  • 2
  • 3
  • 4
  • 5

广播类:BluetoothBroadcastReceiver.java

public class BluetoothBroadcastReceiver extends BroadcastReceiver {
    private static final String TAG = "BluetoothBroadcastRecei";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Log.e(TAG, "设备名字:" + device.getName() + "\n设备地址:" + device.getAddress());
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

检测及开启蓝牙功能:MainActivity.java

@Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //判断是否有访问位置的权限,没有权限,直接申请位置权限
        if ((checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
                || (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 200);
        }

        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        receiver = new BluetoothBroadcastReceiver();
        registerReceiver(receiver, filter);
        
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        
        if(bluetoothAdapter!=null){
            // 如果设备存在蓝牙功能
            if(bluetoothAdapter.isEnabled()){
                // 如果蓝牙功能已开启
                if(bluetoothAdapter.isDiscovering()){
                    // 如果正在执行“发现设备”操作
                    bluetoothAdapter.cancelDiscovery();
                }
                else {
                    // 开始“发现设备”!
                    bluetoothAdapter.startDiscovery();
                }
            }
        }
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();

        // Don't forget to unregister the ACTION_FOUND receiver.
        unregisterReceiver(receiver);
    }
  • 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
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/483785
推荐阅读
相关标签