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

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

问题描述

我目前正在对Android应用程序连接到通过蓝牙的工具,需要写字符串命令并接收字符串的响应回。目前,我有连接/读/写工作的TCP / IP通过Wi-Fi,现在试图实现蓝牙。但我遇到了一些障碍。我一直在寻找在网上试图寻找类似的例子,还没有任何运气。我一直在使用Android开发资源,例如:蓝牙聊天作为我的主要参考点

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.

我目前的code似乎工作。然后,它抛出一个服务发现在连接点未能例外。我现在用的是 DeviceListActivity 类做我要连接到该设备的发现和选择。它返回anActivityResult,然后我的蓝牙类等待它来处理,然后进行连接。在code下几乎是相同的蓝牙聊天应用程序。

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.

您可以对/找到该设备在手动任何时候在手机上...它确实需要一通code,但我不认为这是我遇到的问题。

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天全站免登陆