通过蓝牙连接Android设备与免提汽车收音机

时间:2012-06-11 15:00:31

标签: android bluetooth

我的问题是与Android设备连接,能够通过蓝牙与其他设备通信。

一方面,我有一个Android设备与Android 2.3.7,另一方面,我有一个PIONEER DEH-6400BT。我可以成功连接这两个设备并完成使用无线免提蓝牙通信时所解释的所有内容。

我想做的事情如下: 我想通过蓝牙(使用蓝牙插座)连接到收音机,并且能够通过与收音机列出联系人/添加联系人/显示联系人正在拨打/或仅显示某些内容的能力相关的无线电做任何事情无线电显示器上的一种字符串。

为了实现这一点,我开始使用BluetoothChat示例并使用InsecureBluetooth class,并在开始时我开始连接普通的蓝牙免提设备。令人惊讶的是,我通过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的常规方式,但这样我甚至无法连接常规免提设备。我也尝试过使用反射方式,但这也不起作用。

我还尝试了配对/不配对连接/断开状态的任意组合,但没有运气。

我渴望任何提示。

0 个答案:

没有答案