Android - 蓝牙发现找不到任何设备 [英] Android - Bluetooth discovery doesn't find any device

查看:37
本文介绍了Android - 蓝牙发现找不到任何设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个小应用程序,以开始使用蓝牙 Android API 可以提供的服务.

I'm currently working on a little app to get started with the services that the Bluetooth Android API can provide.

编辑 -> 答案:

问题似乎是由特定的 Nexus 5 设备引起的.似乎他们的蓝牙接收器不能正常工作.以下解决方案适用于其他设备

It seems that the issue was due to the specific Nexus 5 devices. Seems like their bluetooth receiver doesn't work well. Solution below should work for other devices

备注:

  1. 我在这里阅读了文档:http://developer.android.com/指南/主题/connectivity/bluetooth.html以及本教程的以下源代码 http://www.londatiga.net/it/programming/android/how-to-programmatically-scan-or-discover-android-bluetooth-device/ 位于 github 下的/lorensiuswlt/AndroBluetooth

  1. I’ve read the documentation here: http://developer.android.com/guide/topics/connectivity/bluetooth.html as well as the following source code of this tutorial http://www.londatiga.net/it/programming/android/how-to-programmatically-scan-or-discover-android-bluetooth-device/ located on github under /lorensiuswlt/AndroBluetooth

我已经完成了几乎所有我感兴趣的功能(例如检查适配器是否存在、启用/禁用蓝牙、查询配对设备、设置适配器可发现).

I’ve finished almost all the features that interested me (such as check for adapter existence, enable/disable the blueooth, querying paired divices, set the adapter discoverable).

问题:

实际上没有找到设备,当我启动 .onDiscovery() 方法时,即使从我的 Nexus 5 上的设置/蓝牙找到设备.

Actually no device is found when i launch the .onDiscovery() method, even though devices are found from Settings/Bluetooth on my Nexus 5.

我是这样处理的:

public class MainActivity extends AppCompatActivity {
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

...

protected void onCreate(Bundle savedInstanceState) {
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter); 
}

过滤器运行良好就我可以尝试而言,即 ACTION_STATE_CHANGED(启用蓝牙)和两个 ACTION_DISCOVERY_***.

The filter is working well as far as i could try, i.e ACTION_STATE_CHANGED (on bluetooth enabling) and the two ACTION_DISCOVERY_***.

然后成功调用以下方法:

The following method is then successfuly called:

public void onDiscovery(View view)
{
    mBluetoothAdapter.startDiscovery();
}

然后我有我的蓝牙接收器:

And then i have my bluetooth receiver:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);

            if (state == BluetoothAdapter.STATE_ON) {
                showToast("ACTION_STATE_CHANGED: STATE_ON");
            }
        }

        else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            mDeviceList = new ArrayList<>();
            showToast("ACTION_DISCOVERY_STARTED");
            mProgressDlg.show();
        }

        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action) && !bluetoothSwitchedOFF) {
            mProgressDlg.dismiss();
            showToast("ACTION_DISCOVERY_FINISHED");

            Intent newIntent = new Intent(MainActivity.this, DeviceListActivity.class);

            newIntent.putParcelableArrayListExtra("device.list", mDeviceList);

            startActivity(newIntent);
        }

        else if (BluetoothDevice.ACTION_FOUND.equals(action)) {// When discovery finds a device
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            mDeviceList.add(device);
            showToast("Device found = " + device.getName());
        }
    }
};

我在输出 logcat 时没有遇到任何问题,并且在我进行的测试期间没有发现任何问题.唯一的问题是在扫描结束时未发现任何设备,此时有许多可发现的设备可用.

I don't have any issue coming out the logcat and didn't notice any trouble during the test I did. The only problem is that no device is discovered at the end of the scan, when many discoverable ones are available arround.

我尽量不写太多代码,以免淹没话题.询问我是否需要更多.

I tried to not put too much code in order to not flood the topic. Ask me if you need more.

感谢您阅读我,并提前感谢您的回答.

Thanks for reading me, and thanks in advance for you answers.

推荐答案

你在什么版本的 Android 上运行这个?如果是 Android 6.x,我相信您需要在清单中添加 ACCESS_FINE_LOCATION 权限.例如:

What version of Android are you running this on? If it is Android 6.x, I believe you need to add the ACCESS_FINE_LOCATION permission to your manifest. For example:

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

我遇到了类似的问题,这为我解决了.

I had a similar issue and this fixed it for me.

更新: 添加 直接来自 Google 的文档:

要通过蓝牙和 Wi-Fi 扫描访问附近外部设备的硬件标识符,您的应用现在必须具有 ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION 权限

To access the hardware identifiers of nearby external devices via Bluetooth and Wi-Fi scans, your app must now have the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions

2020 年更新:我们最近更新了我们的应用程序以面向 SDK 版本 29.在此过程中,我们的应用程序无法再次发现蓝牙设备.自从最初编写此答案以来,我们一直在使用 ACCESS_COARSE_LOCATION.更改为 ACCESS_FINE_LOCATION 似乎可以解决此问题.如果目标是 29 岁以上,我现在建议开发人员尝试 ACCESS_FINE_LOCATION.已更新答案以反映这一点.

UPDATE 2020: We recently updated our application to target SDK Version 29. In doing this, our application stopped being able to discover Bluetooth devices again. We have been using ACCESS_COARSE_LOCATION since this answer was originally written. Changing to ACCESS_FINE_LOCATION appears to fix the issue. I now recommend developers try ACCESS_FINE_LOCATION if targeting 29+. Answer updated to reflect this.

这篇关于Android - 蓝牙发现找不到任何设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆