我无法启动 Java 服务器(使用bluecove 2.1.1 on Windows 7 x64, external bluetooth dongle
)和Android
客户端(操作系统版本 2.3.6)之间的蓝牙连接。
设备发现工作正常,但我无法连接到 PC( BluetoothSocket::connect() throw an exception
) 上运行的服务。下面是一个非常原始的服务器和客户端版本(我用它MAC address
来bluetooth dongle
减少代码长度):
Java 服务器
import java.io.*;
import javax.microedition.io.*;
import javax.bluetooth.*;
public class RFCOMMServer {
public static void main(String args[]) {
try {
StreamConnectionNotifier service = (StreamConnectionNotifier) Connector
.open("btspp://localhost:"
+ new UUID("0000110100001000800000805F9B34FB",
false).toString() + ";name=helloService");
StreamConnection conn = (StreamConnection) service.acceptAndOpen();
System.out.println("Connected");
DataInputStream in = new DataInputStream(conn.openInputStream());
DataOutputStream out = new DataOutputStream(conn.openOutputStream());
String received = in.readUTF(); // Read from client
System.out.println("received: " + received);
out.writeUTF("Echo: " + received); // Send Echo to client
conn.close();
service.close();
} catch (IOException e) {
System.err.print(e.toString());
}
}
}
安卓客户端
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.UUID;
import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.os.Handler;
import android.widget.LinearLayout;
import android.widget.ArrayAdapter;
public class AndroidBluetoothEchoClientActivity extends ListActivity {
LinearLayout layout;
private ArrayAdapter<String> mArrayAdapter;
final Handler handler = new Handler();
final Runnable updateUI = new Runnable() {
public void run() {
mArrayAdapter.add(bluetoothClient.getBluetoothClientData());
}
};
BluetoothClient bluetoothClient;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
this.setListAdapter(mArrayAdapter);
bluetoothClient = new BluetoothClient(handler, updateUI);
bluetoothClient.start();
}
}
class BluetoothClient extends Thread {
BluetoothAdapter mBluetoothAdapter;
private String data = null;
final Handler handler;
final Runnable updateUI;
public BluetoothClient(Handler handler, Runnable updateUI) {
this.handler = handler;
this.updateUI = updateUI;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
public String getBluetoothClientData() {
return data;
}
public void run() {
BluetoothSocket clientSocket = null;
// Client knows the MAC address of server
BluetoothDevice mmDevice = mBluetoothAdapter
.getRemoteDevice("00:15:83:07:CE:27");
try {
clientSocket = mmDevice.createRfcommSocketToServiceRecord(UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB"));
mBluetoothAdapter.cancelDiscovery();
clientSocket.connect();
DataInputStream in = new DataInputStream(clientSocket.getInputStream());
DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
out.writeUTF("Hello"); // Send to server
data = in.readUTF(); // Read from server
handler.post(updateUI);
} catch (Exception e) {
}
}
}
这是问题的核心(我猜):
BluetoothDevice mmDevice = mBluetoothAdapter
.getRemoteDevice("00:15:83:07:CE:27");//normally get device
try {
clientSocket = mmDevice.createRfcommSocketToServiceRecord(UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB"));//here also no problem
mBluetoothAdapter.cancelDiscovery();
clientSocket.connect();//here throw an exception
我尝试使用反射:
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
clientSocket = (BluetoothSocket) m.invoke(device, 1);
但它也行不通。
一个多星期以来我都无法解决这个问题,如果你能帮助我做出决定,我将不胜感激。
这是日志文件:
02-27 08:32:33.656: W/ActivityThread(10335): Application edu.ius.rwisman.AndroidBluetoothEchoClient is waiting for the debugger on port 8100...
02-27 08:32:33.687: I/System.out(10335): Sending WAIT chunk
02-27 08:32:33.882: I/System.out(10335): Debugger has connected
02-27 08:32:33.882: I/System.out(10335): waiting for debugger to settle...
02-27 08:32:34.085: I/System.out(10335): waiting for debugger to settle...
02-27 08:32:34.289: I/System.out(10335): waiting for debugger to settle...
02-27 08:32:34.492: I/System.out(10335): waiting for debugger to settle...
02-27 08:32:34.687: I/System.out(10335): waiting for debugger to settle...
02-27 08:32:34.890: I/System.out(10335): waiting for debugger to settle...
02-27 08:32:35.093: I/System.out(10335): waiting for debugger to settle...
02-27 08:32:35.296: I/System.out(10335): debugger has settled (1493)
02-27 08:32:35.390: I/ApplicationPackageManager(10335): cscCountry is not German : SER
02-27 08:32:35.875: D/dalvikvm(10335): threadid=9: still suspended after undo (sc=1 dc=1)
02-27 08:32:45.351: D/BluetoothSocket(10335): create BluetoothSocket: type = 1, fd = -1, uuid = [00001101-0000-1000-8000-00805f9b34fb], port = -1
02-27 08:32:45.351: D/BLZ20_WRAPPER(10335): blz20_init: initializing...
02-27 08:32:45.351: D/BTL_IFC_WRP(10335): wsactive_init: init active list
02-27 08:32:45.460: D/BLZ20_WRAPPER(10335): blz20_init: success
02-27 08:32:45.460: D/BTL_IFC_WRP(10335): wrp_sock_create: CTRL
02-27 08:32:45.460: D/BTL_IFC_WRP(10335): wrp_alloc_new_sock: wrp_alloc_new_sock sub 1
02-27 08:32:45.460: D/BTL_IFC_WRP(10335): wrp_sock_create: 43
02-27 08:32:45.460: D/BTL_IFC_WRP(10335): wrp_sock_connect: wrp_sock_connect brcm.bt.btlif:9000 (43)
02-27 08:32:45.460: D/BTL_IFC_WRP(10335): wrp_sock_connect: BTLIF_MAKE_LOCAL_SERVER_NAME return name: brcm.bt.btlif.9000
02-27 08:32:45.460: D/BTL_IFC_WRP(10335): wrp_sock_connect: wrp_sock_connect ret:-1 server name:brcm.bt.btlif.9000
02-27 08:32:45.460: E/BTL_IFC_WRP(10335): ##### ERROR : wrp_sock_connect: connect failed (Connection refused)#####
02-27 08:32:45.460: E/BTL_IFC(10335): ##### ERROR : btl_ifc_ctrl_connect: control channel failed Connection refused#####
02-27 08:32:45.460: D/BTL_IFC_WRP(10335): wrp_close_full: wrp_close (43:-1) [brcm.bt.btlif]
02-27 08:32:45.460: D/BTL_IFC_WRP(10335): wsactive_del: delete wsock 43 from active list [ad3bc380]