IOException:主机在蓝牙连接中关闭

时间:2012-09-24 14:22:17

标签: android bluetooth ioexception

我在Android4.1中有两个安卓手机可能我可以称他们为手机A和手机B. 然后我知道了手机A的MAC地址和uuid。所以我想做的是,我打算将手机A的MAC地址和手机A的MAC地址给手机B,然后让B连接到蓝牙的A.所以A是蓝牙服务器,B是蓝牙客户端。 但是当我这样做时。电话B说了      W / System.err(18404):java.io.IOException:主机已关闭

手机A中的代码就像服务器那样:

    private class AcceptThread extends Thread {
    private final BluetoothServerSocket mmServerSocket;

    public AcceptThread() {

        BluetoothServerSocket tmp = null;
        try {               
           Method m =  mBluetoothAdapter.getClass().getMethod("listenUsingRfcommOn", new Class[] {int.class});
           tmp = (BluetoothServerSocket)m.invoke(mBluetoothAdapter, new Object[]{25});
        Log.i("liyufei","in server  we begin at uuid = "+selfUuid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mmServerSocket = tmp;
    }

    public void run() {
        BluetoothSocket socket = null;
        // Keep listening until exception occurs or a socket is returned
        Log.i("liyufei","listening is begin");
        while (true) {
            try {
                Log.i("liyufei","wait...............");
                socket = mmServerSocket.accept();
            } catch (IOException e) {
                e.printStackTrace();
                break;
            }
            Log.i("liyufei","keep going socket is try to create");
            if (socket != null) {
                Log.i("liyufei","server socket accept!!!");
                mHandler.obtainMessage(MESSAGE_ACCEPT).sendToTarget();
                try {
                    mmServerSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            }
            Log.i("liyufei","socket is null");
        }
        Log.i("liyufei","while is stop");
    }

    /** Will cancel the listening socket, and cause the thread to finish */
    public void cancel() {
        try {
            mmServerSocket.close();
            Log.i("liyufei","we try to Close server!!");
        } catch (IOException e) { 
            e.printStackTrace();
        }
    }
}

电话B中的代码就像客户端一样:

private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {

        BluetoothSocket tmp = null;
        mmDevice = device;
        Log.i("liyufei","new connect thread......");
        try {
            Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
            tmp = (BluetoothSocket) m.invoke(mmDevice,Integer.valueOf(25));
        } catch (Exception e) { 
            e.printStackTrace();
        }
        mmSocket = tmp;
    }

    public void run() {
        mBluetoothAdapter.cancelDiscovery();  
        try {
            Log.i("liyufei","we try to connect.....");
            mmSocket.connect();
        } catch (Exception connectException) {
            Log.i("liyufei","connect to server failed...");
            connectException.printStackTrace();
            try {
                mmSocket.close();
            } catch (IOException closeException) { }
            return;
        }
        Log.i("liyufei","client has success connect!!!");
        mHandler.obtainMessage(MESSAGE_CONNECT).sendToTarget();
    }

    /** Will cancel an in-progress connection, and close the socket */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) { }
    }
}

然后我就可以得到IOException:主机在mmSocket.connect()之后关闭;在电话B中 谁能帮帮我???非常感谢!!!!! 来自@Waqas的测试后记录

09-25 14:31:26.587: I/liyufei(12453): new connect thread......
09-25 14:31:26.587: I/liyufei(12453): we begin in connect uuid = 66d77030-9982-42cf-88a7-272954408361
09-25 14:31:26.595: E/BluetoothService.cpp(308): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)
09-25 14:31:26.595: I/liyufei(12453): we try to connect.....
09-25 14:31:26.900: D/PhoneStatusBar(389): disable: < expand* icons alerts ticker system_info back home recent clock >
09-25 14:31:26.970: D/dalvikvm(520): GC_FOR_ALLOC freed 140K, 44% free 14337K/25415K, paused 20ms, total 20ms
09-25 14:31:26.986: I/dalvikvm-heap(520): Grow heap (frag case) to 17.297MB for 3409936-byte allocation
09-25 14:31:27.017: D/dalvikvm(520): GC_CONCURRENT freed <1K, 31% free 17667K/25415K, paused 13ms+3ms, total 32ms
09-25 14:31:27.017: D/dalvikvm(520): WAIT_FOR_CONCURRENT_GC blocked 19ms
09-25 14:31:31.720: I/liyufei(12453): connect to server failed...1
09-25 14:31:31.720: W/System.err(12453): java.io.IOException: Host is down
09-25 14:31:31.720: W/System.err(12453):    at android.bluetooth.BluetoothSocket.connectNative(Native Method)
09-25 14:31:31.720: W/System.err(12453):    at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:216)
09-25 14:31:31.720: W/System.err(12453):    at com.example.android.beam.BeamBlueTooth$ConnectThread.run(BeamBlueTooth.java:297)
09-25 14:31:31.720: I/liyufei(12453): we try to connect.....
09-25 14:31:31.720: I/liyufei(12453): connect to server failed...2
09-25 14:31:31.720: W/System.err(12453): java.io.IOException: File descriptor in bad state
09-25 14:31:31.720: W/System.err(12453):    at android.bluetooth.BluetoothSocket.connectNative(Native Method)
09-25 14:31:31.720: W/System.err(12453):    at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:216)
09-25 14:31:31.720: W/System.err(12453):    at com.example.android.beam.BeamBlueTooth$ConnectThread.run(BeamBlueTooth.java:297)
09-25 14:31:31.720: I/liyufei(12453): we try to connect.....
09-25 14:31:31.728: I/liyufei(12453): connect to server failed...3
09-25 14:31:31.728: W/System.err(12453): java.io.IOException: File descriptor in bad state
09-25 14:31:31.728: W/System.err(12453):    at android.bluetooth.BluetoothSocket.connectNative(Native Method)
09-25 14:31:31.728: W/System.err(12453):    at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:216)
09-25 14:31:31.728: W/System.err(12453):    at com.example.android.beam.BeamBlueTooth$ConnectThread.run(BeamBlueTooth.java:297)

0 个答案:

没有答案