這是來自Google示例代碼BluetoothChat的Standard的一段代碼。 BluetoothChatService.java無法連接到藍牙設備
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (mState == STATE_CONNECTED) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
當我嘗試使用
mChatService.connect(device, false);
設備連接我得到異常
java.io.IOException: bt socket closed, read return: -1
at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:872)
at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
at java.io.InputStream.read(InputStream.java:163)
任何幫助,將不勝感激以下。
根據日誌,bt連接嘗試失敗。所以你可以嘗試系統藍牙連接你的設備。設備是否需要配對? –
設備已經配對。 – Nikki