我的问题与 android 设备连接并能够通过蓝牙与另一台设备通信。
一方面我有一个 android 2.3.7 的 android 设备,另一方面我有一个 PIONEER DEH-6400BT。我可以成功连接这两个设备并完成使用无线电免提蓝牙通信时解释的所有操作。
我想做的事情如下:我想通过蓝牙(使用蓝牙插座)连接到收音机,并且能够对与收音机列出联系人/添加联系人/的能力连接的收音机做任何事情显示联系人正在拨号/或只是在收音机显示屏上显示某种字符串。
为了实现这一点,我从 BluetoothChat 示例开始,并使用InsecureBluetooth 类,一开始我开始连接普通的蓝牙免提设备。令人惊讶的是,我通过蓝牙套接字实现了与设备的连接,并且收到了启动整个通信协议 (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 的常规方式,但即使使用常规免提设备,我也无法连接。我也尝试过使用反射方式,但这也不起作用。
我也尝试过配对/未配对连接/断开状态的任何组合,但没有运气。
我渴望任何提示。