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

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

问题描述

我有一个支持OBEX对象推送模式的设备,此配置文件是基于串行端口配置文件。我的猜测是,我可以使用Android的蓝牙聊天例如该设备连接到我的Andr​​oid手机。不过,我遇到了一个问题,对于在Android SDK中的 socket.accept()功能。我试图完成与此装置像这样连我的电话:

 适配器= BluetoothAdapter.getDefaultAdapter();
设备= adapter.getRemoteDevice(00:1B:DC:0F:EC:7E);

AcceptThread =新AcceptThread(真,适配器,设备);
AcceptThread.start();
 

在AcceptThread构造函数是codeD这样的:

 公共AcceptThread(布尔安全,BluetoothAdapter适配器,BluetoothDevice类设备){
    BluetoothServerSocket TMP = NULL;
    this.adapter =适配器;
    this.device =设备;

    //创建一个新的监听服务器套接字
    尝试 {
        TMP = adapter.listenUsingInsecureRfcommWithServiceRecord(device.getName(),UUID.fromString(00001101-0000-1000-8000-00805F9B34FB));
    }赶上(例外五){
        Log.e(TAG,.AcceptThread#听()失败,E);
    }
    mmServerSocket = TMP;
}
 

现在的问题是,当我尝试做一个连接()前面说过

 公共无效的run(){
    的BluetoothSocket插座= NULL;

    //监听到服务器套接字,如果我们没有连接
    而(mState!= STATE_CONNECTED){
        尝试 {
            //这是一个阻塞调用,只会返回一个
            //连接成功或异常
            Log.d(TAG,AcceptThread.run:接受服务器套接字连接);

            插座= mmServerSocket.accept(20000);

            Log.d(TAG,.AcceptThread.run#服务器套接字连接接受);
        }赶上(例外五){
            Log.e(TAG,.RUN#accept()方法失败:+ E);
            打破;
        }
    }
}
 

正如你所看到的ServerSocket接受 20秒每进来的连接 20000毫秒。当时间后,应用程序将抛出IOException这样

 十月7号至11号:30:08.355:E / SIMPLECONNECT(1301):.RUN#accept()方法失败:java.io.IOException异常:连接超时
 

它告诉我,我的设备不可能连接到我的Andr​​oid手机。可是没有显示器上的连接按钮设备,只需发送functionalitywhich将文件发送到我的手机。我相信这个传送功能也做了连接在后台,但我不知道。

任何人都可以找出任何解决方案给我吗?我上运行我的应用程序一个三星Galaxy SIII 安卓4.0.4

解决方案

我终于解决了,问题是,不同的Andr​​oid版本和不同的设备seemes需要不同的插座。我trye​​d它与三星Galaxy XCOVER,TAB1,TAB2,Nexus的,请注意,摩托罗拉Defy和HTC Flyer的。 我使用的套接字是:

答:

 方法M = mmDevice.getClass()实现getMethod(createRfcommSocket,新的等级[] {} int.class)。
mSocket =(的BluetoothSocket)m.invoke(mmDevice,Integer.valueOf(1));
 

B:

 方法M = mmDevice.getClass()实现getMethod(createInsecureRfcommSocket,新的等级[] {} int.class)。
mSocket =(的BluetoothSocket)m.invoke(mmDevice,Integer.valueOf(1));
 

C:

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

的Andr​​oid 4.0.x的作品的Nexus,传单,TAB1有A,B 为TAB2的Andr​​oid 4.0.3工程为B 安卓3,6中,x作品DEFY有A,B 对于XCOVER的Andr​​oid 2.3.6工程用C

我无法找到一个解决方案女巫适用于所有的设备和I,M没能找到女巫插座将工作之前,我创建和使用的插座,尤其是XCOVER执行的connect()为所有插座未抛出异常,但赶上,如果我尝试TRO的write()。所以,如果你想建立一个bloothoh连接至极的作品,你必须创建插座,连接一个写和所有设备,然后将的记住插座至极的作品(如preferences)

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(); 

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;
}

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;
        }
    }
}

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.

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

解决方案

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:

A:

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

B:

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 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

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