3

Android Bluetoothチャットサンプルに基づいて、ブルートゥース部分を備えたBluetoothゲームを書いています。テストする電話が 2 台あります。これが問題です。ある電話を別の電話に接続すると、「デバイスに接続できません」バンドルが表示されることがありますが、Bluetoothチャットサンプルを実行すると、これは表示されないため、デバイスの問題ではないと思います. Bluetooth チャットのサンプルを調べたことがあり、同じ問題を抱えている人はいますか?

例外を出力しようとしましたが、「java.io.IOException: Service discovery failed」のようなものです。そして、これが例外を引き起こすコードです。

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

    public ConnectThread(BluetoothDevice device) {
        mmDevice = device;
        BluetoothSocket tmp = null;

        // Get a BluetoothSocket for a connection with the
        // given BluetoothDevice
        try {
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
            Log.e(TAG, "create() failed", e);
        }
        mmSocket = tmp;
    }

    public void run() {
        Log.i(TAG, "BEGIN mConnectThread");
        setName("ConnectThread");

        // Always cancel discovery because it will slow down a connection
        mAdapter.cancelDiscovery();

        // Make a connection to the BluetoothSocket
        try {
            // This is a blocking call and will only return on a
            // successful connection or an exception
            mmSocket.connect();
        } catch (IOException e) {
            Log.e("error", e.toString());
            connectionFailed();
            // Close the socket
            try {
                mmSocket.close();
            } catch (IOException e2) {
                Log.e(TAG, "unable to close() socket during connection failure", e2);
            }
            // Start the service over to restart listening mode
            BluetoothChatService.this.start();
            return;
        }

        // Reset the ConnectThread because we're done
        synchronized (BluetoothChatService.this) {
            mConnectThread = null;
        }

        // Start the connected thread
        connected(mmSocket, mmDevice);
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of connect socket failed", e);
        }
    }
} 

正確な位置は
mmSocket.connect();

4

1 に答える 1