文本视图未在 RFCOMM 处理程序中更新



>我需要根据从蓝牙 rfcomm 通道接收的数据更新文本视图

这是 rfcomm 处理程序函数:

// The Handler that gets information back from the BluetoothChatService
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
txtViewDebug = (TextView) findViewById(R.id.txtView_textViewDebug);
switch (msg.what) {
case MESSAGE_STATE_CHANGE:
if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
switch (msg.arg1) {
case BluetoothChatService.STATE_CONNECTED:
// mTitle.setText(R.string.title_connected_to);
// mTitle.append(mConnectedDeviceName);
mConversationArrayAdapter.clear();
break;
case BluetoothChatService.STATE_CONNECTING:
//  mTitle.setText(R.string.title_connecting);           // ORIGINALLY PRESENT
break;
case BluetoothChatService.STATE_LISTEN:
case BluetoothChatService.STATE_NONE:
// mTitle.setText(R.string.title_not_connected);        // ORIGINALLY PRESENT
// mTitle.setText(R.string.not_connected);
break;
}
break;
case MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
mConversationArrayAdapter.add("Me:  " + writeMessage);
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
switch(rfcomm_state_flag){
case 0:
mConversationArrayAdapter.add(mConnectedDeviceName+":  " + readMessage);
break;
case 1:
// current received data is the lower half of the CSR audio signal
for(int i=0; i<AUDIO_FRAME_SIZE; i++){
audio_sig_from_csr[i] = (short) readMessage.charAt(i);
// txtViewDebug.append(Integer.toHexString(audio_sig_from_csr[i] & 0xffff)  + "n" ) ;
// txtViewDebug.setText(txtViewDebug.getText() + Integer.toHexString(audio_sig_from_csr[i] & 0xffff)  + "n" );
}
txtViewDebug.append("Test value" + "n") ;
// txtViewDebug.invalidate();
break;
case 2:
// current received data is the upper half of the CSR audio signal
rfcomm_state_flag = 0;      // reset this flag to 0
for(int i=0; i<AUDIO_FRAME_SIZE; i++){
tempVar = (short) readMessage.charAt(i);
audio_sig_from_csr[i] += (tempVar << 8);
}
try {
for(int i=0;i <audio_sig_from_csr.length; i++)
// savToDisk.writeShort(Short.reverseBytes(lin[i]));        // from wav file creator file
savToDisk.writeShort(audio_sig_from_csr[i] );
// payload += lin.length; // use this line if lin is an array of byte
// payload = payload + (lin.length)*2;
fd.sync();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(BluetoothChat.this, "File written", Toast.LENGTH_LONG).show();
break;
}
break;
case MESSAGE_DEVICE_NAME:
// save the connected device's name
mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
Toast.makeText(getApplicationContext(), "Connected to "
+ mConnectedDeviceName, Toast.LENGTH_SHORT).show();
break;
case MESSAGE_TOAST:
Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
Toast.LENGTH_SHORT).show();
break;
}
}
};  

该项目本身基于安卓开发者网站上的蓝牙聊天示例程序。我修改了蓝牙聊天程序,以从自定义蓝牙设备发送和接收 rfcomm 数据

case MESSAGE_READ:  

是来自蓝牙设备的传入 RFCOMM 数据以字节形式接收的地方

我将字节values更改为String,然后更改为short,我想在 TextView 中将其显示为十六进制值,以比较我从 rfcomm 通道获得的值是否与蓝牙设备发送的值相同

我更新文本视图的尝试可以在case MESSAGE_READ:case 1:下看到

但是,TextView 不会更新我是否调用 append() 函数的 setText() 函数。

该行

txtViewDebug = (TextView) findViewById(R.id.txtView_textViewDebug);  

onCreate()但这也不起作用。

在这里阅读后,我尝试打电话给invalidate()但这也不起作用

为什么不从此处理程序函数内部更新文本视图?我在界面中添加了另一个命令按钮并添加了一个简单的txtViewDebug.setText()它按预期工作,但我需要能够使用我在此处理程序函数中收到的值更新它。

为什么文本视图不更新,如何解决此问题?

必须在 ui 线程上设置文本 因此,请尝试将代码包装在runOnUiThread

相关内容

  • 没有找到相关文章

最新更新