BluetoothAdapter.getDefaultAdapter()。startDiscovery()无效

问题描述 投票:0回答:1
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>
android android-bluetooth
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.