Android 蓝牙读取特定字符串
Android Bluetooth read specific string
我的 android 程序现在可以接收像 "Test" 或 "Test 123".
这样的字符串
现在,我需要接收传入数据的特定字符串。
例如:~100 *200 $300。
Textbox1 = "100", Textbox2 = "200", Textbox3 = "300",如何通过过滤符号来分离这3个数据?
请帮忙。
这是我当前的蓝牙代码。
private byte read()
{
byte dataRead = 0;
try
{
dataRead = (byte) inputStream.read();
}
catch(IOException readException)
{
toastText = "Failed to read from input stream: " + readException.getMessage();
Toast.makeText(Trial_Blood_Pressure.this, toastText, Toast.LENGTH_SHORT).show();
}
return dataRead;
}
if (flag)
{
final byte data = read();
readMessageHandler.post(new Runnable()
{
public void run()
{
String message;
if (data != 1){
/* if(txtReceived.getText().toString().equals("ON") || txtReceived.getText().toString().equals("OFF"))
{
txtReceived.setText("");
}*/
message = txtReceived.getText().toString() + (char)data;}
else{
message = "";}
txtReceived.setText(message);
}
});
}
使用相同的分隔符。即:"10000"
.
然后您可以使用 String.split()
创建一个包含单独值的字符串数组。
我的 android 程序现在可以接收像 "Test" 或 "Test 123".
这样的字符串
现在,我需要接收传入数据的特定字符串。
例如:~100 *200 $300。
Textbox1 = "100", Textbox2 = "200", Textbox3 = "300",如何通过过滤符号来分离这3个数据?
请帮忙。
这是我当前的蓝牙代码。
private byte read()
{
byte dataRead = 0;
try
{
dataRead = (byte) inputStream.read();
}
catch(IOException readException)
{
toastText = "Failed to read from input stream: " + readException.getMessage();
Toast.makeText(Trial_Blood_Pressure.this, toastText, Toast.LENGTH_SHORT).show();
}
return dataRead;
}
if (flag)
{
final byte data = read();
readMessageHandler.post(new Runnable()
{
public void run()
{
String message;
if (data != 1){
/* if(txtReceived.getText().toString().equals("ON") || txtReceived.getText().toString().equals("OFF"))
{
txtReceived.setText("");
}*/
message = txtReceived.getText().toString() + (char)data;}
else{
message = "";}
txtReceived.setText(message);
}
});
}
使用相同的分隔符。即:"10000"
.
然后您可以使用 String.split()
创建一个包含单独值的字符串数组。