Android 蓝牙 API 未找到任何设备

Android Bluetooth API not finding any devices

我正在尝试在我的应用程序中实现蓝牙设备发现功能。我已经使用 BluetoothAdapterBroadcastReceiver 实现了建议的方法,如下所示:

My Activity (discover() is called in onCreate):

    public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
        BluetoothAdapter mBluetoothAdapter;
        private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();

                //Finding devices
                if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
                    //discovery starts, we can show progress dialog or perform other tasks
                    testDevice("Start discover");
                } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                    //discovery finishes, dismis progress dialog
                    testDevice("Discovery finished");
                } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    //bluetooth device found
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    testDevice("Found device " + device.getName());
                }
            }
        };

        public void discover() {
            this.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

            IntentFilter filter = new IntentFilter();

            filter.addAction(BluetoothDevice.ACTION_FOUND);
            filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
            filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
            registerReceiver(mReceiver, filter);
            mBluetoothAdapter.startDiscovery();
        }

        public void testDevice(String msg) {
                Toast.makeText(this, msg,
                        Toast.LENGTH_LONG).show();
        }

}

在我的清单中,我拥有以下权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

结果是我得到了两次祝酒词:"Start discovery" 和 10 秒后 "Discovery finished"。从未找到设备。我已经在 Samsung S9+、Sansumg Tab A 和 Motorola something(蓝牙开启)上进行了测试。

有什么我没有做的吗,或者有什么我不知道的已知问题吗?

纯粹是我的猜测,但如果您 运行 处于 API 级别 23 +(并且您可能是),您需要动态请求权限,而不仅仅是声明它们在清单中。使用 RxPermissionsContextCompat.checkSelfPermission.