2

私の質問はAndroidデバイスに接続されており、Bluetooth経由で別のデバイスと通信できます。

一方では Android 2.3.7 を搭載した Android デバイスを使用し、もう一方では PIONEER DEH-6400BT を使用しています。2 つのデバイスを正常に接続でき、無線ハンズフリー Bluetooth 通信を使用するときに説明されているすべてのことを実行できます。

私がやろうとしていることは次のとおりです。Bluetooth経由で(Bluetoothソケットを使用して)ラジオに接続し、連絡先をリストする/連絡先を追加するラジオの機能に接続されているラジオで何でもできるようにしたいです/連絡先がダイヤルしていることを示します/またはラジオディスプレイに何らかの文字列を表示しているだけです。

これを実現するために、BluetoothChat の例に加えてInsecureBluetooth クラスを使用して開始し、最初に通常の Bluetooth ハンズフリー デバイスとの接続を開始しました。驚いたことに、Bluetoothsocket 経由でデバイスとの接続に成功し、通信プロトコル全体を開始する最初のメッセージ (AT+BRSF:27) を受信した後、すべての通信が中断され、何かを送信しても応答がありませんでした。これにより、デバイスに接続できるという希望が得られますが、続行する方法がわかりません.

これは最も関連性の高いコードです:

/**
 * This thread runs while listening for incoming connections. It behaves
 * like a server-side client. It runs until a connection is accepted
 * (or until cancelled).
 */
private class AcceptThread extends Thread {
    // The local server socket
    private final BluetoothServerSocket mmServerSocket;

    public AcceptThread() {
        BluetoothServerSocket tmp = null;

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

    public void run() {
        if (D) Log.d(TAG, "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();
                Log.i(TAG, ">>>>>>>>>>>>>>>>>>> SOCKEET accept() <<<<<<<<<<<<<<<<<<<<<<");
            } catch (IOException e) {
                Log.e(TAG, "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");
    }

    public void cancel() {
        if (D) Log.d(TAG, "cancel " + this);
        try {
            mmServerSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of server failed", e);
        }
    }
}

/**
 * This thread runs while attempting to make an outgoing connection
 * with a device. It runs straight through; the connection either
 * succeeds or fails.
 */
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);
                tmp = InsecureBluetooth.createRfcommSocket(mmDevice, 10, false);
            } 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) {
            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);
    }

行がコメントアウトされていることがわかります (tmp = device.createRfcommSocketToServiceRecord(MY_UUID);)、これは BluetoothSocket を作成する通常の方法ですが、この方法では通常のハンズフリー デバイスでも接続できませんでした。リフレクションの方法も試しましたが、うまくいきませんでした。

また、ペアリング/ペアリングなしの接続/切断ステータスの組み合わせを試してみましたが、うまくいきませんでした。

ヒントを切望しています。

4

0 に答える 0