[英]Android BluetoothSocket::connect() throws an exception
I can not initiate the bluetooth connection between Java server(using bluecove 2.1.1 on Windows 7 x64, external bluetooth dongle
) and Android
client(OS version 2.3.6). 我无法启动Java服务器(
bluecove 2.1.1 on Windows 7 x64, external bluetooth dongle
使用bluecove 2.1.1 on Windows 7 x64, external bluetooth dongle
)与Android
客户端(操作系统版本2.3.6)之间的蓝牙连接。
Device discovery works normally, but I cannot connect to service running on PC( BluetoothSocket::connect() throw an exception
). 设备发现正常运行,但是我无法连接到PC上运行的服务(
BluetoothSocket::connect() throw an exception
)。 Below is a very primitive version of server and client(I use MAC address
of bluetooth dongle
for reducing code length): 以下是服务器和客户端的非常原始的版本(我使用
bluetooth dongle
MAC address
来减少代码长度):
Java Server 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());
}
}
}
Android Client Android客户端
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) {
}
}
}
Here is core of the problem(I guess): 这是问题的核心(我想):
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
I try to use reflection: 我尝试使用反射:
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
clientSocket = (BluetoothSocket) m.invoke(device, 1);
but it also not work. 但它也不起作用。
For over a week I can not solve this problem, I would be grateful if you help with the decision. 一个多星期以来,我无法解决此问题,如果您能帮助您做出决定,我将不胜感激。
Here is the log file: 这是日志文件:
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]
Problem solved, simply verification code on my device and on PC was different, but Windows shown device in paired devices list. 问题解决了,只是我的设备和PC上的验证码不同,但是Windows在配对的设备列表中显示了该设备。 I repair device and it works.
我维修设备,并且可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.