BluetoothServerSocket 接受失败并抛出 IOException [英] BluetoothServerSocket accept fails and throws an IOException

查看:13
本文介绍了BluetoothServerSocket 接受失败并抛出 IOException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个支持 OBEX 对象推送配置文件的设备,该配置文件基于串行端口配置文件.我的猜测是我可以使用 Android 蓝牙聊天示例将此设备连接到我的 Android 手机.但是我遇到了一个问题,关于 android SDK 中的 socket.accept() 功能.我尝试像这样完成将我的手机与此设备连接:

I have a device which supports the OBEX Object Push Profile, this profile is based upon the Serial Port Profile. My guess is that I can use the Android Bluetooth Chat example for connecting this device to my Android Phone. But I ran into a problem, regarding the socket.accept() functionality in the android SDK. I try to accomplish to connect my phone with this device like this:

adapter = BluetoothAdapter.getDefaultAdapter(); 
device = adapter.getRemoteDevice("00:1B:DC:0F:EC:7E");

AcceptThread = new AcceptThread(true, adapter, device);
AcceptThread.start(); 

AcceptThread 中的构造函数是这样编码的:

The constructor in AcceptThread is coded like this:

public AcceptThread(boolean secure, BluetoothAdapter adapter, BluetoothDevice device) {
    BluetoothServerSocket tmp = null;
    this.adapter = adapter;
    this.device = device;

    // Create a new listening server socket
    try {
        tmp = adapter.listenUsingInsecureRfcommWithServiceRecord(device.getName(), UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
    } catch (Exception e) {
        Log.e(TAG, ".AcceptThread # listen() failed", e);
    } 
    mmServerSocket = tmp;
}

问题是当我尝试执行我之前说过的 connect()

The problem is when I try to do a connect() as I said before

public void run() {
    BluetoothSocket socket = null;

    // Listen to the server socket if we're not connected
    while (mState != STATE_CONNECTED) {
        try {
            // This is a blocking call and will only return on a
            // successful connection or an exception
            Log.d(TAG, "AcceptThread.run: accepting server socket connection");

            socket = mmServerSocket.accept(20000); 

            Log.d(TAG, ".AcceptThread.run # server socket connection accepted");
        } catch (Exception e) {
            Log.e(TAG, ".run # accept() failed: "+e);
            break;
        }
    }
}

正如您所看到的,ServerSocket 在 20 秒20000 ms 内接受每个传入的连接. 当时间到时,应用程序将抛出这样的 IOException>

As you can see the ServerSocket accept every incomming connection for 20 seconds or 20000 ms. When the time is up, the app will throw an IOException like this

07-11 10:30:08.355: E/SIMPLECONNECT(1301): .run # accept() failed: java.io.IOException: Connection timed out

这告诉我我的设备无法连接到我的安卓手机.该设备的显示屏上没有连接按钮,只有一个发送功能,可以将文件发送到我的手机.我相信这个发送功能也会在后台进行连接,但我不确定.

which tells me that my device couldnt connect to my android phone. The device doesnt have a connect button on the display, just a send functionalitywhich will send a file to my phone. I believe that this send functionality also do a connect in the background, but I am not sure.

有人可以为我指出任何解决方案吗?我在带有 Android 4.0.4

Can anybody pinpoint any solutions for me? I am running my app on a Samsung Galaxy SIII with Android 4.0.4

推荐答案

我终于解决了,问题是不同的Android版本和不同的设备似乎需要不同的socket.我在三星 Galaxy XCOVER、Tab1、Tab2、Nexus、Note、摩托罗拉 Defy 和 HTC Flyer 上进行了尝试.我使用的套接字是:

I finally solved it, the problem is that different Android Versions and different devices seemes to need different sockets. I tryed it with Samsung Galaxy XCOVER, Tab1, Tab2, Nexus, Note, Motorola Defy and HTC Flyer. The Sockets I used are:

答:

Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
mSocket = (BluetoothSocket) m.invoke(mmDevice, Integer.valueOf(1));

乙:

Method m = mmDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[]{int.class});
mSocket=(BluetoothSocket)m.invoke(mmDevice,Integer.valueOf(1));

C:

mSocket=BluetoothAdapter.getDefaultAdapter().getRemoteDevice(mmDevice.getAddress()).createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));

Android 4.0.x 适用于 Nexus、Flyer、Tab1 with A,BAndroid 4.0.3 适用于带有 B 的 Tab2Android 3,6,x 适用于 A,B 的 DEFYAndroid 2.3.6 适用于带 C 的 XCOVER

Android 4.0.x works for Nexus, Flyer,Tab1 with A,B Android 4.0.3 works for Tab2 with B Android 3,6,x works for DEFY with A,B Android 2.3.6 works for XCOVER with C

我找不到适用于所有设备的解决方案,而且我无法在创建和使用 Socket 之前找出适用于所有设备的解决方案,尤其是 XCOVER 为所有 Socket 执行 connect() 而不抛出异常,但如果我尝试 tro write() 就会捕获.因此,如果您想设置一个适用于所有设备的 bloothoh 连接,您必须创建套接字,连接写入,然后记住该套接字有效(例如在首选项中)

I can't find a solution witch works for all devices and I;m not able to find out witch socket will work before I create and use the Socket, especially the XCOVER perform the connect() for all Sockets without throwing an exception, but catch if i try tro write(). So if you want to setup a bloothoh connection wich works for all devices you have to create the sockets, connect an write and then remeber the socket wich works (e.g. in preferences)

这篇关于BluetoothServerSocket 接受失败并抛出 IOException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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