1

私は Android4.1 で 2 つの Android フォンを持っています。おそらく、それらをフォン A とフォン B と呼ぶことができます。次に、フォン A の MAC アドレスと uuid を知っています。電話Aのuuidを電話Bに送信し、BをBluetoothでAに接続させます。したがって、AはBluetoothサーバー、BはBluetoothクライアントです。しかし、私がそれを行うと、電話Bは
W/System.err(18404): java.io.IOException: Host is downと言った

そのようなサーバーとしての電話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:Host is down after 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)
4

0 に答える 0