BluetoothAdapter.getDefaultAdapter()。startDiscovery()无法正常工作

时间:2020-03-24 16:14:54

标签: android android-bluetooth

public void startScan() {
        final List<MyBluetoothDevice> arrayOfFoundBTDevices = new ArrayList<>();

        // start looking for bluetooth devices

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        boolean scanStatus = mBluetoothAdapter.startDiscovery();


        Timber.d("SCANNING_STATUS : " + scanStatus);

        // Discover new devices
        // Create a BroadcastReceiver for ACTION_FOUND
        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Timber.d("onReceiveSignal");
                String action = intent.getAction();
                // When discovery finds a device
                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    // Get the bluetoothDevice object from the Intent
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                    Timber.d("Discovery Finished ");
                    AppUtils.showToast(context, "Scanning restart");
                    mBluetoothAdapter.startDiscovery();
                }
            }
        };

        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        context.registerReceiver(mReceiver, filter);
    }

我正在尝试扫描附近的蓝牙设备。 但是,BluetoothAdapter.getDefaultAdapter()。startDiscovery()方法在API级别29中返回false,但在API级别26中起作用

-------------在AndroidManifest.xml中定义的权限----------- 找不到适用于Android 10的解决方案

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bluetoothexample">

    <uses-feature
        android:name="android.hardware.bluetooth"
        android:required="true" />

    <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:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".FoundBTDevices" />
    </application>
</manifest>

2 个答案:

答案 0 :(得分:1)

您需要打开位置信息才能在Android 10中扫描附近的设备。

答案 1 :(得分:0)

不确定您是否还在寻找答案。 根据API 29,Google需要LOCATION组权限才能使用某些蓝牙功能。因此,您将需要在Android Manifest中添加一些标签,并且不要忘记在RUNTIME请求这些权限。

我不记得确切需要哪个位置,因此您可以添加以下所有3个:

  • android.permission.ACCESS_FINE_LOCATION
  • android.permission.ACCESS_COARSE_LOCATION
  • android.permission.ACCESS_BACKGROUND_LOCATION