//注册一个广播接收器
IntentFilter TcpFilter = new IntentFilter();
TcpFilter.addAction("action.JBBUI");
broadcastReceiver = new JBBUIBroadcastReceiver();
registerReceiver(broadcastReceiver,TcpFilter);
private class JBBUIBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent Recvintent = getIntent();
String action = intent.getAction();
byte[] AreaCmd = {};
String str;
if("action.JBBUI".equals(action)){
AreaCmd = Recvintent.getByteArrayExtra("JBBCmd");//这里收到的数据AreaCmd 一致显示null
}
//Toast.makeText(jiboba.this,"收到指令",Toast.LENGTH_SHORT).show();
CmdAnalysis(AreaCmd);
}
}
广播是从tcpservice发过来的
//发送广播到activity
public void SendCMD(String str,byte[] cmd){
final Intent intent = new Intent();
//先判断指令属于哪一种设备
if((cmd[3] == (byte) 0x01) && (cmd[4] == (byte) 0x0B) && (cmd[5] == (byte) 0x05)){
intent.setAction("action.JBBUI");
intent.putExtra("JBBCmd",cmd);//这里的cmd是有数据的
sendBroadcast(intent);
}else if((cmd[3] == (byte) 0x01) && (cmd[4] == (byte) 0x07) && (cmd[5] == (byte) 0x05)){
intent.setAction("action.updateUI");
intent.putExtra("GoalCmd",cmd);
sendBroadcast(intent);
}
}