我正面临以下问题: 我通过蓝牙接口,一个平板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。 –