我有兩個Android設備,我想要使用藍牙連接,並通過RFCOMM通道傳輸數據。我只有一個一個設備接收數據,而其他設備發送它...兩個Android設備之間的RFCOMM連接?
使用此代碼,我能夠連接到其他設備,並開始收聽RFCOMM通道:
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
socket = (BluetoothSocket) m.invoke(device, 2);
socket.connect();
class BasicThread implements Runnable{
public void run() {
try {
InputStream stream = socket.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(stream));
while (true){
Log.d("myapp", "now listening...");
latestLine = r.readLine();
Log.d("myapp", latestLine);
}
} catch (IOException e) {
}
}
}
new Thread(new BasicThread()).run();
使用其他設備,我實現了一個監聽套接字是這樣的:
Method m = blue.getClass().getMethod("listenUsingRfcommOn", new Class[] { int.class });
BluetoothServerSocket socket = (BluetoothServerSocket) m.invoke(blue, 2);
BluetoothSocket sock = socket.accept();
Log.d("myapp", "Connected...\n\n\n\n\n\n\n\n");
OutputStream s = sock.getOutputStream();
final PrintWriter out = new PrintWriter(s);
它們都連接上的RFCOMM通道2,都可以看到海誓山盟,然而,第二設備始終保持擋在BluetoothSocket sock = socket.accept();
任何幫助?
是否有一個不使用公共方法的原因http://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html –
也你說如何連接通道2? –