我困在這個奇怪的問題上。我正在做的是通過藍牙通信2臺設備。當我發送像ßàæé這樣的特殊字符時,我會收到像這樣的問號?????在另一臺設備上。下面是從輸入流i轉換成緩衝字符串以這種方式讀出後,在接收側將特殊字符轉換爲字符串
private class ConnectedThread extends Thread {
private final Socket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(Socket socket) {
Log.d("ConnectionService", "create ConnectedThread");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.d("ConnectionService", "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.i("ConnectionService", "BEGIN mConnectedThread");
byte[] buffer = new byte[4096];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
if(bytes==0){
break;
}
String message = new String(buffer, 0, bytes);
Log.d("PC-DATA", message);
inputAnalyzer(message);
} catch (IOException e) {
Log.d("ConnectionService", "disconnected", e);
connectionLost();
break;
}
}
connectionLost();
}
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
} catch (IOException e) {
Log.d("ConnectionService", "Exception during write", e);
}
}
public void cancel() {
try {
mmSocket.close();
connection = false;
} catch (IOException e) {
Log.d("ConnectionService", "close() of connect socket failed",
e);
}
}
}
的代碼。
String message = new String(buffer, 0, bytes);
這是錯誤還是其他我錯過了! 有什麼幫助嗎?
你在哪裏看到問號?在實際數據中,還是在打印數據時記錄/屏幕?您的字體可能只是沒有在第二種情況下定義的那些字符。 – 2013-03-19 17:23:03
在實際數據中。 – Ahmed 2013-03-19 18:55:39