在 Android 上使用蓝牙服务发现失败异常 [英] Service discovery failed exception using Bluetooth on Android

查看:22
本文介绍了在 Android 上使用蓝牙服务发现失败异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个 Android 应用程序,该应用程序通过蓝牙连接到仪器,需要编写字符串命令并接收字符串响应.目前,我通过 Wi-Fi 为 TCP/IP 进行连接/读/写工作,现在正在尝试实现蓝牙.但我遇到了一些障碍.我一直在网上搜索,试图找到类似的例子,但没有任何运气.我一直在使用 Android 开发者资源示例:Bluetooth Chat 作为我的主要参考点.

I'm currently working on an Android application that connects to an instrument via Bluetooth and need to write string commands and receive string responses back. Currently I have the connect/read/write working for TCP/IP over Wi-Fi and now trying to implement Bluetooth. But I am running into some roadblocks. I have been searching the web trying to find examples of something similar and haven't had any luck. I have been using the Android developer resource example: Bluetooth Chat as my main reference point.

我当前的代码似乎可以工作..然后它在连接点抛出一个服务发现失败异常.我正在使用 DeviceListActivity 类来发现和选择我想要连接的设备.它返回一个ActivityResult,然后我的蓝牙类等待它处理它,然后连接到它.下面的代码与蓝牙聊天应用程序几乎相同.

My current code seems to work.. Then it throws a Service Discovery Failed exception at the point of the connection. I am using the DeviceListActivity class to do the discovery and selecting of the device I want to connect to. It returns anActivityResult and then my Bluetooth class waits for it to handle that and then does the connect to it. The code beneath is almost identical to the Bluetooth Chat App.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(!m_BluetoothAdapter.isEnabled())
    {
        m_BluetoothAdapter.enable();
    }
    switch (requestCode) {
        case REQUEST_CONNECT_DEVICE:
            // When DeviceListActivity returns with a device to connect
            if (resultCode == Activity.RESULT_OK) {
                // Get the device MAC address
                String address = data.getExtras()
                                     .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
                // Get the BLuetoothDevice object
                BluetoothDevice device = m_BluetoothAdapter.getRemoteDevice(address);
                // Attempt to connect to the device
                connect(device);
            }
            break;

        case REQUEST_ENABLE_BT:
            // When the request to enable Bluetooth returns
            if (resultCode == Activity.RESULT_OK) {
                // Bluetooth is now enabled, so set up a chat session
            }
            else {
                // User did not enable Bluetooth or an error occured

                Toast.makeText(this, "Bluetooth not enabled", Toast.LENGTH_SHORT).show();
                finish();
            }
    }
}

这是我的连接函数:

private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

private void connect(BluetoothDevice device) {
    m_Device = device;
    BluetoothSocket tmp = null;

    // Get a BluetoothSocket for a connection with the
    // given BluetoothDevice
    try {
        tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
    }
    catch (IOException e) {

    }
    m_Socket = tmp;

    m_BluetoothAdapter.cancelDiscovery();

    try {
        // This is a blocking call and will only return on a
        // successful connection or an exception
        m_Socket.connect();
    }
    catch (IOException e) {
        try {
            m_Socket.close();
        }
        catch (IOException e2) {
        }
        return;
    }
}

希望无论我做错什么都很简单,但恐怕从来没有那么容易.这是我第一次做任何蓝牙开发,也许我做错了什么......但我不知道为什么我会得到服务发现失败的异常.

Hopefully, whatever I am doing wrong is simple, but I'm afraid it's never that easy. This is my first time doing any Bluetooth development, and maybe I'm doing something blatantly wrong... But I'm not sure why I get the service discovery failed exception.

您可以随时在手机上手动配对/查找设备...它确实需要密码,但我认为这不是我遇到的问题.

You can pair/find the device at all times manually on the phone... It does require a passcode, but I don't think that is the problem that I am having.

推荐答案

三天后,由于一些非常有用的帖子,我弄明白了.

After three days I got it figured out thanks to some very helpful posts.

我不得不更换:

tmp = device.createRfcommSocketToServiceRecord(MY_UUID);

与:

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
         tmp = (BluetoothSocket) m.invoke(device, 1);

瞧,它管用了!

这篇关于在 Android 上使用蓝牙服务发现失败异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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