蓝牙聊天示例 - 将接收到的数据显示到 TextView
Bluetooth Chat Sample - display received data to TextView
我正在使用蓝牙聊天示例在我的 Android 设备上从我的 Arduino 接收一些传感器数据,所以我停用了发送数据的代码并更改了接收代码,我得到了完整的字符串使用我的传感器数据(例如:Sensordata1、Sensordata2、Sensordata3、Sensordata4、Sensordata5),将其拆分为特定的值并将其作为聊天元素显示到 ListView。
这是 BluetoothChatFragment 中更改的代码:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
if(readMessage != null && readMessage.length() > 29) {
String seperateValues[] = readMessage.split(",");
String pressure_1 = seperateValues[0];
String pressure_2 = seperateValues[1];
String pressure_3 = seperateValues[2];
String pressure_4 = seperateValues[3];
String pressure_5 = seperateValues[4];
int pressure_1_int = Integer.parseInt(pressure_1);
int pressure_2_int = Integer.parseInt(pressure_2);
int pressure_3_int = Integer.parseInt(pressure_3);
int pressure_4_int = Integer.parseInt(pressure_4);
int pressure_5_int = Integer.parseInt(pressure_5);
float pressure_1_float = (Float.parseFloat(pressure_1)) / 10;
float pressure_2_float = (Float.parseFloat(pressure_2)) / 10;
float pressure_3_float = (Float.parseFloat(pressure_3)) / 10;
float pressure_4_float = (Float.parseFloat(pressure_4)) / 10;
float pressure_5_float = (Float.parseFloat(pressure_5)) / 10;
pressure_1 = Float.toString(pressure_1_float);
pressure_2 = Float.toString(pressure_2_float);
pressure_3 = Float.toString(pressure_3_float);
pressure_4 = Float.toString(pressure_4_float);
pressure_5 = Float.toString(pressure_5_float);
String completeString = pressure_1 + " " +
pressure_2 + " " + pressure_3 + " " + pressure_4 + " " + pressure_5;
mConversationArrayAdapter.add(completeString);
break;
这不是一种显示数据的好方法,所以我想在单个 TextView 字段中显示 5 个值...
我将 TextView 安装在 xml 文件 (fragment_bluetooth_chat.xml) 中,并使用以下命令调用 TextView:
TextView pressure1 = (TextView) findViewById(R.id.textView1);
TextView pressure2 = (TextView) findViewById(R.id.textView2);
TextView pressure3 = (TextView) findViewById(R.id.textView3);
TextView pressure4 = (TextView) findViewById(R.id.textView4);
TextView pressure5 = (TextView) findViewById(R.id.textView5);
但是Android工作室不接受findViewById!?
我的错误是什么?
非常感谢您的帮助!
这里是 xml (fragment_bluetooth_chat.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Druck1"
android:id="@+id/textView1"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:textColor="#575757" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Druck2"
android:id="@+id/textView2"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:textColor="#575757" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Druck3"
android:id="@+id/textView3"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:textColor="#575757" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Druck4"
android:id="@+id/textView4"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:textColor="#575757" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Druck5"
android:id="@+id/textView5"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:textColor="#575757" />
</LinearLayout>
将 TextView 添加到 BluetoothChatFragment 中的另一个 "Layout Views":
// Layout Views
private ListView mConversationView;
private EditText mOutEditText;
private Button mSendButton;
private TextView pressure1, pressure2, pressure3, pressure4, pressure5;
并将 TextViews 绑定到 'onViewCreated()' 方法中的 UI 元素:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
mConversationView = (ListView) view.findViewById(R.id.in);
mOutEditText = (EditText) view.findViewById(R.id.edit_text_out);
mSendButton = (Button) view.findViewById(R.id.button_send);
pressure1 = (TextView) view.findViewById(R.id.textView1);
pressure2 = (TextView) view.findViewById(R.id.textView2);
pressure3 = (TextView) view.findViewById(R.id.textView3);
pressure4 = (TextView) view.findViewById(R.id.textView4);
pressure5 = (TextView) view.findViewById(R.id.textView5);
}
请注意,在本例中是“view.findViewById()”。
我正在使用蓝牙聊天示例在我的 Android 设备上从我的 Arduino 接收一些传感器数据,所以我停用了发送数据的代码并更改了接收代码,我得到了完整的字符串使用我的传感器数据(例如:Sensordata1、Sensordata2、Sensordata3、Sensordata4、Sensordata5),将其拆分为特定的值并将其作为聊天元素显示到 ListView。
这是 BluetoothChatFragment 中更改的代码:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
if(readMessage != null && readMessage.length() > 29) {
String seperateValues[] = readMessage.split(",");
String pressure_1 = seperateValues[0];
String pressure_2 = seperateValues[1];
String pressure_3 = seperateValues[2];
String pressure_4 = seperateValues[3];
String pressure_5 = seperateValues[4];
int pressure_1_int = Integer.parseInt(pressure_1);
int pressure_2_int = Integer.parseInt(pressure_2);
int pressure_3_int = Integer.parseInt(pressure_3);
int pressure_4_int = Integer.parseInt(pressure_4);
int pressure_5_int = Integer.parseInt(pressure_5);
float pressure_1_float = (Float.parseFloat(pressure_1)) / 10;
float pressure_2_float = (Float.parseFloat(pressure_2)) / 10;
float pressure_3_float = (Float.parseFloat(pressure_3)) / 10;
float pressure_4_float = (Float.parseFloat(pressure_4)) / 10;
float pressure_5_float = (Float.parseFloat(pressure_5)) / 10;
pressure_1 = Float.toString(pressure_1_float);
pressure_2 = Float.toString(pressure_2_float);
pressure_3 = Float.toString(pressure_3_float);
pressure_4 = Float.toString(pressure_4_float);
pressure_5 = Float.toString(pressure_5_float);
String completeString = pressure_1 + " " +
pressure_2 + " " + pressure_3 + " " + pressure_4 + " " + pressure_5;
mConversationArrayAdapter.add(completeString);
break;
这不是一种显示数据的好方法,所以我想在单个 TextView 字段中显示 5 个值... 我将 TextView 安装在 xml 文件 (fragment_bluetooth_chat.xml) 中,并使用以下命令调用 TextView:
TextView pressure1 = (TextView) findViewById(R.id.textView1);
TextView pressure2 = (TextView) findViewById(R.id.textView2);
TextView pressure3 = (TextView) findViewById(R.id.textView3);
TextView pressure4 = (TextView) findViewById(R.id.textView4);
TextView pressure5 = (TextView) findViewById(R.id.textView5);
但是Android工作室不接受findViewById!? 我的错误是什么?
非常感谢您的帮助!
这里是 xml (fragment_bluetooth_chat.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Druck1"
android:id="@+id/textView1"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:textColor="#575757" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Druck2"
android:id="@+id/textView2"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:textColor="#575757" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Druck3"
android:id="@+id/textView3"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:textColor="#575757" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Druck4"
android:id="@+id/textView4"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:textColor="#575757" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Druck5"
android:id="@+id/textView5"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:textColor="#575757" />
</LinearLayout>
将 TextView 添加到 BluetoothChatFragment 中的另一个 "Layout Views":
// Layout Views
private ListView mConversationView;
private EditText mOutEditText;
private Button mSendButton;
private TextView pressure1, pressure2, pressure3, pressure4, pressure5;
并将 TextViews 绑定到 'onViewCreated()' 方法中的 UI 元素:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
mConversationView = (ListView) view.findViewById(R.id.in);
mOutEditText = (EditText) view.findViewById(R.id.edit_text_out);
mSendButton = (Button) view.findViewById(R.id.button_send);
pressure1 = (TextView) view.findViewById(R.id.textView1);
pressure2 = (TextView) view.findViewById(R.id.textView2);
pressure3 = (TextView) view.findViewById(R.id.textView3);
pressure4 = (TextView) view.findViewById(R.id.textView4);
pressure5 = (TextView) view.findViewById(R.id.textView5);
}
请注意,在本例中是“view.findViewById()”。