![](/img/trans.png)
[英]Is there way to know Bluetooth connection status to a device during app startup in android?
[英]android bluetooth chat app (device loses connection immediately)
我在這里經歷了多個線程,卻沒有遇到我遇到的問題的答案。
我的設置:我有一台Mac PC,正在將其用作虛擬串行端口來與我的Android Nexus S手機通信。 在電話上運行藍牙聊天應用程序,並將其作為客戶端與我設置的虛擬通信進行交談。
最初,我嘗試使用2部android手機嘗試藍牙聊天應用程序,以確認其正常工作。 我可以來回發送短信。
我的用例:我有一個讀取RFid標簽並將數據發送到android手機以收集信息的設備。
我現在使用PC代表我的設備。
++++++++++++++++++可以解決這個問題,
我嘗試通過手機連接到PC,最初我得到“正在連接...”狀態欄的更新,大約15秒后,我收到一條吐司消息,說“我已連接到PC”,但是當我收到“設備斷開連接”吐司。 然后狀態欄轉到“未連接”
當我逐步調試器時,它似乎在藍牙聊天應用程序的以下部分失敗。 特別是這一行( bytes = mmInStream.read(buffer);
)
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
當我查看logcat時,輸入輸出上的read()的I / O異常是“ software caused connection abort
”。
問題:這與我的虛擬端口設置不正確有關嗎? 我已將終端啟動,並等待使用/ 9600 baud屏幕命令在/dev/tty.Nexus ....上接收輸入
否則,我認為輸入流連接到的套接字可能不可用。 我將其打印到日志中,看來它不是NULL。 每次我逐步通過時都會死於ConnectThread而不是ConnectedThread中 。
代碼的以下部分:特別是這一行( mmSocket.connect();
)
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);
}
我想知道套接字變量是否由於多線程而失去作用域,並且套接字正在傳遞?
謝謝
如何在Mac PC上設置虛擬串行帖子。 由於您嘗試在2部手機上運行該應用程序且該應用程序正常運行,因此我認為問題出在PC上。
我已經發布了關於Android和Java藍牙條目這里 。 希望它會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.