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显示当前的字节数发送。TextView.setText不工作for循环
不知何故,我在我的函数中有2个完全相同的setText代码。 textViewFileProgress.setText(count/2 + "/" + fileLength);
其中之一是在for循环内,这是行不通的。
另一个是在for循环之外,它工作。
我确定该程序运行该代码,因为我能够在Android监视器中看到它之前的调试消息。
任何想法是什么问题?
'TextView的文本#setText()'只允许在程序的主线程中使用内部的AsyncTask类来设置'TextView'的文本。 – abcOfJavaAndCPP
@abcOfJavaAndCPP这个函数在'extends Activity'类中。其应用主页 – tzj
https://i.imgur.com/JNmuOwF.png 我的byteToHex函数在主线程中 – tzj