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

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

问题描述

我目前正在开发一个小应用程序,以开始使用Bluetooth 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/guide/topics/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).

问题:

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

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_COURSE_LOCATION权限.例如:

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

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

我遇到了类似的问题,这对我来说已经解决了.

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

更新:添加直接从Google获得的文档:

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

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

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