Commit 10041a18578cf5f7105bbe569c6d68b848cdeb5d

Authored by Penley
1 parent 24d26a70

switch basic files

Showing 36 changed files with 2217 additions and 0 deletions
  1 +apply plugin: 'com.android.application'
  2 +apply plugin: 'realm-android'
  3 +
  4 +android {
  5 + compileSdkVersion 25
  6 + buildToolsVersion "25.0.2"
  7 +
  8 + defaultConfig {
  9 + applicationId "com.qnbar.switchcontrol"
  10 + minSdkVersion 18
  11 + targetSdkVersion 25
  12 + versionCode 1
  13 + versionName "1.0"
  14 +
  15 + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  16 +
  17 + }
  18 +
  19 + sourceSets {
  20 + main {
  21 + jniLibs.srcDirs = ['libs']
  22 + }
  23 + }
  24 +
  25 + buildTypes {
  26 + release {
  27 + minifyEnabled false
  28 + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  29 + }
  30 + }
  31 +}
  32 +
  33 +dependencies {
  34 + compile fileTree(dir: 'libs', include: ['*.jar'])
  35 + androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
  36 + exclude group: 'com.android.support', module: 'support-annotations'
  37 + })
  38 + compile 'com.android.support:appcompat-v7:25.2.0'
  39 + compile group: 'com.google.code.gson', name: 'gson', version: '2.4'
  40 + testCompile 'junit:junit:4.12'
  41 +}
... ...
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
  1 +# Add project specific ProGuard rules here.
  2 +# By default, the flags in this file are appended to flags specified
  3 +# in /Users/wugian/Documents/android-sdk-macosx/tools/proguard/proguard-android.txt
  4 +# You can edit the include path and order by changing the proguardFiles
  5 +# directive in build.gradle.
  6 +#
  7 +# For more details, see
  8 +# http://developer.android.com/guide/developing/tools/proguard.html
  9 +
  10 +# Add any project specific keep options here:
  11 +
  12 +# If your project uses WebView with JS, uncomment the following
  13 +# and specify the fully qualified class name to the JavaScript interface
  14 +# class:
  15 +#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
  16 +# public *;
  17 +#}
... ...
  1 +package com.qnbar.switchcontrol;
  2 +
  3 +import android.content.Context;
  4 +import android.support.test.InstrumentationRegistry;
  5 +import android.support.test.runner.AndroidJUnit4;
  6 +
  7 +import org.junit.Test;
  8 +import org.junit.runner.RunWith;
  9 +
  10 +import static org.junit.Assert.*;
  11 +
  12 +/**
  13 + * Instrumentation test, which will execute on an Android device.
  14 + *
  15 + * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
  16 + */
  17 +@RunWith(AndroidJUnit4.class)
  18 +public class ExampleInstrumentedTest {
  19 + @Test
  20 + public void useAppContext() throws Exception {
  21 + // Context of the app under test.
  22 + Context appContext = InstrumentationRegistry.getTargetContext();
  23 +
  24 + assertEquals("com.qnbar.switchcontrol", appContext.getPackageName());
  25 + }
  26 +}
... ...
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3 + package="com.qnbar.switchcontrol">
  4 +
  5 + <uses-permission android:name="android.permission.INTERNET" />
  6 +
  7 + <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
  8 + android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
  9 + <activity android:name=".MainActivity">
  10 + <intent-filter>
  11 + <action android:name="android.intent.action.MAIN" />
  12 +
  13 + <category android:name="android.intent.category.LAUNCHER" />
  14 + </intent-filter>
  15 + </activity>
  16 + </application>
  17 +
  18 +</manifest>
\ No newline at end of file
... ...
  1 +package com.adroplat.fist_switch;
  2 +
  3 +import android.app.Application;
  4 +import android.content.Context;
  5 +
  6 +/**
  7 + * Created by wugian on 2017/3/8.
  8 + */
  9 +
  10 +public class SwitchApplication extends Application {
  11 + private static Context context;
  12 +
  13 + public static Context getAppContext() {
  14 + return context;
  15 + }
  16 +
  17 + @Override
  18 + public void onCreate() {
  19 + super.onCreate();
  20 + context = this.getApplicationContext();
  21 + }
  22 +}
... ...
  1 +package com.adroplat.fist_switch.config;
  2 +
  3 +import android.util.Log;
  4 +
  5 +/**
  6 + * Created by wugian on 2017/3/8.
  7 + */
  8 +
  9 +public class GlobalVar {
  10 + public static final String CHARSET_UTF8 = "utf-8";
  11 + public static final byte MAX_BYTE = (byte) 0xFF;
  12 + static GlobalVar globalVar;
  13 +
  14 +
  15 + public static GlobalVar getLogUtils() {
  16 + return globalVar;
  17 + }
  18 +
  19 + public void i(String s) {
  20 + Log.d("GlobalVar", s);
  21 + }
  22 +}
... ...
  1 +package com.adroplat.fist_switch.jni;
  2 +
  3 +
  4 +import com.adroplat.fist_switch.config.GlobalVar;
  5 +import com.adroplat.fist_switch.utils.ByteUtils;
  6 +
  7 +import java.io.Serializable;
  8 +import java.io.UnsupportedEncodingException;
  9 +import java.util.ArrayList;
  10 +
  11 +/**
  12 + * Created by WLJ on 16-3-8.
  13 + */
  14 +public class Device implements Serializable, Cloneable {
  15 + private static final long serialVersionUID = -1055014034866886590L;
  16 +
  17 + private static Device instance;
  18 +
  19 + private Device() {
  20 + }
  21 +
  22 + public static Device getInstance() {
  23 + if (null == instance) {
  24 + instance = new Device();
  25 + }
  26 + Device hub;
  27 + try {
  28 + hub = (Device) instance.clone();
  29 + } catch (CloneNotSupportedException e) {
  30 + hub = new Device();
  31 + }
  32 + return hub;
  33 + }
  34 +
  35 + /**
  36 + * 控制ID
  37 + */
  38 + private long ControllerId;
  39 + /**
  40 + * 命令类型
  41 + */
  42 + private int CmdType;
  43 + /**
  44 + * 错误码
  45 + */
  46 + private byte ErrorCode;
  47 + /**
  48 + * 设备IP
  49 + */
  50 + private byte[] DeviceIp;
  51 + /**
  52 + * 设备端口
  53 + */
  54 + private int DevicePort;
  55 + /**
  56 + * 设备公网IP
  57 + */
  58 + private byte[] MappedIp;
  59 + /**
  60 + * 设备公网端口
  61 + */
  62 + private int MappedPort;
  63 + /**
  64 + * 设备号
  65 + */
  66 + private byte[] DeviceNum;
  67 + /**
  68 + * 设备口令
  69 + */
  70 + private byte[] DeviceKey;
  71 + /**
  72 + * 是否是广域网控制
  73 + */
  74 + private boolean IsWan;
  75 + /**
  76 + * 子设备
  77 + */
  78 + private ArrayList<SubDevice> SubDevices;
  79 +
  80 + public long getControllerId() {
  81 + return ControllerId;
  82 + }
  83 +
  84 + public void setControllerId(long controllerId) {
  85 + ControllerId = controllerId;
  86 + }
  87 +
  88 + public int getCmdType() {
  89 + return CmdType;
  90 + }
  91 +
  92 + public void setCmdType(int cmdType) {
  93 + CmdType = cmdType;
  94 + }
  95 +
  96 + public byte getErrorCode() {
  97 + return ErrorCode;
  98 + }
  99 +
  100 + public void setErrorCode(byte errorCode) {
  101 + ErrorCode = errorCode;
  102 + }
  103 +
  104 + public byte[] getDeviceIp() {
  105 + return DeviceIp;
  106 + }
  107 +
  108 + public String getStringDeviceIp() {
  109 + String strIp = "null";
  110 + if (null != DeviceIp) {
  111 + char point = '.';
  112 + StringBuilder builder = new StringBuilder();
  113 + for (byte b : DeviceIp) {
  114 + builder.append(b & GlobalVar.MAX_BYTE).append(point);
  115 + }
  116 + builder.deleteCharAt(builder.length() - 1);
  117 + strIp = builder.toString();
  118 + }
  119 + return strIp;
  120 + }
  121 +
  122 + public void setDeviceIp(byte[] deviceIp) {
  123 + DeviceIp = deviceIp;
  124 + }
  125 +
  126 + public int getDevicePort() {
  127 + return DevicePort;
  128 + }
  129 +
  130 + public void setDevicePort(int devicePort) {
  131 + DevicePort = devicePort;
  132 + }
  133 +
  134 + public byte[] getMappedIp() {
  135 + return MappedIp;
  136 + }
  137 +
  138 + public void setMappedIp(byte[] mappedIp) {
  139 + MappedIp = mappedIp;
  140 + }
  141 +
  142 + public int getMappedPort() {
  143 + return MappedPort;
  144 + }
  145 +
  146 + public void setMappedPort(int mappedPort) {
  147 + MappedPort = mappedPort;
  148 + }
  149 +
  150 + public byte[] getDeviceNum() {
  151 + return DeviceNum;
  152 + }
  153 +
  154 + public void setDeviceNum(byte[] deviceNum) {
  155 + DeviceNum = deviceNum;
  156 + }
  157 +
  158 + public byte[] getDeviceKey() {
  159 + return DeviceKey;
  160 + }
  161 +
  162 + public String getDeviceStrKey() {
  163 + String strKey = "";
  164 + try {
  165 + strKey = new String(DeviceKey, GlobalVar.CHARSET_UTF8);
  166 + } catch (UnsupportedEncodingException e) {
  167 + e.printStackTrace();
  168 + }
  169 + return strKey;
  170 + }
  171 +
  172 + public void setDeviceKey(byte[] deviceKey) {
  173 + DeviceKey = deviceKey;
  174 + }
  175 +
  176 + public boolean isWan() {
  177 + return IsWan;
  178 + }
  179 +
  180 + public void setWan(boolean wan) {
  181 + IsWan = wan;
  182 + }
  183 +
  184 + public ArrayList<SubDevice> getSubDevices() {
  185 + return SubDevices;
  186 + }
  187 +
  188 + public void setSubDevices(ArrayList<SubDevice> subDevices) {
  189 + this.SubDevices = subDevices;
  190 + }
  191 +
  192 + /**
  193 + * DeviceNum转换成long
  194 + *
  195 + * @return
  196 + */
  197 + public long getLongDeviceNum() {
  198 + return ByteUtils.macArrayToLong(DeviceNum);
  199 + }
  200 +
  201 + /**
  202 + * DeviceKey转换成long
  203 + *
  204 + * @return
  205 + */
  206 + public long getLongDeviceKey() {
  207 + return ByteUtils.macArrayToLong(DeviceKey);
  208 + }
  209 +
  210 + /**
  211 + * MappedIp转换成int
  212 + *
  213 + * @return
  214 + */
  215 + public int getIntMappedIp() {
  216 + return ByteUtils.byteArrayToInt(MappedIp);
  217 + }
  218 +
  219 + /**
  220 + * DeviceIp转换成int
  221 + *
  222 + * @return
  223 + */
  224 + public int getIntDeviceIp() {
  225 + return ByteUtils.byteArrayToInt(DeviceIp);
  226 + }
  227 +}
... ...
  1 +package com.adroplat.fist_switch.jni;
  2 +
  3 +import android.content.Context;
  4 +import android.content.Intent;
  5 +import android.os.AsyncTask;
  6 +import android.support.v4.content.LocalBroadcastManager;
  7 +
  8 +import com.adroplat.fist_switch.SwitchApplication;
  9 +import com.adroplat.fist_switch.config.GlobalVar;
  10 +import com.adroplat.fist_switch.utils.ToastUtils;
  11 +import com.adroplat.fist_switch.utils.WifiUtils;
  12 +import com.adroplat.fist_switch.utils.protocol.one.FistProtocol;
  13 +import com.google.gson.Gson;
  14 +
  15 +import java.util.ArrayList;
  16 +
  17 +import io.realm.Realm;
  18 +
  19 +/**
  20 + * Created by WLJ on 2016/1/14.
  21 + */
  22 +public class FistJni {
  23 + static {
  24 + System.loadLibrary("fist_ndk");
  25 + }
  26 +
  27 + private static FistJni instance;
  28 +
  29 + private FistJni() {
  30 + }
  31 +
  32 + public static FistJni getInstance() {
  33 + if (null == instance) {
  34 + instance = new FistJni();
  35 + }
  36 + return instance;
  37 + }
  38 +
  39 + private static Intent intent;
  40 +
  41 + public static Intent getIntent() {
  42 + intent = new Intent();
  43 + return intent;
  44 + }
  45 +
  46 + public static final String CONFIG_DONE = "CONFIG_DONE";
  47 + public static final String SEARCH_RESPONSE = "SEARCH_RESPONSE";
  48 + public static final String LINK_STATUS_RESPONSE = "LINK_STATUS_RESPONSE";
  49 + public static final String LINK_RESPONSE = "LINK_RESPONSE";
  50 + public static final String CTRL_PWD_RESPONSE = "CTRL_PWD_RESPONSE";
  51 + public static final String START_ADD = "START_ADD";
  52 + public static final String STOP_ADD = "STOP_ADD";
  53 + public static final String ADD_DONE = "ADD_DONE";
  54 + public static final String CONTROL_REQUEST_TIMEOUT = "CONTROL_REQUEST_TIMEOUT";
  55 + public static final String CONTROL_RESPONSE = "CONTROL_RESPONSE";
  56 + public static final String CONTROL_DONE = "CONTROL_DONE";
  57 + public static final String CONTROL_OVER = "CONTROL_OVER";
  58 + public static final String BYTE_ERROR_CODE = "BYTE_ERROR_CODE";
  59 + public static final String JSON_DEVICE = "JSON_DEVICE";
  60 +
  61 + private static void configDone(Device device) {
  62 + sendJsonDevice(CONFIG_DONE, device);
  63 + }
  64 +
  65 + private static void online(Device device, String jsonDevice) {
  66 + Realm realm = Realm.getDefaultInstance();
  67 +// Hub host = realm.where(Hub.class)
  68 +// .equalTo(Hub.Companion.getDEVICE_NUM(), device.getLongDeviceNum()).findFirst();
  69 +// if (null != host) {//已存储,更新IP,查询子设备
  70 +// realm.beginTransaction();
  71 +// host.setIp(device.getIntDeviceIp());
  72 +// host.setLAN(true);
  73 +// realm.commitTransaction();
  74 +// FistJni.getInstance().query(jsonDevice);
  75 +// }
  76 + realm.close();
  77 + }
  78 +
  79 + private static void saveRemote(Device device) {
  80 + if (null == device) return;
  81 + Realm realm = Realm.getDefaultInstance();
  82 +// Hub host = realm.where(Hub.class)
  83 +// .equalTo(Hub.Companion.getDEVICE_NUM(), device.getLongDeviceNum()).findFirst();
  84 +// if (null != host) {//已存储,更新IP,查询子设备
  85 +// realm.beginTransaction();
  86 +// host.setMapIp(device.getIntMappedIp());
  87 +// host.setMapPort(device.getMappedPort());
  88 +// host.setPeerIp(device.getIntDeviceIp());
  89 +// host.setPeerPort(device.getDevicePort());
  90 +// host.setLAN(false);
  91 +// host.setAlreadyHaveRemoteIP(true);
  92 +// realm.commitTransaction();
  93 +// }
  94 + realm.close();
  95 + }
  96 +
  97 + private static Gson gson;
  98 +
  99 + public static Gson getGson() {
  100 + if (null == gson) {
  101 + gson = new Gson();
  102 + }
  103 + return gson;
  104 + }
  105 +
  106 + /**
  107 + * 发送广播
  108 + *
  109 + * @param action
  110 + * @param device
  111 + */
  112 + private static void sendJsonDevice(String action, Device device) {
  113 + if (null == action || action.isEmpty() || null == device) return;
  114 + Context context = SwitchApplication.getAppContext();
  115 + if (null == context) return;
  116 + Intent intent = getIntent();
  117 + intent.setAction(action);
  118 + intent.putExtra(JSON_DEVICE, device);
  119 + LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
  120 + }
  121 +
  122 + public static void fistListener(String jsonDevice) {
  123 + if (null == jsonDevice || jsonDevice.isEmpty()) return;
  124 + GlobalVar.getLogUtils().i("fistListener jsonDevice =" + jsonDevice);
  125 + Gson gson = getGson();
  126 + Device device = gson.fromJson(jsonDevice, Device.class);
  127 + char cmdTpye = (char) device.getCmdType();
  128 +
  129 + if (FistProtocol.CommondType.CMD_TYPE_SEARCH_RESPONSE == cmdTpye) {//查询响应
  130 + removeId(device);
  131 + online(device, jsonDevice);
  132 + sendJsonDevice(SEARCH_RESPONSE, device);
  133 +
  134 + } else if (FistProtocol.CommondType.CMD_TYPE_CONFIG_DONE == cmdTpye) {//配置完成处理
  135 + configDone(device);
  136 +
  137 + } else if (FistProtocol.CommondType.CMD_TYPE_ONLINE == cmdTpye) {//上线通知
  138 + removeId(device);
  139 + online(device, jsonDevice);
  140 + configDone(device);
  141 +
  142 + } else if (FistProtocol.CommondType.CMD_TYPE_QUERY_RESPONSE == cmdTpye) {
  143 + sendJsonDevice(ADD_DONE, device);
  144 + ArrayList<SubDevice> subDevices = device.getSubDevices();
  145 + if (!subDevices.isEmpty()) {//查询结果不为空
  146 + Realm realm = Realm.getDefaultInstance();
  147 +// long hostNum = device.getLongDeviceNum();
  148 +// RealmResults<Hub> hubs = realm.where(Hub.class)
  149 +// .equalTo(Hub.Companion.getDEVICE_NUM(), hostNum)
  150 +// .notEqualTo(Transform.Companion.getSYNC_CODE(), SyncState.DELETE.ordinal()).findAll();
  151 +// if (!hubs.isEmpty()) {
  152 +// int subSize = subDevices.size();
  153 +// for (int i = 0; i < subSize; i++) {
  154 +// SubDevice subDevice = subDevices.get(i);
  155 +// long subDeviceNum = subDevice.getSubDeviceNum();
  156 +// RealmResults<SwSubDev> swSubDevs = realm.where(SwSubDev.class)
  157 +// .equalTo(SwSubDev.Companion.getHOST_NUM(), hostNum)
  158 +// .equalTo(SwSubDev.Companion.getPARENT_NUM(), subDeviceNum)
  159 +// .findAll();
  160 +// if (!swSubDevs.isEmpty()) {//数据库已存储
  161 +// int lightingSize = swSubDevs.size();
  162 +// byte lingtingState = (byte) subDevice.getSubDeviceStatus();
  163 +// for (int j = 0; j < lightingSize; j++) {//更新开关状态
  164 +// SwSubDev lighting = swSubDevs.get(j);
  165 +// realm.beginTransaction();
  166 +// byte currentState = (byte) (lingtingState >> lighting.getDeviceNum() & 1);
  167 +// if (0 == currentState) {
  168 +// lighting.setOn(false);
  169 +// } else {
  170 +// lighting.setOn(true);
  171 +// }
  172 +// realm.commitTransaction();
  173 +// }
  174 +// }
  175 +// }
  176 +// }
  177 +
  178 + realm.close();
  179 + }
  180 +// sendJsonDevice(LightingGroupAdapter.REFRESH, device);
  181 +
  182 + } else if (FistProtocol.CommondType.CMD_TYPE_ADD_RESPONSE == cmdTpye) {
  183 + sendJsonDevice(START_ADD, device);
  184 +
  185 + } else if (FistProtocol.CommondType.CMD_TYPE_ADD_OVER == cmdTpye) {//子设备结束入网
  186 +// if (0 == device.getIntDeviceIp()) return;
  187 +// FistJni.stopAddSubDevice(jsonDevice);
  188 +
  189 + } else if (FistProtocol.CommondType.CMD_TYPE_CONTROL == cmdTpye) {//控制超时
  190 + sendJsonDevice(CONTROL_REQUEST_TIMEOUT, device);
  191 +
  192 + } else if (FistProtocol.CommondType.CMD_TYPE_CONTROL_RESPONSE == cmdTpye) {//控制应答
  193 + sendJsonDevice(CONTROL_RESPONSE, device);
  194 +
  195 + } else if (FistProtocol.CommondType.CMD_TYPE_CONTROL_DONE_RESPONSE == cmdTpye) {//单个控制结果
  196 + sendJsonDevice(CONTROL_DONE, device);
  197 +
  198 + } else if (FistProtocol.CommondType.CMD_TYPE_CONTROL_OVER == cmdTpye) {
  199 + sendJsonDevice(CONTROL_OVER, device);
  200 + GlobalVar.getLogUtils().i("CONTROL_OVER");
  201 + } else if (FistProtocol.CommondType.CMD_NET_LINK_STATUS_RESPONSE == cmdTpye) {
  202 + sendJsonDevice(LINK_STATUS_RESPONSE, device);
  203 +
  204 + } else if (FistProtocol.CommondType.CMD_LINK_RESPONSE == cmdTpye) {
  205 + sendJsonDevice(LINK_RESPONSE, device);
  206 +
  207 + } else if (FistProtocol.CommondType.CMD_MODIFY_CTRL_PWD_RESPONSE == cmdTpye) {
  208 + GlobalVar.getLogUtils().i("FistProtocol.CommondType.CMD_MODIFY_CTRL_PWD_RESPONSE");
  209 + sendJsonDevice(CTRL_PWD_RESPONSE, device);
  210 +
  211 + } /*else if (RemoteProtocol.MsgType.SET_SUCCESS == cmdTpye) {//密码设置成功
  212 + GlobalVar.getLogUtils().i("SET_SUCCESS jsonDevice =" + jsonDevice);
  213 + long deviceNum = device.getLongDeviceNum();
  214 + Realm realm = Realm.getDefaultInstance();
  215 + Hub host = realm.where(Hub.class).equalTo(Hub.Companion.getDEVICE_NUM(), deviceNum).findFirst();
  216 + if (null != host) {
  217 + realm.beginTransaction();
  218 + host.setWanState(WanState.ON);
  219 + realm.commitTransaction();
  220 + }
  221 + realm.close();
  222 + } else if (RemoteProtocol.MsgType.SET_FAILED == cmdTpye) {//密码设置失败
  223 + GlobalVar.getLogUtils().i("SET_FAILED jsonDevice =" + jsonDevice);
  224 +
  225 + } else if (RemoteProtocol.MsgType.QUERY_SUCCESS == cmdTpye) {//查询对端成功
  226 + final long deviceNum = device.getLongDeviceNum();
  227 + ArrayList<Long> ids = HeService.getLocIds();
  228 + for (long id : ids) {
  229 + if (deviceNum == id) {
  230 + saveRemote(device);
  231 + device.setWan(true);
  232 + jsonDevice = gson.toJson(device);
  233 + querySubDevice(jsonDevice);
  234 + break;
  235 + }
  236 + }
  237 + removeId(device);
  238 +
  239 + } else if (RemoteProtocol.MsgType.QUERY_FAILED == cmdTpye) {//查询对端失败
  240 +
  241 + }*/
  242 + }
  243 +
  244 + /**
  245 + * 开始
  246 + */
  247 + public static native void start();
  248 +
  249 + /**
  250 + * 启动SKD
  251 + */
  252 + public void startSdk() {
  253 + new AsyncTask<Void, Void, Void>() {
  254 + @Override
  255 + protected Void doInBackground(Void... params) {
  256 + start();
  257 + return null;
  258 + }
  259 + }.execute();
  260 + }
  261 +
  262 + /**
  263 + * 移除查询到的主机ID
  264 + *
  265 + * @param device
  266 + */
  267 + private static void removeId(Device device) {
  268 +// ArrayList<Long> ids = HeService.getLocIds();
  269 +// final long deviceNum = device.getLongDeviceNum();
  270 +// for (long id : ids) {
  271 +// if (deviceNum == id) {
  272 +// ids.remove(id);
  273 +// break;
  274 +// }
  275 +// }
  276 + }
  277 +
  278 + /**
  279 + * 搜索设备
  280 + */
  281 + public static native void searchDevice();
  282 +
  283 + /**
  284 + * 查询子设备
  285 + */
  286 + public static native void querySubDevice(String jsonDevice);
  287 +
  288 + public void query(final String jsonDevice) {
  289 +// if (!HeService.isConn()) {
  290 +// return;
  291 +// }
  292 + new AsyncTask<Void, Void, Void>() {
  293 + @Override
  294 + protected Void doInBackground(Void... params) {
  295 + querySubDevice(jsonDevice);
  296 + return null;
  297 + }
  298 + }.execute();
  299 + }
  300 +
  301 + /**
  302 + * 开始添加子设备
  303 + */
  304 + public static native void startAddSubDevice(String jsonDevice);
  305 +
  306 + public void startAdd(final String jsonDevice) {
  307 + if (!(WifiUtils.isWifiConnected())) {
  308 + ToastUtils.toastShort("网络未连接");
  309 + return;
  310 + }
  311 + new AsyncTask<Void, Void, Void>() {
  312 + @Override
  313 + protected Void doInBackground(Void... params) {
  314 + startAddSubDevice(jsonDevice);
  315 + return null;
  316 + }
  317 + }.execute();
  318 + }
  319 +
  320 + /**
  321 + * 停止添加子设备
  322 + */
  323 + public static native void stopAddSubDevice(String jsonDevice);
  324 +
  325 + public void stopAdd(final String jsonDevice) {
  326 + if (!(WifiUtils.isWifiConnected())) {
  327 + ToastUtils.toastShort("网络未连接");
  328 + return;
  329 + }
  330 + new AsyncTask<Void, Void, Void>() {
  331 + @Override
  332 + protected Void doInBackground(Void... params) {
  333 + stopAddSubDevice(jsonDevice);
  334 + return null;
  335 + }
  336 + }.execute();
  337 + }
  338 +
  339 + /**
  340 + * 删除子设备
  341 + */
  342 + public static native void deleteSubDevices(String jsonDevice);
  343 +
  344 + public void delSub(final String jsonDevice) {
  345 +// if (!HeService.isConn()) {
  346 +// ToastUtils.toastShort("网络未连接");
  347 +// return;
  348 +// }
  349 + new AsyncTask<Void, Void, Void>() {
  350 + @Override
  351 + protected Void doInBackground(Void... params) {
  352 + deleteSubDevices(jsonDevice);
  353 + return null;
  354 + }
  355 + }.execute();
  356 + }
  357 +
  358 + /**
  359 + * 查询软件版本
  360 + */
  361 + public static native void querySoftwareVer(String jsonDevice);
  362 +
  363 + /**
  364 + * 查询工作模式
  365 + */
  366 + public static native void queryModel(String jsonDevice);
  367 +
  368 + public void sceneC(final String jsonDevice) {
  369 + if (!(WifiUtils.isWifiConnected())) {
  370 + ToastUtils.toastShort("网络未连接");
  371 + return;
  372 + }
  373 + new AsyncTask<Void, Void, Void>() {
  374 + @Override
  375 + protected Void doInBackground(Void... params) {
  376 + GlobalVar.getLogUtils().i("sceneC jsonDevice =" + jsonDevice);
  377 + sceneControl(jsonDevice);
  378 + return null;
  379 + }
  380 + }.execute();
  381 + }
  382 +
  383 + /**
  384 + * 情景控制
  385 + */
  386 + public static native void sceneControl(String jsonDevice);
  387 +
  388 + public void normalC(final String jsonDevice) {
  389 + if (!(WifiUtils.isWifiConnected())) {
  390 + ToastUtils.toastShort("网络未连接");
  391 + return;
  392 + }
  393 + new AsyncTask<Void, Void, Void>() {
  394 + @Override
  395 + protected Void doInBackground(Void... params) {
  396 + GlobalVar.getLogUtils().i("normalC jsonDevice =" + jsonDevice);
  397 + normalControl(jsonDevice);
  398 + return null;
  399 + }
  400 + }.execute();
  401 + }
  402 +
  403 + /**
  404 + * 普通控制
  405 + */
  406 + private static native void normalControl(String jsonDevice);
  407 +
  408 +
  409 + /**
  410 + * 设置远程密码
  411 + *
  412 + * @param jsonDevice
  413 + * @param pwd
  414 + */
  415 + private static native void setRemotePwd(String jsonDevice, String pwd);
  416 +
  417 + public void setPwd(final String jsonDevice, final String pwd) {
  418 + if (!(WifiUtils.isWifiConnected())) {
  419 + ToastUtils.toastShort("网络未连接");
  420 + return;
  421 + }
  422 + new AsyncTask<Void, Void, Void>() {
  423 + @Override
  424 + protected Void doInBackground(Void... params) {
  425 + setRemotePwd(jsonDevice, pwd);
  426 + return null;
  427 + }
  428 + }.execute();
  429 + }
  430 +
  431 + private static native void getRemoteCtrlIp(String jsonDevice, String random, String pwd);
  432 +
  433 + public void searchRecordDevice() {
  434 +// final ArrayList<Long> ids = HeService.getLocIds();
  435 +// if (ids.isEmpty()) return;
  436 +// new AsyncTask<Void, Void, Void>() {
  437 +//
  438 +// @Override
  439 +// protected Void doInBackground(Void... params) {
  440 +// searchDevice();
  441 +// Realm realm = Realm.getDefaultInstance();
  442 +// for (long id : ids) {
  443 +// Hub host = realm.where(Hub.class).equalTo(Hub.Companion.getDEVICE_NUM(), id).findFirst();
  444 +// if (null != host) {
  445 +// if (WanState.ON == host.getWanState()) {
  446 +// realm.beginTransaction();
  447 +// host.setAlreadyHaveRemoteIP(false);
  448 +// realm.commitTransaction();
  449 +// Device device = Device.getInstance();
  450 +// device.setDeviceNum(ByteUtils.longToMacArray(id));
  451 +// device.setDeviceKey(host.getWanPwd().getBytes());
  452 +// Gson gson = getGson();
  453 +// String jsonDevice = gson.toJson(device);
  454 +// GlobalVar.getLogUtils().i("searchRecordDevice = " + jsonDevice);
  455 +// String strRandom = RandomUtil.getRandomStr();
  456 +// String strPwd = host.getWanPwd();
  457 +// getRemoteCtrlIp(jsonDevice, strRandom, strPwd);
  458 +// }
  459 +// }
  460 +// }
  461 +// realm.close();
  462 +// return null;
  463 +// }
  464 +// }.execute();
  465 + }
  466 +
  467 + public void searchLocalDevice() {
  468 + new AsyncTask<Void, Void, Void>() {
  469 +
  470 + @Override
  471 + protected Void doInBackground(Void... params) {
  472 + searchDevice();
  473 + return null;
  474 + }
  475 + }.execute();
  476 + }
  477 +
  478 + /**
  479 + * 链接路由
  480 + *
  481 + * @param jsonDevice
  482 + * @param ssid
  483 + * @param pwd
  484 + */
  485 + public static native void linkRouter(String jsonDevice, String ssid, String pwd);
  486 +
  487 + public void getlinkRouter(final String jsonDevice, final String ssid, final String pwd) {
  488 + if (!(WifiUtils.isWifiConnected())) {
  489 + ToastUtils.toastShort("网络未连接");
  490 + return;
  491 + }
  492 + new AsyncTask<Void, Void, Void>() {
  493 + @Override
  494 + protected Void doInBackground(Void... params) {
  495 + GlobalVar.getLogUtils().i("getlinkRouter jsonDevice =" + jsonDevice);
  496 + linkRouter(jsonDevice, ssid, pwd);
  497 + return null;
  498 + }
  499 + }.execute();
  500 + }
  501 +
  502 + /**
  503 + * 修改密码或SSID
  504 + *
  505 + * @param jsonDevice
  506 + * @param ssid
  507 + * @param pwd
  508 + */
  509 + public static native void modifyAp(String jsonDevice, String ssid, String pwd);
  510 +
  511 + /**
  512 + * 链接状态
  513 + *
  514 + * @param jsonDevice
  515 + */
  516 + public static native void linkStatus(String jsonDevice);
  517 +
  518 + public void getLinkStatus(final String jsonDevice) {
  519 + if (!(WifiUtils.isWifiConnected())) {
  520 + ToastUtils.toastShort("网络未连接");
  521 + return;
  522 + }
  523 + new AsyncTask<Void, Void, Void>() {
  524 + @Override
  525 + protected Void doInBackground(Void... params) {
  526 + GlobalVar.getLogUtils().i("getLinkStatus jsonDevice =" + jsonDevice);
  527 + linkStatus(jsonDevice);
  528 + return null;
  529 + }
  530 + }.execute();
  531 + }
  532 +
  533 +
  534 + /**
  535 + * 修改控制密码
  536 + *
  537 + * @param jsonDevice
  538 + * @param pwd
  539 + */
  540 + public static native void modifyCtrlPWd(String jsonDevice, String pwd);
  541 +
  542 + public void modifyPwd(final String jsonDevice, final String pwd) {
  543 + if (!(WifiUtils.isWifiConnected())) {
  544 + ToastUtils.toastShort("网络未连接");
  545 + return;
  546 + }
  547 + new AsyncTask<Void, Void, Void>() {
  548 + @Override
  549 + protected Void doInBackground(Void... params) {
  550 + GlobalVar.getLogUtils().i("modifyPwd jsonDevice =" + jsonDevice);
  551 + modifyCtrlPWd(jsonDevice, pwd);
  552 + return null;
  553 + }
  554 + }.execute();
  555 + }
  556 +}
\ No newline at end of file
... ...
  1 +//package com.adroplat.fist_switch.jni;
  2 +//
  3 +//import android.content.Context;
  4 +//import android.content.Intent;
  5 +//import android.os.AsyncTask;
  6 +//import android.support.v4.content.LocalBroadcastManager;
  7 +//
  8 +//import com.adroplat.fist_switch.adapter.LightingGroupAdapter;
  9 +//import com.adroplat.fist_switch.config.GlobalVar;
  10 +//import com.adroplat.fist_switch.service.HeService;
  11 +//import com.adroplat.fist_switch.sqlite.Hub;
  12 +//import com.adroplat.fist_switch.sqlite.SwSubDev;
  13 +//import com.adroplat.fist_switch.sqlite.SyncState;
  14 +//import com.adroplat.fist_switch.sqlite.Transform;
  15 +//import com.adroplat.fist_switch.sqlite.WanState;
  16 +//import com.adroplat.fist_switch.ui.SwitchApp;
  17 +//import com.adroplat.fist_switch.utils.ByteUtils;
  18 +//import com.adroplat.fist_switch.utils.RandomUtil;
  19 +//import com.adroplat.fist_switch.utils.ToastUtils;
  20 +//import com.adroplat.fist_switch.utils.WifiUtils;
  21 +//import com.adroplat.fist_switch.utils.protocol.one.FistProtocol;
  22 +//import com.adroplat.fist_switch.utils.protocol.remote.RemoteProtocol;
  23 +//import com.google.gson.Gson;
  24 +//
  25 +//import java.util.ArrayList;
  26 +//
  27 +//import io.realm.Realm;
  28 +//import io.realm.RealmResults;
  29 +//
  30 +///**
  31 +// * Created by WLJ on 2016/1/14.
  32 +// */
  33 +//public class FistJni_back {
  34 +// static {
  35 +// System.loadLibrary("fist_ndk");
  36 +// }
  37 +//
  38 +// private static FistJni_back instance;
  39 +//
  40 +// private FistJni_back() {
  41 +// }
  42 +//
  43 +// public static FistJni_back getInstance() {
  44 +// if (null == instance) {
  45 +// instance = new FistJni_back();
  46 +// }
  47 +// return instance;
  48 +// }
  49 +//
  50 +// private static Intent intent;
  51 +//
  52 +// public static Intent getIntent() {
  53 +// intent = new Intent();
  54 +// return intent;
  55 +// }
  56 +//
  57 +// public static final String CONFIG_DONE = "CONFIG_DONE";
  58 +// public static final String SEARCH_RESPONSE = "SEARCH_RESPONSE";
  59 +// public static final String LINK_STATUS_RESPONSE = "LINK_STATUS_RESPONSE";
  60 +// public static final String LINK_RESPONSE = "LINK_RESPONSE";
  61 +// public static final String CTRL_PWD_RESPONSE = "CTRL_PWD_RESPONSE";
  62 +// public static final String START_ADD = "START_ADD";
  63 +// public static final String STOP_ADD = "STOP_ADD";
  64 +// public static final String ADD_DONE = "ADD_DONE";
  65 +// public static final String CONTROL_REQUEST_TIMEOUT = "CONTROL_REQUEST_TIMEOUT";
  66 +// public static final String CONTROL_RESPONSE = "CONTROL_RESPONSE";
  67 +// public static final String CONTROL_DONE = "CONTROL_DONE";
  68 +// public static final String CONTROL_OVER = "CONTROL_OVER";
  69 +// public static final String BYTE_ERROR_CODE = "BYTE_ERROR_CODE";
  70 +// public static final String JSON_DEVICE = "JSON_DEVICE";
  71 +//
  72 +// private static void configDone(Device device) {
  73 +// sendJsonDevice(CONFIG_DONE, device);
  74 +// }
  75 +//
  76 +// private static void online(Device device, String jsonDevice) {
  77 +// Realm realm = Realm.getDefaultInstance();
  78 +// Hub host = realm.where(Hub.class)
  79 +// .equalTo(Hub.Companion.getDEVICE_NUM(), device.getLongDeviceNum()).findFirst();
  80 +// if (null != host) {//已存储,更新IP,查询子设备
  81 +// realm.beginTransaction();
  82 +// host.setIp(device.getIntDeviceIp());
  83 +// host.setLAN(true);
  84 +// realm.commitTransaction();
  85 +// FistJni_back.getInstance().query(jsonDevice);
  86 +// }
  87 +// realm.close();
  88 +// }
  89 +//
  90 +// private static void saveRemote(Device device) {
  91 +// if (null == device) return;
  92 +// Realm realm = Realm.getDefaultInstance();
  93 +// Hub host = realm.where(Hub.class)
  94 +// .equalTo(Hub.Companion.getDEVICE_NUM(), device.getLongDeviceNum()).findFirst();
  95 +// if (null != host) {//已存储,更新IP,查询子设备
  96 +// realm.beginTransaction();
  97 +// host.setMapIp(device.getIntMappedIp());
  98 +// host.setMapPort(device.getMappedPort());
  99 +// host.setPeerIp(device.getIntDeviceIp());
  100 +// host.setPeerPort(device.getDevicePort());
  101 +// host.setLAN(false);
  102 +// host.setAlreadyHaveRemoteIP(true);
  103 +// realm.commitTransaction();
  104 +// }
  105 +// realm.close();
  106 +// }
  107 +//
  108 +// private static Gson gson;
  109 +//
  110 +// public static Gson getGson() {
  111 +// if (null == gson) {
  112 +// gson = new Gson();
  113 +// }
  114 +// return gson;
  115 +// }
  116 +//
  117 +// /**
  118 +// * 发送广播
  119 +// *
  120 +// * @param action
  121 +// * @param device
  122 +// */
  123 +// private static void sendJsonDevice(String action, Device device) {
  124 +// if (null == action || action.isEmpty() || null == device) return;
  125 +// Context context = SwitchApp.getAppContext();
  126 +// if (null == context) return;
  127 +// Intent intent = getIntent();
  128 +// intent.setAction(action);
  129 +// intent.putExtra(JSON_DEVICE, device);
  130 +// LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
  131 +// }
  132 +//
  133 +// public static void fistListener(String jsonDevice) {
  134 +// if (null == jsonDevice || jsonDevice.isEmpty()) return;
  135 +// GlobalVar.getLogUtils().i("fistListener jsonDevice =" + jsonDevice);
  136 +// Gson gson = getGson();
  137 +// Device device = gson.fromJson(jsonDevice, Device.class);
  138 +// char cmdTpye = (char) device.getCmdType();
  139 +//
  140 +// if (FistProtocol.CommondType.CMD_TYPE_SEARCH_RESPONSE == cmdTpye) {//查询响应
  141 +// removeId(device);
  142 +// online(device, jsonDevice);
  143 +// sendJsonDevice(SEARCH_RESPONSE, device);
  144 +//
  145 +// } else if (FistProtocol.CommondType.CMD_TYPE_CONFIG_DONE == cmdTpye) {//配置完成处理
  146 +// configDone(device);
  147 +//
  148 +// } else if (FistProtocol.CommondType.CMD_TYPE_ONLINE == cmdTpye) {//上线通知
  149 +// removeId(device);
  150 +// online(device, jsonDevice);
  151 +// configDone(device);
  152 +//
  153 +// } else if (FistProtocol.CommondType.CMD_TYPE_QUERY_RESPONSE == cmdTpye) {
  154 +// sendJsonDevice(ADD_DONE, device);
  155 +// ArrayList<SubDevice> subDevices = device.getSubDevices();
  156 +// if (!subDevices.isEmpty()) {//查询结果不为空
  157 +// Realm realm = Realm.getDefaultInstance();
  158 +// long hostNum = device.getLongDeviceNum();
  159 +// RealmResults<Hub> hubs = realm.where(Hub.class)
  160 +// .equalTo(Hub.Companion.getDEVICE_NUM(), hostNum)
  161 +// .notEqualTo(Transform.Companion.getSYNC_CODE(), SyncState.DELETE.ordinal()).findAll();
  162 +// if (!hubs.isEmpty()) {
  163 +// int subSize = subDevices.size();
  164 +// for (int i = 0; i < subSize; i++) {
  165 +// SubDevice subDevice = subDevices.get(i);
  166 +// long subDeviceNum = subDevice.getSubDeviceNum();
  167 +// RealmResults<SwSubDev> swSubDevs = realm.where(SwSubDev.class)
  168 +// .equalTo(SwSubDev.Companion.getHOST_NUM(), hostNum)
  169 +// .equalTo(SwSubDev.Companion.getPARENT_NUM(), subDeviceNum)
  170 +// .findAll();
  171 +// if (!swSubDevs.isEmpty()) {//数据库已存储
  172 +// int lightingSize = swSubDevs.size();
  173 +// byte lingtingState = (byte) subDevice.getSubDeviceStatus();
  174 +// for (int j = 0; j < lightingSize; j++) {//更新开关状态
  175 +// SwSubDev lighting = swSubDevs.get(j);
  176 +// realm.beginTransaction();
  177 +// byte currentState = (byte) (lingtingState >> lighting.getDeviceNum() & 1);
  178 +// if (0 == currentState) {
  179 +// lighting.setOn(false);
  180 +// } else {
  181 +// lighting.setOn(true);
  182 +// }
  183 +// realm.commitTransaction();
  184 +// }
  185 +// }
  186 +// }
  187 +// }
  188 +//
  189 +// realm.close();
  190 +// }
  191 +// sendJsonDevice(LightingGroupAdapter.REFRESH, device);
  192 +//
  193 +// } else if (FistProtocol.CommondType.CMD_TYPE_ADD_RESPONSE == cmdTpye) {
  194 +// sendJsonDevice(START_ADD, device);
  195 +//
  196 +// } else if (FistProtocol.CommondType.CMD_TYPE_ADD_OVER == cmdTpye) {//子设备结束入网
  197 +//// if (0 == device.getIntDeviceIp()) return;
  198 +//// FistJni.stopAddSubDevice(jsonDevice);
  199 +//
  200 +// } else if (FistProtocol.CommondType.CMD_TYPE_CONTROL == cmdTpye) {//控制超时
  201 +// sendJsonDevice(CONTROL_REQUEST_TIMEOUT, device);
  202 +//
  203 +// } else if (FistProtocol.CommondType.CMD_TYPE_CONTROL_RESPONSE == cmdTpye) {//控制应答
  204 +// sendJsonDevice(CONTROL_RESPONSE, device);
  205 +//
  206 +// } else if (FistProtocol.CommondType.CMD_TYPE_CONTROL_DONE_RESPONSE == cmdTpye) {//单个控制结果
  207 +// sendJsonDevice(CONTROL_DONE, device);
  208 +//
  209 +// } else if (FistProtocol.CommondType.CMD_TYPE_CONTROL_OVER == cmdTpye) {
  210 +// sendJsonDevice(CONTROL_OVER, device);
  211 +// GlobalVar.getLogUtils().i("CONTROL_OVER");
  212 +// } else if (FistProtocol.CommondType.CMD_NET_LINK_STATUS_RESPONSE == cmdTpye) {
  213 +// sendJsonDevice(LINK_STATUS_RESPONSE, device);
  214 +//
  215 +// } else if (FistProtocol.CommondType.CMD_LINK_RESPONSE == cmdTpye) {
  216 +// sendJsonDevice(LINK_RESPONSE, device);
  217 +//
  218 +// } else if (FistProtocol.CommondType.CMD_MODIFY_CTRL_PWD_RESPONSE == cmdTpye) {
  219 +// GlobalVar.getLogUtils().i("FistProtocol.CommondType.CMD_MODIFY_CTRL_PWD_RESPONSE");
  220 +// sendJsonDevice(CTRL_PWD_RESPONSE, device);
  221 +//
  222 +// } else if (RemoteProtocol.MsgType.SET_SUCCESS == cmdTpye) {//密码设置成功
  223 +// GlobalVar.getLogUtils().i("SET_SUCCESS jsonDevice =" + jsonDevice);
  224 +// long deviceNum = device.getLongDeviceNum();
  225 +// Realm realm = Realm.getDefaultInstance();
  226 +// Hub host = realm.where(Hub.class).equalTo(Hub.Companion.getDEVICE_NUM(), deviceNum).findFirst();
  227 +// if (null != host) {
  228 +// realm.beginTransaction();
  229 +// host.setWanState(WanState.ON);
  230 +// realm.commitTransaction();
  231 +// }
  232 +// realm.close();
  233 +// } else if (RemoteProtocol.MsgType.SET_FAILED == cmdTpye) {//密码设置失败
  234 +// GlobalVar.getLogUtils().i("SET_FAILED jsonDevice =" + jsonDevice);
  235 +//
  236 +// } else if (RemoteProtocol.MsgType.QUERY_SUCCESS == cmdTpye) {//查询对端成功
  237 +// final long deviceNum = device.getLongDeviceNum();
  238 +// ArrayList<Long> ids = HeService.getLocIds();
  239 +// for (long id : ids) {
  240 +// if (deviceNum == id) {
  241 +// saveRemote(device);
  242 +// device.setWan(true);
  243 +// jsonDevice = gson.toJson(device);
  244 +// querySubDevice(jsonDevice);
  245 +// break;
  246 +// }
  247 +// }
  248 +// removeId(device);
  249 +//
  250 +// } else if (RemoteProtocol.MsgType.QUERY_FAILED == cmdTpye) {//查询对端失败
  251 +//
  252 +// }
  253 +// }
  254 +//
  255 +// /**
  256 +// * 开始
  257 +// */
  258 +// public static native void start();
  259 +//
  260 +// /**
  261 +// * 启动SKD
  262 +// */
  263 +// public void startSdk() {
  264 +// new AsyncTask<Void, Void, Void>() {
  265 +// @Override
  266 +// protected Void doInBackground(Void... params) {
  267 +// start();
  268 +// return null;
  269 +// }
  270 +// }.execute();
  271 +// }
  272 +//
  273 +// /**
  274 +// * 移除查询到的主机ID
  275 +// *
  276 +// * @param device
  277 +// */
  278 +// private static void removeId(Device device) {
  279 +// ArrayList<Long> ids = HeService.getLocIds();
  280 +// final long deviceNum = device.getLongDeviceNum();
  281 +// for (long id : ids) {
  282 +// if (deviceNum == id) {
  283 +// ids.remove(id);
  284 +// break;
  285 +// }
  286 +// }
  287 +// }
  288 +//
  289 +// /**
  290 +// * 搜索设备
  291 +// */
  292 +// public static native void searchDevice();
  293 +//
  294 +// /**
  295 +// * 查询子设备
  296 +// */
  297 +// public static native void querySubDevice(String jsonDevice);
  298 +//
  299 +// public void query(final String jsonDevice) {
  300 +// if (!HeService.isConn()) {
  301 +// return;
  302 +// }
  303 +// new AsyncTask<Void, Void, Void>() {
  304 +// @Override
  305 +// protected Void doInBackground(Void... params) {
  306 +// querySubDevice(jsonDevice);
  307 +// return null;
  308 +// }
  309 +// }.execute();
  310 +// }
  311 +//
  312 +// /**
  313 +// * 开始添加子设备
  314 +// */
  315 +// public static native void startAddSubDevice(String jsonDevice);
  316 +//
  317 +// public void startAdd(final String jsonDevice) {
  318 +// if (!(WifiUtils.isWifiConnected())) {
  319 +// ToastUtils.toastShort("网络未连接");
  320 +// return;
  321 +// }
  322 +// new AsyncTask<Void, Void, Void>() {
  323 +// @Override
  324 +// protected Void doInBackground(Void... params) {
  325 +// startAddSubDevice(jsonDevice);
  326 +// return null;
  327 +// }
  328 +// }.execute();
  329 +// }
  330 +//
  331 +// /**
  332 +// * 停止添加子设备
  333 +// */
  334 +// public static native void stopAddSubDevice(String jsonDevice);
  335 +//
  336 +// public void stopAdd(final String jsonDevice) {
  337 +// if (!(WifiUtils.isWifiConnected())) {
  338 +// ToastUtils.toastShort("网络未连接");
  339 +// return;
  340 +// }
  341 +// new AsyncTask<Void, Void, Void>() {
  342 +// @Override
  343 +// protected Void doInBackground(Void... params) {
  344 +// stopAddSubDevice(jsonDevice);
  345 +// return null;
  346 +// }
  347 +// }.execute();
  348 +// }
  349 +//
  350 +// /**
  351 +// * 删除子设备
  352 +// */
  353 +// public static native void deleteSubDevices(String jsonDevice);
  354 +//
  355 +// public void delSub(final String jsonDevice) {
  356 +// if (!HeService.isConn()) {
  357 +// ToastUtils.toastShort("网络未连接");
  358 +// return;
  359 +// }
  360 +// new AsyncTask<Void, Void, Void>() {
  361 +// @Override
  362 +// protected Void doInBackground(Void... params) {
  363 +// deleteSubDevices(jsonDevice);
  364 +// return null;
  365 +// }
  366 +// }.execute();
  367 +// }
  368 +//
  369 +// /**
  370 +// * 查询软件版本
  371 +// */
  372 +// public static native void querySoftwareVer(String jsonDevice);
  373 +//
  374 +// /**
  375 +// * 查询工作模式
  376 +// */
  377 +// public static native void queryModel(String jsonDevice);
  378 +//
  379 +// public void sceneC(final String jsonDevice) {
  380 +// if (!(WifiUtils.isWifiConnected())) {
  381 +// ToastUtils.toastShort("网络未连接");
  382 +// return;
  383 +// }
  384 +// new AsyncTask<Void, Void, Void>() {
  385 +// @Override
  386 +// protected Void doInBackground(Void... params) {
  387 +// GlobalVar.getLogUtils().i("sceneC jsonDevice =" + jsonDevice);
  388 +// sceneControl(jsonDevice);
  389 +// return null;
  390 +// }
  391 +// }.execute();
  392 +// }
  393 +//
  394 +// /**
  395 +// * 情景控制
  396 +// */
  397 +// public static native void sceneControl(String jsonDevice);
  398 +//
  399 +// public void normalC(final String jsonDevice) {
  400 +// if (!(WifiUtils.isWifiConnected())) {
  401 +// ToastUtils.toastShort("网络未连接");
  402 +// return;
  403 +// }
  404 +// new AsyncTask<Void, Void, Void>() {
  405 +// @Override
  406 +// protected Void doInBackground(Void... params) {
  407 +// GlobalVar.getLogUtils().i("normalC jsonDevice =" + jsonDevice);
  408 +// normalControl(jsonDevice);
  409 +// return null;
  410 +// }
  411 +// }.execute();
  412 +// }
  413 +//
  414 +// /**
  415 +// * 普通控制
  416 +// */
  417 +// private static native void normalControl(String jsonDevice);
  418 +//
  419 +//
  420 +// /**
  421 +// * 设置远程密码
  422 +// *
  423 +// * @param jsonDevice
  424 +// * @param pwd
  425 +// */
  426 +// private static native void setRemotePwd(String jsonDevice, String pwd);
  427 +//
  428 +// public void setPwd(final String jsonDevice, final String pwd) {
  429 +// if (!(WifiUtils.isWifiConnected())) {
  430 +// ToastUtils.toastShort("网络未连接");
  431 +// return;
  432 +// }
  433 +// new AsyncTask<Void, Void, Void>() {
  434 +// @Override
  435 +// protected Void doInBackground(Void... params) {
  436 +// setRemotePwd(jsonDevice, pwd);
  437 +// return null;
  438 +// }
  439 +// }.execute();
  440 +// }
  441 +//
  442 +// private static native void getRemoteCtrlIp(String jsonDevice, String random, String pwd);
  443 +//
  444 +// public void searchRecordDevice() {
  445 +// final ArrayList<Long> ids = HeService.getLocIds();
  446 +// if (ids.isEmpty()) return;
  447 +// new AsyncTask<Void, Void, Void>() {
  448 +//
  449 +// @Override
  450 +// protected Void doInBackground(Void... params) {
  451 +// searchDevice();
  452 +// Realm realm = Realm.getDefaultInstance();
  453 +// for (long id : ids) {
  454 +// Hub host = realm.where(Hub.class).equalTo(Hub.Companion.getDEVICE_NUM(), id).findFirst();
  455 +// if (null != host) {
  456 +// if (WanState.ON == host.getWanState()) {
  457 +// realm.beginTransaction();
  458 +// host.setAlreadyHaveRemoteIP(false);
  459 +// realm.commitTransaction();
  460 +// Device device = Device.getInstance();
  461 +// device.setDeviceNum(ByteUtils.longToMacArray(id));
  462 +// device.setDeviceKey(host.getWanPwd().getBytes());
  463 +// Gson gson = getGson();
  464 +// String jsonDevice = gson.toJson(device);
  465 +// GlobalVar.getLogUtils().i("searchRecordDevice = " + jsonDevice);
  466 +// String strRandom = RandomUtil.getRandomStr();
  467 +// String strPwd = host.getWanPwd();
  468 +// getRemoteCtrlIp(jsonDevice, strRandom, strPwd);
  469 +// }
  470 +// }
  471 +// }
  472 +// realm.close();
  473 +// return null;
  474 +// }
  475 +// }.execute();
  476 +// }
  477 +//
  478 +// public void searchLocalDevice() {
  479 +// new AsyncTask<Void, Void, Void>() {
  480 +//
  481 +// @Override
  482 +// protected Void doInBackground(Void... params) {
  483 +// searchDevice();
  484 +// return null;
  485 +// }
  486 +// }.execute();
  487 +// }
  488 +//
  489 +// /**
  490 +// * 链接路由
  491 +// *
  492 +// * @param jsonDevice
  493 +// * @param ssid
  494 +// * @param pwd
  495 +// */
  496 +// public static native void linkRouter(String jsonDevice, String ssid, String pwd);
  497 +//
  498 +// public void getlinkRouter(final String jsonDevice, final String ssid, final String pwd) {
  499 +// if (!(WifiUtils.isWifiConnected())) {
  500 +// ToastUtils.toastShort("网络未连接");
  501 +// return;
  502 +// }
  503 +// new AsyncTask<Void, Void, Void>() {
  504 +// @Override
  505 +// protected Void doInBackground(Void... params) {
  506 +// GlobalVar.getLogUtils().i("getlinkRouter jsonDevice =" + jsonDevice);
  507 +// linkRouter(jsonDevice, ssid, pwd);
  508 +// return null;
  509 +// }
  510 +// }.execute();
  511 +// }
  512 +//
  513 +// /**
  514 +// * 修改密码或SSID
  515 +// *
  516 +// * @param jsonDevice
  517 +// * @param ssid
  518 +// * @param pwd
  519 +// */
  520 +// public static native void modifyAp(String jsonDevice, String ssid, String pwd);
  521 +//
  522 +// /**
  523 +// * 链接状态
  524 +// *
  525 +// * @param jsonDevice
  526 +// */
  527 +// public static native void linkStatus(String jsonDevice);
  528 +//
  529 +// public void getLinkStatus(final String jsonDevice) {
  530 +// if (!(WifiUtils.isWifiConnected())) {
  531 +// ToastUtils.toastShort("网络未连接");
  532 +// return;
  533 +// }
  534 +// new AsyncTask<Void, Void, Void>() {
  535 +// @Override
  536 +// protected Void doInBackground(Void... params) {
  537 +// GlobalVar.getLogUtils().i("getLinkStatus jsonDevice =" + jsonDevice);
  538 +// linkStatus(jsonDevice);
  539 +// return null;
  540 +// }
  541 +// }.execute();
  542 +// }
  543 +//
  544 +//
  545 +// /**
  546 +// * 修改控制密码
  547 +// *
  548 +// * @param jsonDevice
  549 +// * @param pwd
  550 +// */
  551 +// public static native void modifyCtrlPWd(String jsonDevice, String pwd);
  552 +//
  553 +// public void modifyPwd(final String jsonDevice, final String pwd) {
  554 +// if (!(WifiUtils.isWifiConnected())) {
  555 +// ToastUtils.toastShort("网络未连接");
  556 +// return;
  557 +// }
  558 +// new AsyncTask<Void, Void, Void>() {
  559 +// @Override
  560 +// protected Void doInBackground(Void... params) {
  561 +// GlobalVar.getLogUtils().i("modifyPwd jsonDevice =" + jsonDevice);
  562 +// modifyCtrlPWd(jsonDevice, pwd);
  563 +// return null;
  564 +// }
  565 +// }.execute();
  566 +// }
  567 +//}
\ No newline at end of file
... ...
  1 +package com.adroplat.fist_switch.jni;
  2 +
  3 +/**
  4 + * Created by yikuyiliao on 16-3-8.
  5 + */
  6 +public class SubDevice implements Cloneable {
  7 +
  8 + private static SubDevice instance;
  9 +
  10 + private SubDevice() {
  11 + }
  12 +
  13 + public static SubDevice getInstance() {
  14 + if (null == instance) {
  15 + instance = new SubDevice();
  16 + }
  17 + SubDevice subDevice = null;
  18 + try {
  19 + subDevice = (SubDevice) instance.clone();
  20 + } catch (CloneNotSupportedException e) {
  21 + e.printStackTrace();
  22 + }
  23 + if (null == subDevice) {
  24 + subDevice = new SubDevice();
  25 + }
  26 + return subDevice;
  27 + }
  28 +
  29 + private long SubDeviceNum;
  30 + private int SubDeviceType;
  31 + private int SubDeviceStatus;
  32 + private int SubDeviceOnlineStatus;
  33 + private int SubDeviceBit;
  34 +
  35 +
  36 + public long getSubDeviceNum() {
  37 +
  38 + return SubDeviceNum;
  39 + }
  40 +
  41 + public void setSubDeviceNum(long subDeviceNum) {
  42 +
  43 + SubDeviceNum = subDeviceNum;
  44 + }
  45 +
  46 + public int getSubDeviceType() {
  47 + return SubDeviceType;
  48 + }
  49 +
  50 + public void setSubDeviceType(int subDeviceType) {
  51 +
  52 + SubDeviceType = subDeviceType;
  53 + }
  54 +
  55 + public int getSubDeviceStatus() {
  56 + return SubDeviceStatus;
  57 + }
  58 +
  59 + public void setSubDeviceStatus(int subDeviceStatus) {
  60 +
  61 + SubDeviceStatus = subDeviceStatus;
  62 + }
  63 +
  64 + public int getSubDeviceOnlineStatus() {
  65 + return SubDeviceOnlineStatus;
  66 + }
  67 +
  68 + public void setSubDeviceOnlineStatus(int subDeviceOnlineStatus) {
  69 + SubDeviceOnlineStatus = subDeviceOnlineStatus;
  70 + }
  71 +
  72 + public int getSubDeviceBit() {
  73 + return SubDeviceBit;
  74 + }
  75 +
  76 + public void setSubDeviceBit(int subDeviceBit) {
  77 + SubDeviceBit = subDeviceBit;
  78 + }
  79 +}
... ...
  1 +package com.adroplat.fist_switch.utils;
  2 +
  3 +import java.nio.ByteBuffer;
  4 +import java.util.Arrays;
  5 +
  6 +//import com.adroplat.fist_switch.utils.protocol.remote.RemoteHead;
  7 +
  8 +/**
  9 + * byte转换工具类
  10 + * Created by WLJ on 2015/10/10.
  11 + */
  12 +public class ByteUtils {
  13 +
  14 +
  15 + /**
  16 + * byte数组转int
  17 + *
  18 + * @param byteArray
  19 + * @return
  20 + */
  21 + public static int byteArrayToInt(byte[] byteArray) {
  22 + int value = 0;
  23 + if (null == byteArray || 4 != byteArray.length) {
  24 +
  25 + } else {
  26 + int len = byteArray.length;
  27 + for (int i = 0; i < len; i++) {
  28 + value += (byteArray[i] & 0xff) << (8 * (3 - i));
  29 + }
  30 + }
  31 + return value;
  32 + }
  33 +
  34 + /**
  35 + * int转byte数组
  36 + *
  37 + * @param value
  38 + * @return
  39 + */
  40 + public static byte[] intToByteArrays(int value) {
  41 + byte[] byteArray = new byte[4];
  42 + int len = byteArray.length;
  43 + for (int i = 0; i < len; i++) {
  44 + byteArray[i] = (byte) ((value >> (8 * (3 - i))) & 0xff);
  45 + }
  46 + return byteArray;
  47 + }
  48 +
  49 + public static String intToIp(int ipInt) {
  50 + StringBuilder builder = new StringBuilder();
  51 + builder.delete(0, builder.length());
  52 + char c = '.';
  53 + return builder.append(((ipInt >> 24) & 0xff)).append(c)
  54 + .append((ipInt >> 16) & 0xff).append(c)
  55 + .append((ipInt >> 8) & 0xff).append(c)
  56 + .append((ipInt & 0xff))
  57 + .toString();
  58 + }
  59 +
  60 + public static byte[] intToLend(int value) {
  61 + byte[] byteArray = new byte[4];
  62 + int len = byteArray.length;
  63 + for (int i = 0; i < len; i++) {
  64 + byteArray[i] = (byte) ((value >> (8 * i)) & 0xff);
  65 + }
  66 + return byteArray;
  67 + }
  68 +
  69 + /**
  70 + * byte数组转long
  71 + *
  72 + * @param macArray
  73 + * @return
  74 + */
  75 + public static long macArrayToLong(byte[] macArray) {
  76 + long value = 0;
  77 + if (null == macArray || 8 < macArray.length) {
  78 +
  79 + } else {
  80 + int len = macArray.length;
  81 + for (int i = 0; i < len; i++) {
  82 + value |= (long) (macArray[i] & 0xff) << (8 * (len - i - 1));
  83 + }
  84 + }
  85 + return value;
  86 + }
  87 +
  88 + /**
  89 + * long转byte数组
  90 + *
  91 + * @param value
  92 + * @return
  93 + */
  94 + public static byte[] longToMacArray(long value) {
  95 + byte[] lAray = longToBytes(value);
  96 + return Arrays.copyOfRange(lAray, 2, lAray.length);
  97 + }
  98 +
  99 +
  100 + /**
  101 + * long转16进制String
  102 + *
  103 + * @param value
  104 + * @return
  105 + */
  106 + public static String longToHexString(long value) {
  107 + byte[] array = longToMacArray(value);
  108 + int offset = 0;
  109 + int len = array.length;
  110 + for (int i = 0; i < len; i++) {
  111 + if (0 != array[i]) {
  112 + offset = i;
  113 + break;
  114 + }
  115 + }
  116 + array = Arrays.copyOfRange(array, offset, len);
  117 + return byteArayToHexString(array).trim();
  118 + }
  119 +
  120 + /**
  121 + * 16进制String转long
  122 + *
  123 + * @param hex
  124 + * @return
  125 + */
  126 + public static long hexStringToLong(String hex) {
  127 + byte[] array = hexStringToByteAray(hex);
  128 + return macArrayToLong(array);
  129 + }
  130 +
  131 + /**
  132 + * byte数组转16进制String
  133 + *
  134 + * @param array
  135 + * @return
  136 + */
  137 + public static String byteArayToHexString(byte[] array) {
  138 + String strHex = null;
  139 + if (null == array || 0 == array.length) {
  140 + } else {
  141 + StringBuilder builder = new StringBuilder();
  142 + for (byte b : array) {
  143 + String str = Integer.toHexString(b & 0xff);
  144 + if (1 == str.length()) {
  145 + str = '0' + str;
  146 + }
  147 + builder.append(str);
  148 + }
  149 + strHex = builder.toString();
  150 + }
  151 + return strHex;
  152 + }
  153 +
  154 + /**
  155 + * byte数组转16进制MAC
  156 + *
  157 + * @param array
  158 + * @return
  159 + */
  160 + public static String byteArayToMacStr(byte[] array) {
  161 + String strHex = null;
  162 + if (null == array || 0 == array.length) {
  163 + } else {
  164 + StringBuilder builder = new StringBuilder();
  165 + for (byte b : array) {
  166 + String str = Integer.toHexString(b & 0xff);
  167 + if (1 == str.length()) {
  168 + str = '0' + str;
  169 + }
  170 + builder.append(str + ":");
  171 + }
  172 + strHex = builder.deleteCharAt(builder.length() - 1).toString().toUpperCase();
  173 + }
  174 + return strHex;
  175 + }
  176 +
  177 + /**
  178 + * 16进制String转byte数组
  179 + *
  180 + * @param hex
  181 + * @return
  182 + */
  183 + public static byte[] hexStringToByteAray(String hex) {
  184 + if (hex == null || hex.equals("")) {
  185 + return null;
  186 + }
  187 + hex = hex.toUpperCase();
  188 + char[] hexChars = hex.toCharArray();
  189 + int len = hexChars.length / 2;
  190 + byte[] d = new byte[len];
  191 + for (int i = 0; i < len; i++) {
  192 + int pos = i * 2;
  193 + d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
  194 + }
  195 + return d;
  196 + }
  197 +
  198 + /**
  199 + * Convert char to byte
  200 + *
  201 + * @param c char
  202 + * @return byte
  203 + */
  204 + private static byte charToByte(char c) {
  205 + return (byte) "0123456789ABCDEF".indexOf(c);
  206 + }
  207 +
  208 + /**
  209 + * int转char
  210 + *
  211 + * @param value
  212 + * @return
  213 + */
  214 + public static char intToChar(int value) {
  215 + byte[] byteArray = intToByteArrays(value);
  216 +// return (char) ((byteArray[2] & 0xff) << 8 | byteArray[3] & 0xff);
  217 + return bytesToChar(byteArray);
  218 + }
  219 +
  220 +
  221 + /**
  222 + * char转byte[]
  223 + */
  224 + public static byte[] charToBytes(char c) {
  225 + byte[] bytes = new byte[2];
  226 +// final char DIGIT = RemoteHead.DIGIT;
  227 +// bytes[0] = (byte) ((c >> 8) & DIGIT);
  228 +// bytes[1] = (byte) (c & DIGIT);
  229 + return bytes;
  230 + }
  231 +
  232 + /**
  233 + * byte[]转char
  234 + */
  235 + public static char bytesToChar(byte[] bytes) {
  236 + char c = 0;
  237 + if (null != bytes) {
  238 + int size = bytes.length;
  239 + if (size >= 2) {
  240 + c = (char) ((bytes[size - 2] & 0xff) << 8 | bytes[size - 1] & 0xff);
  241 + } else if (1 == size) {
  242 + c = (char) (bytes[0]);
  243 + }
  244 + }
  245 + return c;
  246 + }
  247 +
  248 + /**
  249 + * 转化CRON需要的星期
  250 + *
  251 + * @param weekCode
  252 + * @return
  253 + */
  254 + public static String byteToDayWeek(byte weekCode, String weekDay[]) {
  255 + StringBuilder sb = new StringBuilder();
  256 + if (null != weekDay) {
  257 + final int size = weekDay.length;
  258 + for (int i = 0; i < size; i++) {
  259 + if (0 != (weekCode >> i) % 2) {
  260 + sb.append(weekDay[i]).append(",");
  261 + }
  262 + }
  263 + int len = sb.length();
  264 + if (len > 0) {
  265 + sb.deleteCharAt(len - 1);
  266 + }
  267 + }
  268 + return sb.toString();
  269 + }
  270 +
  271 + public static byte[] longToBytes(long value) {
  272 + ByteBuffer buffer = ByteBuffer.allocate(8);
  273 + buffer.putLong(0, value);
  274 + return buffer.array();
  275 + }
  276 +
  277 + public static long bytesToLong(byte[] value) {
  278 + long result = 0;
  279 + if (null != value) {
  280 + for (int i = 0; i < value.length; i++) {
  281 + value[i] &= 0xff;
  282 + }
  283 + ByteBuffer buffer = ByteBuffer.allocate(8);
  284 + buffer.put(value, 0, value.length);
  285 + buffer.flip();//need flip
  286 + result = buffer.getLong();
  287 + }
  288 + return result;
  289 + }
  290 +
  291 +}
... ...
  1 +package com.adroplat.fist_switch.utils;
  2 +
  3 +import android.util.Log;
  4 +
  5 +/**
  6 + * Created by wugian on 2017/3/8.
  7 + */
  8 +
  9 +public class ToastUtils {
  10 + public static void toastShort(String s){
  11 + Log.d("ToastUtils", s);
  12 + }
  13 +}
... ...
  1 +package com.adroplat.fist_switch.utils;
  2 +
  3 +/**
  4 + * Created by wugian on 2017/3/8.
  5 + */
  6 +
  7 +public class WifiUtils {
  8 + public static boolean isWifiConnected() {
  9 + return true;
  10 + }
  11 +}
... ...
  1 +package com.qnbar.switchcontrol;
  2 +
  3 +import android.os.Bundle;
  4 +import android.os.Handler;
  5 +import android.os.Message;
  6 +import android.support.v7.app.AppCompatActivity;
  7 +import android.util.Log;
  8 +import android.view.View;
  9 +
  10 +import com.adroplat.fist_switch.jni.FistJni;
  11 +
  12 +public class MainActivity extends AppCompatActivity {
  13 +
  14 + @Override
  15 + protected void onCreate(Bundle savedInstanceState) {
  16 + super.onCreate(savedInstanceState);
  17 + setContentView(R.layout.activity_main);
  18 +
  19 + Handler handler = new Handler() {
  20 + @Override
  21 + public void dispatchMessage(Message msg) {
  22 + super.dispatchMessage(msg);
  23 + switch (msg.what) {
  24 + case UdpPostSender.MSG_What:
  25 + Log.d("lovely_switch", "msg what:" + msg.toString());
  26 + break;
  27 + default:
  28 + Log.d("lovely_switch", msg.toString());
  29 + }
  30 + }
  31 + };
  32 +// new UdpPostSender("", 9000, 9000, handler).start();
  33 + new Thread(){
  34 + @Override
  35 + public void run() {
  36 + super.run();
  37 + new UdpHelper().StartListen();
  38 + }
  39 + }.start();
  40 +// new UdpHelper().run();
  41 +// new UdpHelper().run();
  42 + }
  43 +
  44 + public void startSdk(View view) {
  45 + FistJni fistJni = FistJni.getInstance();
  46 + fistJni.startSdk();
  47 + }
  48 +
  49 + public void searchLocalDevice(View view){
  50 + FistJni fistJni = FistJni.getInstance();
  51 + fistJni.searchLocalDevice();
  52 + }
  53 +}
... ...
  1 +package com.qnbar.switchcontrol;
  2 +
  3 +import android.util.Log;
  4 +
  5 +import java.io.IOException;
  6 +import java.net.DatagramPacket;
  7 +import java.net.DatagramSocket;
  8 +import java.net.InetAddress;
  9 +import java.net.SocketException;
  10 +/**
  11 + * UdpHelper帮助类
  12 + *
  13 + */
  14 +public class UdpHelper{
  15 + public Boolean IsThreadDisable = false;//指示监听线程是否终止
  16 + InetAddress mInetAddress;
  17 +
  18 + public void StartListen() {
  19 + // UDP服务器监听的端口
  20 + Integer port = 9000;
  21 + // 接收的字节大小,客户端发送的数据不能超过这个大小
  22 + byte[] message = new byte[4096];
  23 + try {
  24 + // 建立Socket连接
  25 + DatagramSocket datagramSocket = new DatagramSocket(port);
  26 + datagramSocket.setBroadcast(true);
  27 + try {
  28 + while (!IsThreadDisable) {
  29 + // 准备接收数据
  30 + Log.d("UDP_Demo", "准备接受");
  31 +
  32 + DatagramPacket datagramPacket = new DatagramPacket(message,
  33 + message.length);
  34 + datagramSocket.receive(datagramPacket);
  35 + String strMsg=new String(datagramPacket.getData()).trim();
  36 + Log.d("UDP_Demo", datagramPacket.getAddress()
  37 + .getHostAddress().toString()
  38 + + ":" +strMsg );
  39 + }
  40 + } catch (IOException e) {//IOException
  41 + e.printStackTrace();
  42 + }
  43 + } catch (SocketException e) {
  44 + e.printStackTrace();
  45 + }
  46 + }
  47 + /* public static void send(String message) {
  48 + message = (message == null ? “Hello IdeasAndroid!” : message);
  49 + int server_port = 8904;
  50 + Log.d(“UDP Demo”, “UDP发送数据:”+message);
  51 + DatagramSocket s = null;
  52 + try {
  53 + s = new DatagramSocket();
  54 + } catch (SocketException e) {
  55 + e.printStackTrace();
  56 + }
  57 + InetAddress local = null;
  58 + try {
  59 + local = InetAddress.getByName(“255.255.255.255”);
  60 + } catch (UnknownHostException e) {
  61 + e.printStackTrace();
  62 + }
  63 + int msg_length = message.length();
  64 + byte[] messageByte = message.getBytes();
  65 + DatagramPacket p = new DatagramPacket(messageByte, msg_length, local,
  66 + server_port);
  67 + try {
  68 + s.send(p);
  69 + s.close();
  70 +
  71 + } catch (IOException e) {
  72 + e.printStackTrace();
  73 + }
  74 + }*/
  75 +}
\ No newline at end of file
... ...
  1 +package com.qnbar.switchcontrol;
  2 +
  3 +import android.os.Handler;
  4 +import android.os.Message;
  5 +import android.text.TextUtils;
  6 +import android.util.Log;
  7 +
  8 +import java.net.DatagramPacket;
  9 +import java.net.DatagramSocket;
  10 +import java.net.InetAddress;
  11 +import java.net.InetSocketAddress;
  12 +import java.net.UnknownHostException;
  13 +
  14 +/**
  15 + * UDP通信接口类,采用单例模式,用ip和通信端口初始化
  16 + */
  17 +public class UdpPostSender extends Thread {
  18 +
  19 + public static final int MSG_What = 0x100;
  20 + protected static final int MSG_PARSE_DATA = 0x100;
  21 + private static final String TAG = "UdpPostSender";
  22 +
  23 + private boolean isThreadDisable = false;// 指示监听线程是否终止
  24 + private Handler mHandler;
  25 + private DatagramSocket udpSocket = null;
  26 + private DatagramPacket dataPacket = null;
  27 + private InetAddress broadcastAddr = null;
  28 + private int defaultPort;
  29 + private int remotePort;
  30 + private String ip;
  31 + private String remoteIp;
  32 +
  33 + public UdpPostSender(String iP, int defaultPort, int remotePort,
  34 + Handler handler) {
  35 + this.ip = iP;
  36 + this.defaultPort = defaultPort;
  37 + this.remotePort = remotePort;
  38 + this.mHandler = handler;
  39 +
  40 + initData();
  41 +
  42 + if (handler != null) {// 不需要接收恢复信息的udp
  43 + this.start();
  44 + }
  45 + }
  46 +
  47 + public String getRemoteIp() {
  48 + return remoteIp;
  49 + }
  50 +
  51 + @Override
  52 + public void run() {
  53 + startListen();
  54 + }
  55 +
  56 + private void initData() {
  57 + try {
  58 + if (udpSocket == null) {
  59 + udpSocket = new DatagramSocket(null);
  60 + udpSocket.setReuseAddress(true);
  61 + // udpSocket.setSoTimeout(5000);
  62 + udpSocket.bind(new InetSocketAddress(defaultPort));
  63 + }
  64 + Log.i("info", "ip----" + ip);
  65 + broadcastAddr = InetAddress.getByName(ip);
  66 +
  67 + } catch (Exception e) {
  68 + Log.i("info", e.getMessage());
  69 + }
  70 + }
  71 +
  72 + public void sendUDPmsg(final String command) {
  73 + byte out[] = command.getBytes();
  74 + dataPacket = new DatagramPacket(out, out.length);
  75 + dataPacket.setPort(remotePort);
  76 + try {
  77 + dataPacket.setAddress(InetAddress.getByName(remoteIp));
  78 + } catch (UnknownHostException e) {
  79 + Log.i("info", e.getMessage());
  80 + e.printStackTrace();
  81 + }
  82 + Log.e(TAG, "cmd" + command + broadcastAddr.toString());
  83 + new Thread() {
  84 + public void run() {
  85 + try {
  86 + udpSocket.send(dataPacket);
  87 + } catch (Exception e) {
  88 + e.printStackTrace();
  89 + }
  90 + }
  91 + }.start();
  92 + }
  93 +
  94 + public void startListen() {
  95 + while (!isThreadDisable) {
  96 + try {
  97 + // 准备接收数据
  98 + byte[] buf = new byte[4 * 1024];
  99 + DatagramPacket packet = new DatagramPacket(buf, buf.length);
  100 + initData();
  101 +// udpSocket.setSoTimeout(2 * 1000);
  102 + udpSocket.receive(packet);
  103 + Log.d("lovely_switch", "success");
  104 +// String ip = packet.getAddress().getHostAddress();
  105 + String cmd = new String(packet.getData(), 0, packet.getLength());
  106 + if (!TextUtils.isEmpty(cmd)) {
  107 + remoteIp = packet.getAddress().toString().replace("/", "");
  108 + }
  109 + Message m = Message.obtain();
  110 + m.what = MSG_PARSE_DATA;
  111 + m.obj = cmd;
  112 + mHandler.sendMessage(m);
  113 + } catch (Exception e) {
  114 + e.printStackTrace();
  115 + }
  116 + }
  117 + }
  118 +
  119 + public void stopListen() {
  120 + isThreadDisable = true;
  121 + }
  122 +}
\ No newline at end of file
... ...
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3 + xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
  4 + android:layout_width="match_parent" android:layout_height="match_parent"
  5 + android:paddingBottom="@dimen/activity_vertical_margin"
  6 + android:paddingLeft="@dimen/activity_horizontal_margin"
  7 + android:paddingRight="@dimen/activity_horizontal_margin"
  8 + android:paddingTop="@dimen/activity_vertical_margin"
  9 + android:orientation="vertical"
  10 + tools:context="com.qnbar.switchcontrol.MainActivity">
  11 +
  12 + <TextView android:layout_width="wrap_content"
  13 + android:layout_height="wrap_content"
  14 + android:text="Hello World!" />
  15 +
  16 + <Button
  17 + android:layout_width="wrap_content"
  18 + android:layout_height="wrap_content"
  19 + android:onClick="startSdk"
  20 + android:text="startSdk"/>
  21 +
  22 + <Button
  23 + android:layout_width="wrap_content"
  24 + android:layout_height="wrap_content"
  25 + android:onClick="searchLocalDevice"
  26 + android:text="searchLocalDevice"/>
  27 +
  28 +</LinearLayout>
... ...
  1 +<resources>
  2 + <!-- Example customization of dimensions originally defined in res/values/dimens.xml
  3 + (such as screen margins) for screens with more than 820dp of available width. This
  4 + would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
  5 + <dimen name="activity_horizontal_margin">64dp</dimen>
  6 +</resources>
... ...
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<resources>
  3 + <color name="colorPrimary">#3F51B5</color>
  4 + <color name="colorPrimaryDark">#303F9F</color>
  5 + <color name="colorAccent">#FF4081</color>
  6 +</resources>
... ...
  1 +<resources>
  2 + <!-- Default screen margins, per the Android Design guidelines. -->
  3 + <dimen name="activity_horizontal_margin">16dp</dimen>
  4 + <dimen name="activity_vertical_margin">16dp</dimen>
  5 +</resources>
... ...
  1 +<resources>
  2 + <string name="app_name">SwitchControl</string>
  3 +</resources>
... ...
  1 +<resources>
  2 +
  3 + <!-- Base application theme. -->
  4 + <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  5 + <!-- Customize your theme here. -->
  6 + <item name="colorPrimary">@color/colorPrimary</item>
  7 + <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  8 + <item name="colorAccent">@color/colorAccent</item>
  9 + </style>
  10 +
  11 +</resources>
... ...
  1 +package com.qnbar.switchcontrol;
  2 +
  3 +import org.junit.Test;
  4 +
  5 +import static org.junit.Assert.*;
  6 +
  7 +/**
  8 + * Example local unit test, which will execute on the development machine (host).
  9 + *
  10 + * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
  11 + */
  12 +public class ExampleUnitTest {
  13 + @Test
  14 + public void addition_isCorrect() throws Exception {
  15 + assertEquals(4, 2 + 2);
  16 + }
  17 +}
\ No newline at end of file
... ...
Please register or login to post a comment