// ZigbeeAppAcitity.java
package com.misoo.ZigbeeApp;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
public class ZigbeeAppActivity extends Activity implements OnClickListener {
protected static final long DelayMillis = 100;
myReceiver receiver;
private byte[] deviceId = new byte[8];
private String deviceIdStr = "";
private int nodeCount;
private boolean mPowerStatus ;
private final String VALUE = "value";
String power_status, power_tips;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override protected void onResume() {
super.onResume();
//------------------------------------------------------
// 准备接收来自myService/myBinder的广播信息
receiver = new myReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.MY_BROADCAST");
registerReceiver(receiver, filter);
super.onResume();
//------------------------------------------------------
initNative(); //初始化JNI本地函数
getStatus = true;
startGetStatus();
}
@Override protected void onPause() {
super.onPause();
uninitNative();
getStatus = false;
}
// -----------------------------------------------
private boolean getStatus;
private void startGetStatus(){
new Thread(){
public void run() {
while (getStatus) {
getStatusNative(); //调用本地函数
try {
sleep(DelayMillis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}.start();
}
// ----------- Callback from JNI 本地函数 -------------
private void reportNodeId(int index, byte id0,
byte id1, byte id2, byte id3, byte id4, byte id5, byte id6, byte id7)
{
deviceId = setByteArray(id0, id1, id2, id3, id4, id5, id6, id7);
deviceIdStr = bytes2HexString(deviceId);
}
private void reportPowerStatus(byte id0, byte id1, byte id2, byte id3, byte id4, byte id5,
byte id6, byte id7, int SocketNum , int SocketStatus)
{
String power_status = SocketStatus == 1 ? "Close" : "OPEN";
String power_tips = "Power"+ SocketNum + " Status is " + status;
// (SocketNum == 1) 代表 POWERSTATUS
// (SocketNum == 0x81) 代表 POWERLEDSTATUS))
}
//---------------------------------------------------------------------------------------//
public void SetPowerStatus( int status){
switch(status){
case 0:
SetPowerStatusNative(deviceId, 1, 1);
break;
case 1:
SetPowerStatusNative(deviceId, 1, 0);
break;
default:
break;
}
}
// -------------------------------------------------------------------------------------
static{
System.loadLibrary("zigbee");
}
//----------------------------------------------------------------------------------------
// --------------- 宣告JNI本地函数 ---------------------------------------------
private native int initNative();
private native int getStatusNative();
private native int uninitNative();
private native int resetNative();
private native int reconnetNative();
private native int getNodeCountNative();
private native int getNodeIdNative(int index);
private native int SetSwitchStatusNative(byte[] DeviceId, int num, int stauts);
private native int GetSwitchStatusNative(byte[] DeviceId, int num);
private native int SetBrightnessNative(byte[] DeviceId, int num, int vol);
private native int GetBrightnessNative(byte[] DeviceId, int num);
private native int SetPowerStatusNative(byte[] DeviceId, int num, int stauts);
private native int GetPowerStatusNative(byte[] DeviceId, int num);
private native int IRStudyNative(byte[] DeviceId, int keynum);
private native int IRSendNative(byte[] DeviceId, int keynum);
private native int GetWindowStatusNative(byte[] DeviceId);
private native int GetInputStatusNative(byte[] DeviceId, int num);
private native int SetOutputStatusNative(byte[] DeviceId, int num, int stauts);
private native int GetOutputStatusNative(byte[] DeviceId, int num);
private native int SetCurtainStatusNative(byte[] DeviceId, int status);
private native int GetCurtainStatusNative(byte[] DeviceId);
private native int GetTempHumiStatusNative(byte[] DeviceId);
// -------------------------------------------------------------------------------
private byte[] setByteArray(byte id0, byte id1, byte id2,
byte id3, byte id4, byte id5, byte id6, byte id7){
byte[] resultByte = new byte[8];
resultByte[0] = id0; resultByte[1] = id1; resultByte[2] = id2;
resultByte[3] = id3; resultByte[4] = id4; resultByte[5] = id5;
resultByte[6] = id6; resultByte[7] = id7; return resultByte;
}
private String bytes2HexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = bArray.length - 1; i >= 0; i--) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2) sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
// ---------------------------------------------------------------------
class myReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
int tempInt;
tempInt = intent.getIntExtra("inta", 101);
if(tempInt != -1)
SetPowerStatus(tempInt); // 设定开关状态
}
}
}