我需要根据从蓝牙rfcomm频道
收到的数据更新TextView这是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;
}
}
};
该项目本身基于Android开发者网站的蓝牙聊天示例程序。我修改了蓝牙聊天程序,以便从自定义蓝牙设备发送和接收rfcomm数据
case MESSAGE_READ:
是来自蓝牙设备的传入rfcomm数据以字节
的形式接收的地方我将字节values
更改为String
,然后更改为short
,我想在TextView中将其显示为十六进制值,以比较我从rfcomm频道获取的内容是否为蓝牙设备发送的相同值
我可以在case 1:
case MESSAGE_READ:
下看到我更新TextView的尝试
但是,TextView不会更新是否调用append()函数的setText()函数。
该行
txtViewDebug = (TextView) findViewById(R.id.txtView_textViewDebug);
在onCreate()
,但那也没有用。
我在阅读here之后尝试致电invalidate()
,但这不起作用
为什么不从此处理程序函数内部更新TextView?我在我的界面添加了另一个命令按钮,并添加了一个简单的txtViewDebug.setText()
,它按预期工作,但我需要能够使用我在此处理函数中收到的值更新它。
为什么没有TextView更新,我该如何解决这个问题?
答案 0 :(得分:0)
必须在ui线程上设置文本
所以尝试将代码包装在runOnUiThread