我正面臨以下問題: 我通過藍牙接口,一個平板android和一個藍牙設備(如閱讀器條形碼)連接兩個設備,直到現在沒問題,問題是,當通過藍牙設備讀取條形碼並將其發送到平板電腦時,條形碼有時以兩部分發送,例如,如果我讀取內容爲「212154521212」的條形碼,則該平板電腦收到「2121」,在「 54521212「,任何人都知道告訴我該怎麼做才能避免這種情況?通過藍牙設備發送數據到平板電腦的錯誤
謝謝先進。
我的代碼從藍牙設備中讀取的數據:
[代碼] 私有類ConnectedThread擴展Thread {
private final InputStream mmInStream;
private BluetoothSocket socket;
public ConnectedThread(BluetoothSocket socket) {
this.socket = socket;
InputStream tmpIn = null;
try {
tmpIn = socket.getInputStream();
} catch (IOException e) {
new LogDeErrosRodesTablet(e);
Log.e(TAG, e.getMessage());
Log.e(TAG, "Erro no construtor da classe ConnectedThread.");
}
mmInStream = tmpIn;
}
public void run() {
// continua lendo o inputstream até ocorrer um erro
while (true) {
int read = 0;
byte[] buffer = new byte[128];
do {
try {
read = mmInStream.read(buffer);
Log.e(TAG, "read: " + read);
final String data = new String(buffer, 0, read);
Log.e(TAG, "data: " + data);
//TODO
//send data only (bar code) only after read all
Bundle bundle = new Bundle();
bundle.putString(TelaInserirPedido.CODIGO_BARRAS, data);
Message message = new Message();
message.what = TelaInserirPedido.MSG_COD_BARRAS;
message.setData(bundle);
//Send a message with data
handler.sendMessage(message);
} catch(Exception ex) {
read = -1;
return;
}
Log.e(TAG, "inside while.");
} while (read > 0);
Log.e(TAG, "outside of while.");
}
}
public void cancel() {
try {
socket.close();
} catch (IOException e) { }
}
}
[/代碼]
什麼是OutputStream? –
您用來發送條形碼的OutputStream。 – user1888162
在任何時候我都使用過OutPutStream。 –