BluetoothAdapter.getDefaultAdapter().startDiscovery() 不工作

BluetoothAdapter.getDefaultAdapter().startDiscovery() not working

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() 方法 returns 在 API 级别 29 中为假,但在 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>

您需要在 Android 10.

中打开位置以扫描附近的设备

不确定您是否仍在寻找答案。 从API 29开始,Google需要LOCATION组权限才能使用某些蓝牙功能。因此,您需要将一些 标记添加到 Android Manifest,并且不要忘记在运行时请求这些权限。

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

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