我有一个Arduino将GPS坐标发送到Android应用程序:
bt_port.println(lat);
bt_port.print ('.');
bt_port.println(lon);
在应用中接收:
void beginListenForData()
{
final Handler handler = new Handler();
stopThread = false;
buffer = new byte[1024];
Thread thread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopThread)
{
try
{
int byteCount = inputStream.available();
if(byteCount > 0)
{
byte[] rawBytes = new byte[byteCount];
inputStream.read(rawBytes);
final String emissorInput = new String(rawBytes,"UTF-8");
handler.post(new Runnable() {
public void run()
{
emissorGPS.setVisibility(View.VISIBLE);
emissorGPS.append(emissorInput);
emissorGPS.postDelayed(new Runnable() {
public void run() {
emissorGPS.setVisibility(View.INVISIBLE);
emissorGPS.setText("");
}
}, 1500);
}
});
}
}
catch (IOException ex)
{
stopThread = true;
}
}
}
});
thread.start();
}
已经尝试过,应用程序崩溃了:
String[] test = emissorInput.split("\\.");
String part1 = test[0];
String part2 = test[1];
工作parcialy因为它只是删除“。”但不能拆分字符串
StringTokenizer tokens = new StringTokenizer(emissorInput, "\\.");
String nextToken = tokens.nextToken();
任何人都可以指导我从哪里开始!?
提前致谢。
祝你好运
答案 0 :(得分:1)
假设您的String以Arduino的“lat.long”形式出现,您可以使用StringTokenizer,因为如果分割的字符串为null,则返回空字符串。
String inbound = "lat.long";
StringTokenizer st = new StringTokenizer(inbound, ".");
String lat = st.nextToken(); //this will contain "lat"
String helpDesk = st.nextToken(); //this will contain "long"