我期待獲取/讀取我連接幾個Android設備後傳遞的數據,到目前爲止我配對,連接並傳輸它們之間的信息,但不知道如何實現閱讀部分,在這裏我不確定是否應該使用createRfcommSocketToServiceRecord或listenUsingRfcommWithServiceRecord爲此目的創建閱讀套接字。從Android上的藍牙數據傳輸中讀取數據
我有兩個屏幕,其中一個是用戶按下按鈕併發送信息,另一個是接收者按下另一個按鈕並讀取數據,我想知道同步是否不正確,然後按「發送」按鈕然後「閱讀」按鈕連接不可用,或者如果這個實現不能一起推薦。
這些都是我的兩次嘗試:
嘗試1:
//Executed after the user press the read data button
private void connectToServerSocket(BluetoothDevice device, UUID uuid) {
try{
BluetoothServerSocket serverSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(device.getName(),uuid);
//Here is where I get the error:
//io to Server Socket JSR82 Connection is not created, failed or aborted
BluetoothSocket clientSocket = serverSocket.accept();
// Start listening for messages.
StringBuilder incoming = new StringBuilder();
listenForMessages(clientSocket, incoming);
// Add a reference to the socket used to send messages.
transferSocket = clientSocket;
} catch (IOException ioe) {
this.printToast("Excep io toServerSocket:" + ioe.getMessage());
} catch (Exception e) {
this.printToast("Excep toServerSocket:" + e.getMessage());
}
}
嘗試2:
private void connectToServerSocket(BluetoothDevice device, UUID uuid) {
try{
BluetoothServerSocket clientSocket = device.createRfcommSocketToServiceRecord(uuid);
//clientSocket without method and invoke is not working either
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
clientSocket = (BluetoothSocket) m.invoke(device, 1);
//Here is where I get the error:
//io to Server Socket JSR82 Connection is not created, failed or aborted
clientSocket.connect();
// Start listening for messages.
StringBuilder incoming = new StringBuilder();
listenForMessages(clientSocket, incoming);
// Add a reference to the socket used to send messages.
transferSocket = clientSocket;
} catch (IOException ioe) {
this.printToast("Excep io toServerSocket:" + ioe.getMessage());
} catch (Exception e) {
this.printToast("Excep toServerSocket:" + e.getMessage());
}
}
在serverSocket.accept()
或clientSocket.connect()
我得到異常:
Connection is not created, failed or aborted
我將不勝感激,如果任何人都可以指導我獲取數據閱讀部分工作。謝謝。