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)
I am trying to set the text to my textview to display the current number of bytes sent.
Somehow, i have 2 of the exact same setText code in my function. textViewFileProgress.setText(count/2 + "/" + fileLength);
one of them is inside a for loop, which does not work.
the other is outside the for loop, which works.
I am sure the program ran that code, as I am able to see the debug messages before it in Android monitor.
Any idea what is the problem?
Try this .
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");
}
And you can see the Logcat
info
Consider this simple code. You implement your for loop in another background thread then set the text in main thread using onPostExecute()
Note that onPostExecute()
run in the main thread while doInBackground()
run in the background thread so you cannot set the text in 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
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.