public void bytesToHex(byte[] in) {
final StringBuilder builder = new StringBuilder();
int count=0;
final int BATCHSIZE=20;
sendingData = true;
Log.d("byteToHex", "sendingData = true, start sending data.");
sendSerial("w"); //write command
Log.d("byteToHex", "sending w");
for(byte b : in) {
//mBluetoothGatt.setCharacteristicNotification(characteristicRX, enabled);
//byte[] a = mBluetoothGatt.readCharacteristic(characteristicRX);
while(!newData){
if(resendData == true){//resends previously sent string
sendSerial(sendByte);
Log.d("byteToHex", "resendData = true, resending: " + sendByte);
resendData = false; //reset resendData flag
}
} //wait for next w from mcu
builder.append(String.format("%02x", b));
if(builder.length()== BATCHSIZE ){
sendByte= builder.toString();
sendSerial(sendByte);
newData = false;
Log.d("byteToHex", "newData = false");
count+=BATCHSIZE;
Log.d("byteToHex", "Sent " + count/2 + " bytes");
textViewFileProgress.setText(count/2 + "/" + fileLength); //<- THIS SETTEXT DOES NOT WORK
builder.setLength(0); //reset the string builder
}
} //for(byte b : in)
//send remaining bytes
sendByte= builder.toString();
sendSerial(sendByte);
newData = false;
Log.d("byteToHex", "newData = false");
count+=builder.length();
Log.d("byteToHex", "Sent " + count/2 + " byte");
textViewFileProgress.setText(count/2 + "/" + fileLength);//<- THIS SETTEXT WORKS
builder.setLength(0); //reset the string builder
sentTerminator = true; //flag to tell BLE service to check if terminator is received on mcu
sendSerial("||"); //terminating command, tell teensy last hex has been sent
while(sentTerminator == true){ //while terminator not yet received
if(resendTerminator == true){ //
sendSerial("||");
Log.d("byteToHex", "resending terminator");
resendTerminator = false; //Resend complete. reset resendTerminator flag.
}
}
sendingData = false;
//return builder.toString();
}//public void bytesToHex(byte[] in)
我正在尝试将文本设置为textview以显示当前发送的字节数。
不知何故,我的函数中有2个完全相同的setText代码。 textViewFileProgress.setText(count/2 + "/" + fileLength);
其中一个在for循环中,这不起作用。
另一个在for循环之外,这是有效的。
我确信该程序运行了该代码,因为我能够在Android监视器中看到它之前的调试消息。
知道这是什么问题吗?
答案 0 :(得分:0)
试试这个。
Log.e("TAG",builder.length()+"");
if(builder.length()== BATCHSIZE ){
sendByte= builder.toString();
sendSerial(sendByte);
newData = false;
Log.d("byteToHex", "newData = false");
count+=BATCHSIZE;
Log.d("byteToHex", "Sent " + count/2 + " bytes");
textViewFileProgress.setText(count/2 + "/" + fileLength); //<- THIS SETTEXT DOES NOT WORK
builder.setLength(0); //reset the string builder
} else {
Log.e("TAG","length != 20");
}
您可以看到Logcat
信息
答案 1 :(得分:0)
考虑这个简单的代码。您在另一个后台线程中实现for循环,然后使用onPostExecute()
在主线程中设置文本
请注意,onPostExecute()
在主线程中运行而doInBackground()
在后台线程中运行,因此您无法在doInBackground()
public void outSideFunction()
{
//pass a string to doInBackground()
new TextSetterTask.execute("");
}
//implement an inner class
private class TextSetterTask extends AsyncTask<String,Void,String>
{
@Override
protected String doInBackground(String... params)
{
//loop
return //the string you want to set the text
//if the background thread is done it will call the onPostExecute
}
@Override
protected void onPostExecute(String result)
{
//set the text
}
}