问题描述
- 关于android手机接收空调机返回数据显示的编程问题。
-
这个是关于用android手机接收空调机返回数据显示的编程问题:
在主页HomeActivtiy.java里面,接收空调传过来的数据代码是:
private Runnable mLoop = new Runnable() { //被上面实例调用
public void run() {
int len;
byte[] rbuf = new byte[128];do { len = mSerial.read(rbuf); //return mSerial.read(rbuf,rbuf.length) (注意:这个就是接收空调机的函数) // rbuf[len] = 0; if (len > 0) { if (len == 35) { System.arraycopy(rbuf, 0, mReceiveByte.data, 0, 35);//《J6》第64页 mReceiveByte.length = 0; //这里是给它赋值; 这里与if (mReceiveByte.length != 0) 比较;调用下面的类 CheckReceiveBytes(len, mReceiveByte.data); // KJLoger.debug("Read Length : " + len); Message msg = new Message(); //《疯狂》第210页 msg.what = 1; msg.obj = mReceiveByte.data; mHandler.sendMessage(msg); mReceiveByte.clear(); } else { //len != 35(在len>0里)即:0<len<35,并且35<len<128 if (mReceiveByte.length != 0) { //与上面作比较; System.arraycopy(rbuf, 0, mReceiveByte.data, mReceiveByte.length, len); //《J6》第64页 CheckReceiveBytes(len, mReceiveByte.data); //调用下面的函数 // KJLoger.debug("Read Length : " + len); Message msg = new Message(); //《疯狂》第210页 msg.what = 1; msg.obj = mReceiveByte.data; mHandler.sendMessage(msg); mReceiveByte.clear(); //调用下面的类的函数 } else { //mReceiveByte.length == 0 System.arraycopy(rbuf, 0, mReceiveByte.data, 0, len); //《J6》第64页
// Intent intent = new Intent(); //注意:怎么把Message转换为Intent
// sendBroadcast(intent);
}} } try { Thread.sleep(100); } catch (InterruptedException e) { //public void printStackTrace (): //Writes a printable representation of this Throwable's stack trace to the System.err stream. //写一个这个抛出堆栈的一个可打印的代表 到 系统错误流。 e.printStackTrace(); } if (mStop) { mRunningMainLoop = false; return; } } while (true); } //run()函数结束 在显示数据的主页ControlActivity2.java中的代码是: final Handler mHandler = new Handler() { int lenCtrl2;
// ha = new HomeActivity(this);
ObjectInputStream ois;@Override public void handleMessage(Message msg) { if (msg.what == 1) { ByteArrayInputStream bais = new ByteArrayInputStream((byte[]) msg.obj); try { ObjectInputStream ois = new ObjectInputStream(bais); } catch (StreamCorruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } do{ try { lenCtrl2 = (Integer) ois.readObject(); } catch (OptionalDataException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(lenCtrl2>0){ if(lenCtrl2 == 35){ System.arraycopy(ois, 0, rbufCtrl2.data, 0, 35); if(rbufCtrl2.data.length == 0) CheckReceiveBytes(lenCtrl2,rbufCtrl2.data); rbufCtrl2.clear(); }else{ if(rbufCtrl2.length!=0){ System.arraycopy(ois, 0, rbufCtrl2.data, 0, lenCtrl2); CheckReceiveBytes(lenCtrl2,rbufCtrl2.data); rbufCtrl2.clear(); }else{ System.arraycopy(ois, 0, rbufCtrl2.data, 0, lenCtrl2); //这个才是正常接收。 String str1 = Byte.toString(rbufCtrl2.data[18]); String str2 = Byte.toString(rbufCtrl2.data[19]); String str3 = Byte.toString(rbufCtrl2.data[20]); String str4 = Byte.toString(rbufCtrl2.data[24]); String str5 = Byte.toString(rbufCtrl2.data[25]); temperature_theindoor.setText(str1); temperature_theoutdoor.setText(str2); humidity_indoor.setText(str3); temperature_indoorintake.setText(str4); temperature_outdoorintake.setText(str5); } } } }while(true); // temperature_theindoor.setText((char[]) (msg.obj), 18, 1); // temperature_theoutdoor.setText((char[]) (msg.obj), 19, 1); // humidity_indoor.setText((char[]) (msg.obj), 20, 20); // temperature_indoorintake.setText((char[]) (msg.obj), 24, 1); // temperature_outdoorintake.setText((char[]) (msg.obj), 25, 1); } } private String IntToHex2(byte Value) { //被下面的函数调用 ;实际上就是Byte-->Char[](Character)16进制-->String char HEX2[] = { Character.forDigit((Value >> 4) & 0x0F, 16), Character.forDigit(Value & 0x0F, 16) }; String Hex2Str = new String(HEX2); return Hex2Str; } private void CheckReceiveBytes(int len, byte[] rbuf) { StringBuffer rr = new StringBuffer(); //《Java程序设计》第87页 rr.append(len + "n"); rr.append(rbuf.length + "n"); for (byte b : rbuf) { //foreach语句 rr.append(IntToHex2(b) + " "); //调用上面的函数 } Message msgtest = new Message(); msgtest.what = 77; //public int what Message类的域成员 msgtest.obj = rr.toString(); mHandler.sendMessage(msgtest); } } ; // 这里是new Handler()的结束
// Looper.loop();
// } // 这里是run()的结束// }
// }
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub } @Override public void mOnClickHeadMenu() { // TODO Auto-generated method stub } @Override public void mOnClickHeadBack() { // TODO Auto-generated method stub } @Override public void mOnClickHeadBack2() { // TODO Auto-generated method stub }
}
但是这个程序编译出来之后,接到空调机里面还是接收不到数据,求解答。
解决方案
两者之间是通过上面进行通信的?
解决方案二:
程序编译出来没有你想要的数据,你应该断点调试程序,从调用mLoop中的run方法开始,一句一句地执行代码,查看每行代码是否执行正确,是否得到预期的结果。
解决方案三:
哦,知道了,谢谢各位的回答!
时间: 2024-11-30 21:13:29