简体   繁体   English

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);
    }

I am try to scan nearby bluetooth devices.我正在尝试扫描附近的蓝牙设备。 But, BluetoothAdapter.getDefaultAdapter().startDiscovery() method returns false in API Level 29 but working in API Level 26但是,BluetoothAdapter.getDefaultAdapter().startDiscovery() 方法在 API 级别 29 中返回 false,但在 API 级别 26 中工作

-------------Permission defined in AndroidManifest.xml----------- Can't find solution for Android 10 -------------在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 中开启位置信息才能扫描附近的设备。

not sure if you are still looking for the answer.不知道你是否还在寻找答案。 From API 29, Google requires LOCATION group permission to use some Bluetooth function.从 API 29 开始,Google 需要 LOCATION 组权限才能使用某些蓝牙功能。 So you will need to add some <uses-permission> tag to Android Manifest, and DON'T forget to request these permissions at RUNTIME.因此,您需要在 Android Manifest 中添加一些 <uses-permission> 标记,并且不要忘记在 RUNTIME 请求这些权限。

I don't remember exactly which location is needed, so you can add all 3 following:我不记得确切需要哪个位置,因此您可以添加以下所有 3 个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 BluetoothAdapter.getDefaultAdapter(); 返回null - BluetoothAdapter.getDefaultAdapter(); returns null 由于棉花糖使用BluetoothAdapter.getDefaultAdapter()。startDiscovery();更新了蓝牙发现。 被打破 - Since marshmallow update Bluetooth discovery using BluetoothAdapter.getDefaultAdapter().startDiscovery(); is broken BluetoothAdapter.getDefaultAdapter()不返回null - BluetoothAdapter.getDefaultAdapter() Not returning null BluetoothAdapter.getDefaultAdapter()引发异常 - BluetoothAdapter.getDefaultAdapter() throws exception 错误:无法访问语句 myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); - error: unreachable statement myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothAdapter.getDefaultAdapter()会因uiautomator中的NullPointerException而失败 - BluetoothAdapter.getDefaultAdapter() fail with NullPointerException from uiautomator BluetoothAdapter.getDefaultAdapter()在不在Activity中时抛出RuntimeException - BluetoothAdapter.getDefaultAdapter() throwing RuntimeException while not in Activity 调用 BluetoothAdapter.getDefaultAdapter 时出现安全异常 - SecurityException when calling BluetoothAdapter.getDefaultAdapter BluetoothAdapter.getDefaultAdapter() 抛出 RuntimeException:存根! - BluetoothAdapter.getDefaultAdapter() throw RuntimeException: Stub! BluetoothAdapter.getDefaultAdapter() 已弃用,现在我该用什么? - What do I use now that BluetoothAdapter.getDefaultAdapter() is deprecated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM