2

誰でもこの問題が何であるか教えてもらえますか? アプリケーションで を起動しようとしていますがAccept Thread、このエラーが発生し続けます。

誰でも助けることができますか?

Android 2.1 で HTC Hero を使用しています。

ERROR/BluetoothChatService(9585): java.io.IOException: Bindlisten failed

android.bluetooth.BluetoothAdapter.listenUsingRfcommWithServiceRecord(BluetoothAdapter.java:749)

こちらによると

public BluetoothServerSocket listenUsingRfcommWithServiceRecord (String name, UUID uuid)

Throws IOException  on error, for example Bluetooth not available, or insufficient permissions, or channel in use. 

ここにいくつかのコード:BluetoothChat.java次のコードを開発するためのガイダンスとリファレンスとして使用しました。したがって、Bluetooth が有効になると、ユーザーがメニュー ボタンを押すと、3 つのボタンが表示されます。serverAccept Thread を開始してゲームのホストになりたい場合、ユーザーはボタンを選択します。

public boolean onOptionsItemSelected(MenuItem item) {
    Intent serverIntent = null;
    switch (item.getItemId()) {
    case R.id.scan:
        // Launch the DeviceListActivity to see devices and do scan
        serverIntent = new Intent(this, DeviceListActivity.class);
        startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_SECURE);
        return true;
    case R.id.discoverable:
        // Ensure this device is discoverable by others
        ensureDiscoverable();
        return true;
    case R.id.server:
        // Ensure this device is discoverable by others
        if (mGameService != null)
        {
            // Only if the state is STATE_NONE, do we know that we haven't started already
            if (mGameService.getState() == BluetoothChatService.STATE_NONE)
            {
              // Start the Bluetooth chat services
              mGameService.start();
            }
        }
        return true;
    }
    return false;
}

ユーザーがServerボタンを選択すると、別のクラスから次のメソッドが呼び出されます。

    public synchronized void start() {
    if (D) Log.d(TAG, "start");

    // Cancel any thread attempting to make a connection
    if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}

    // Cancel any thread currently running a connection
    if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}

    setState(STATE_LISTEN);

    // Start the thread to listen on a BluetoothServerSocket
    if (mSecureAcceptThread == null) {
        mSecureAcceptThread = new AcceptThread();
        mSecureAcceptThread.start();
    }
}

そして、これがAccept Thread. Debugger を使用して、実行されることを確認できますAccept Thread。ただし、最初は失敗し、try catchこれを実行することはありません

 tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME,MY_UUID); 

以下のスレッドを受け入れます。

private class AcceptThread extends Thread {
    // The local server socket
    private final BluetoothServerSocket mmServerSocket;

    public AcceptThread() {
        BluetoothServerSocket tmp = null;

        // Create a new list ening server socket
        try {
                tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME,MY_UUID);
            }
        catch (IOException e) {
            Log.e(TAG, "Socket Type: " + "listen() failed", e);
        }
        mmServerSocket = tmp;
    }

    public void run() {
        if (D) Log.d(TAG, "Socket Type: " +
                "BEGIN mAcceptThread" + this);
        setName("AcceptThread");

        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
                socket = mmServerSocket.accept();
            } catch (IOException e) {
                Log.e(TAG, "Socket Type: " + "accept() failed", e);
                break;
            }

            // If a connection was accepted
            if (socket != null) {
                synchronized (BluetoothChatService.this) {
                    switch (mState) {
                    case STATE_LISTEN:
                    case STATE_CONNECTING:
                        // Situation normal. Start the connected thread.
                        connected(socket, socket.getRemoteDevice());
                        break;
                    case STATE_NONE:
                    case STATE_CONNECTED:
                        // Either not ready or already connected. Terminate new socket.
                        try {
                            socket.close();
                        } catch (IOException e) {
                            Log.e(TAG, "Could not close unwanted socket", e);
                        }
                        break;
                    }
                }
            }
        }
        if (D) Log.i(TAG, "END mAcceptThread, socket Type: ");

    }

デバッグを行ったところ、 で次のエラーが発生しましたAccept Threadsocket = mmServerSocket.accept();ラインで。

[2011-05-27 13:52:21 - DeviceMonitor]Adb connection Error:An existing connection was forcibly closed by the remote host
[2011-05-27 13:52:21 - Logcat]An existing connection was forcibly closed by the remote host
java.io.IOException: An existing connection was forcibly closed by the remote host
    at sun.nio.ch.SocketDispatcher.read0(Native Method)
    at sun.nio.ch.SocketDispatcher.read(Unknown Source)
    at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source)
    at sun.nio.ch.IOUtil.read(Unknown Source)
    at sun.nio.ch.SocketChannelImpl.read(Unknown Source)
    at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:395)
    at com.android.ddmlib.Device.executeShellCommand(Device.java:284)
    at com.android.ddmuilib.logcat.LogPanel$3.run(LogPanel.java:527)

[2011-05-27 13:52:23 - DeviceMonitor]Connection attempts: 1
[2011-05-27 13:52:25 - DeviceMonitor]Connection attempts: 2
[2011-05-27 13:52:27 - DeviceMonitor]Connection attempts: 3
[2011-05-27 14:33:48 - Device]Error during Sync: timeout.
[2011-05-27 16:49:43 - ddms]null
java.lang.NullPointerException
    at com.android.ddmlib.Client.sendAndConsume(Client.java:572)
    at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
    at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
    at com.android.ddmlib.Client.getJdwpPacket(Client.java:671)
    at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
    at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)

[2011-05-27 16:49:43 - ddms]null
java.lang.NullPointerException
    at com.android.ddmlib.Client.sendAndConsume(Client.java:572)
    at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
    at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
    at com.android.ddmlib.Client.getJdwpPacket(Client.java:671)
    at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
    at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)
4

2 に答える 2

2

このコードは、HTC Hero では機能しません。Samsung などの他のデバイスでテストしたところ、同じエラーは発生しないことがわかりました。

于 2011-05-30T13:12:53.277 に答える
0

Bluetooth が HTC Hero で正しく実装されていないという一般的な苦情を見てきました。完全なソリューションがここにあります

于 2013-01-05T02:26:35.357 に答える