update(buffer, 0, buffer.length)
.
44 | *
45 | * @param buffer
46 | * the byte array to update the checksum with
47 | */
48 | public void update(byte[] buffer) {
49 | update(buffer, 0, buffer.length);
50 | }
51 |
52 | @Override
53 | public void update(int b) {
54 | update(new byte[] { (byte) b }, 0, 1);
55 | }
56 |
57 | @Override
58 | public long getValue() {
59 | return value & 0xff;
60 | }
61 |
62 | @Override
63 | public void reset() {
64 | value = init;
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/sdk/src/main/java/com/espressif/iot/esptouch/util/EspNetUtil.java:
--------------------------------------------------------------------------------
1 | package com.espressif.iot.esptouch.util;
2 |
3 | import java.net.InetAddress;
4 | import java.net.UnknownHostException;
5 |
6 | import android.content.Context;
7 | import android.net.wifi.WifiInfo;
8 | import android.net.wifi.WifiManager;
9 |
10 | public class EspNetUtil {
11 |
12 | /**
13 | * get the local ip address by Android System
14 | *
15 | * @param context
16 | * the context
17 | * @return the local ip addr allocated by Ap
18 | */
19 | public static InetAddress getLocalInetAddress(Context context) {
20 | WifiManager wm = (WifiManager) context
21 | .getSystemService(Context.WIFI_SERVICE);
22 | WifiInfo wifiInfo = wm.getConnectionInfo();
23 | int localAddrInt = wifiInfo.getIpAddress();
24 | String localAddrStr = __formatString(localAddrInt);
25 | InetAddress localInetAddr = null;
26 | try {
27 | localInetAddr = InetAddress.getByName(localAddrStr);
28 | } catch (UnknownHostException e) {
29 | e.printStackTrace();
30 | }
31 | return localInetAddr;
32 | }
33 |
34 | private static String __formatString(int value) {
35 | String strValue = "";
36 | byte[] ary = __intToByteArray(value);
37 | for (int i = ary.length - 1; i >= 0; i--) {
38 | strValue += (ary[i] & 0xFF);
39 | if (i > 0) {
40 | strValue += ".";
41 | }
42 | }
43 | return strValue;
44 | }
45 |
46 | private static byte[] __intToByteArray(int value) {
47 | byte[] b = new byte[4];
48 | for (int i = 0; i < 4; i++) {
49 | int offset = (b.length - 1 - i) * 8;
50 | b[i] = (byte) ((value >>> offset) & 0xFF);
51 | }
52 | return b;
53 | }
54 |
55 | /**
56 | * parse InetAddress
57 | *
58 | * @param inetAddrBytes
59 | * @return
60 | */
61 | public static InetAddress parseInetAddr(byte[] inetAddrBytes, int offset,
62 | int count) {
63 | InetAddress inetAddress = null;
64 | StringBuilder sb = new StringBuilder();
65 | for (int i = 0; i < count; i++) {
66 | sb.append(Integer.toString(inetAddrBytes[offset + i] & 0xff));
67 | if (i != count-1) {
68 | sb.append('.');
69 | }
70 | }
71 | try {
72 | inetAddress = InetAddress.getByName(sb.toString());
73 | } catch (UnknownHostException e) {
74 | e.printStackTrace();
75 | }
76 | return inetAddress;
77 | }
78 |
79 | /**
80 | * parse bssid
81 | *
82 | * @param bssid the bssid
83 | * @return byte converted from bssid
84 | */
85 | public static byte[] parseBssid2bytes(String bssid) {
86 | String bssidSplits[] = bssid.split(":");
87 | byte[] result = new byte[bssidSplits.length];
88 | for(int i = 0;i < bssidSplits.length; i++) {
89 | result[i] = (byte) Integer.parseInt(bssidSplits[i], 16);
90 | }
91 | return result;
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/sdk/src/main/java/com/pandocloud/android/agent/ActionListener.java:
--------------------------------------------------------------------------------
1 | package com.pandocloud.android.agent;
2 |
3 | import com.pandocloud.android.agent.Connection.ConnectionStatus;
4 | import org.eclipse.paho.client.mqttv3.IMqttActionListener;
5 | import org.eclipse.paho.client.mqttv3.IMqttToken;
6 |
7 | import android.content.Context;
8 | import android.widget.Toast;
9 |
10 | import com.pandocloud.android.R;
11 |
12 | class ActionListener implements IMqttActionListener {
13 |
14 | /**
15 | * Actions that can be performed Asynchronously and associated with a
16 | * {@link ActionListener} object
17 | *
18 | */
19 | enum Action {
20 | /** Connect Action **/
21 | CONNECT,
22 | /** Disconnect Action **/
23 | DISCONNECT,
24 | /** Subscribe Action **/
25 | SUBSCRIBE,
26 | /** Publish Action **/
27 | PUBLISH
28 | }
29 |
30 | /**
31 | * The {@link Action} that is associated with this instance of
32 | * ActionListener
33 | **/
34 | private Action action;
35 | /** The arguments passed to be used for formatting strings**/
36 | private String[] additionalArgs;
37 | /** Handle of the {@link Connection} this action was being executed on **/
38 | private String clientHandle;
39 | /** {@link Context} for performing various operations **/
40 | private Context context;
41 |
42 | private Connection c;
43 |
44 | /**
45 | * Creates a generic action listener for actions performed form any activity
46 | *
47 | * @param context
48 | * The application context
49 | * @param action
50 | * The action that is being performed
51 | * @param clientHandle
52 | * The handle for the client which the action is being performed
53 | * on
54 | * @param additionalArgs
55 | * Used for as arguments for string formating
56 | */
57 | public ActionListener(Context context, Connection connection, Action action,
58 | String clientHandle, String... additionalArgs) {
59 | this.context = context;
60 | this.c = connection;
61 | this.action = action;
62 | this.clientHandle = clientHandle;
63 | this.additionalArgs = additionalArgs;
64 | }
65 |
66 | /**
67 | * The action associated with this listener has been successful.
68 | *
69 | * @param asyncActionToken
70 | * This argument is not used
71 | */
72 | @Override
73 | public void onSuccess(IMqttToken asyncActionToken) {
74 | switch (action) {
75 | case CONNECT :
76 | connect();
77 | break;
78 | case DISCONNECT :
79 | disconnect();
80 | break;
81 | case SUBSCRIBE :
82 | subscribe();
83 | break;
84 | case PUBLISH :
85 | publish();
86 | break;
87 | }
88 |
89 | }
90 |
91 | /**
92 | * A publish action has been successfully completed, update connection
93 | * object associated with the client this action belongs to, then notify the
94 | * user of success
95 | */
96 | private void publish() {
97 |
98 | //Connection c = Connections.getInstance(context).getConnection(clientHandle);
99 | String actionTaken = context.getString(R.string.toast_pub_success,
100 | (Object[]) additionalArgs);
101 | c.addAction(actionTaken);
102 | Notify.toast(context, actionTaken, Toast.LENGTH_SHORT);
103 | }
104 |
105 | /**
106 | * A subscribe action has been successfully completed, update the connection
107 | * object associated with the client this action belongs to and then notify
108 | * the user of success
109 | */
110 | private void subscribe() {
111 | //Connection c = Connections.getInstance(context).getConnection(clientHandle);
112 | String actionTaken = context.getString(R.string.toast_sub_success,
113 | (Object[]) additionalArgs);
114 | c.addAction(actionTaken);
115 | Notify.toast(context, actionTaken, Toast.LENGTH_SHORT);
116 |
117 | }
118 |
119 | /**
120 | * A disconnection action has been successfully completed, update the
121 | * connection object associated with the client this action belongs to and
122 | * then notify the user of success.
123 | */
124 | private void disconnect() {
125 | //Connection c = Connections.getInstance(context).getConnection(clientHandle);
126 | c.changeConnectionStatus(ConnectionStatus.DISCONNECTED);
127 | String actionTaken = context.getString(R.string.toast_disconnected);
128 | c.addAction(actionTaken);
129 |
130 | }
131 |
132 | /**
133 | * A connection action has been successfully completed, update the
134 | * connection object associated with the client this action belongs to and
135 | * then notify the user of success.
136 | */
137 | private void connect() {
138 |
139 | //Connection c = Connections.getInstance(context).getConnection(clientHandle);
140 | c.changeConnectionStatus(Connection.ConnectionStatus.CONNECTED);
141 | c.addAction("Client Connected");
142 |
143 | }
144 |
145 | /**
146 | * The action associated with the object was a failure
147 | *
148 | * @param token
149 | * This argument is not used
150 | * @param exception
151 | * The exception which indicates why the action failed
152 | */
153 | @Override
154 | public void onFailure(IMqttToken token, Throwable exception) {
155 | switch (action) {
156 | case CONNECT :
157 | connect(exception);
158 | break;
159 | case DISCONNECT :
160 | disconnect(exception);
161 | break;
162 | case SUBSCRIBE :
163 | subscribe(exception);
164 | break;
165 | case PUBLISH :
166 | publish(exception);
167 | break;
168 | }
169 |
170 | }
171 |
172 | /**
173 | * A publish action was unsuccessful, notify user and update client history
174 | *
175 | * @param exception
176 | * This argument is not used
177 | */
178 | private void publish(Throwable exception) {
179 | //Connection c = Connections.getInstance(context).getConnection(clientHandle);
180 | String action = context.getString(R.string.toast_pub_failed,
181 | (Object[]) additionalArgs);
182 | c.addAction(action);
183 | Notify.toast(context, action, Toast.LENGTH_SHORT);
184 |
185 | }
186 |
187 | /**
188 | * A subscribe action was unsuccessful, notify user and update client history
189 | * @param exception This argument is not used
190 | */
191 | private void subscribe(Throwable exception) {
192 | //Connection c = Connections.getInstance(context).getConnection(clientHandle);
193 | String action = context.getString(R.string.toast_sub_failed,
194 | (Object[]) additionalArgs);
195 | c.addAction(action);
196 | Notify.toast(context, action, Toast.LENGTH_SHORT);
197 |
198 | }
199 |
200 | /**
201 | * A disconnect action was unsuccessful, notify user and update client history
202 | * @param exception This argument is not used
203 | */
204 | private void disconnect(Throwable exception) {
205 | //Connection c = Connections.getInstance(context).getConnection(clientHandle);
206 | c.changeConnectionStatus(ConnectionStatus.DISCONNECTED);
207 | c.addAction("Disconnect Failed - an error occured");
208 |
209 | }
210 |
211 | /**
212 | * A connect action was unsuccessful, notify the user and update client history
213 | * @param exception This argument is not used
214 | */
215 | private void connect(Throwable exception) {
216 | //Connection c = Connections.getInstance(context).getConnection(clientHandle);
217 | c.changeConnectionStatus(Connection.ConnectionStatus.ERROR);
218 | c.addAction("Client failed to connect");
219 |
220 | }
221 |
222 | }
--------------------------------------------------------------------------------
/sdk/src/main/java/com/pandocloud/android/agent/Constants.java:
--------------------------------------------------------------------------------
1 | package com.pandocloud.android.agent;
2 |
3 | import org.eclipse.paho.client.mqttv3.MqttMessage;
4 |
5 | /**
6 | * This Class provides constants used for returning results from an activity
7 | *
8 | */
9 | public class Constants {
10 |
11 | /** Application TAG for logs where class name is not used*/
12 | static final String TAG = "MQTT Android";
13 |
14 | /*Default values **/
15 |
16 | /** Default QOS value*/
17 | public static final int defaultQos = 0;
18 | /** Default timeout*/
19 | public static final int defaultTimeOut = 1000;
20 | /** Default keep alive value*/
21 | public static final int defaultKeepAlive = 10;
22 | /** Default SSL enabled flag*/
23 | public static final boolean defaultSsl = false;
24 | /** Default message retained flag */
25 | public static final boolean defaultRetained = false;
26 | /** Default last will message*/
27 | public static final MqttMessage defaultLastWill = null;
28 | /** Default port*/
29 | public static final int defaultPort = 1883;
30 |
31 | /** Connect Request Code */
32 | static final int connect = 0;
33 | /** Advanced Connect Request Code **/
34 | static final int advancedConnect = 1;
35 | /** Last will Request Code **/
36 | static final int lastWill = 2;
37 | /** Show History Request Code **/
38 | static final int showHistory = 3;
39 |
40 | /* Bundle Keys */
41 |
42 | /** Server Bundle Key **/
43 | public static final String server = "server";
44 | /** Port Bundle Key **/
45 | public static final String port = "port";
46 | /** ClientID Bundle Key **/
47 | public static final String clientId = "clientId";
48 | /** Topic Bundle Key **/
49 | public static final String topic = "topic";
50 | /** History Bundle Key **/
51 | static final String history = "history";
52 | /** Message Bundle Key **/
53 | public static final String message = "message";
54 | /** Retained Flag Bundle Key **/
55 | public static final String retained = "retained";
56 | /** QOS Value Bundle Key **/
57 | public static final String qos = "qos";
58 | /** User name Bundle Key **/
59 | public static final String username = "username";
60 | /** Password Bundle Key **/
61 | public static final String password = "password";
62 | /** Keep Alive value Bundle Key **/
63 | public static final String keepalive = "keepalive";
64 | /** Timeout Bundle Key **/
65 | public static final String timeout = "timeout";
66 | /** SSL Enabled Flag Bundle Key **/
67 | public static final String ssl = "ssl";
68 | /** SSL Key File Bundle Key **/
69 | public static final String ssl_key = "ssl_key";
70 | /** Connections Bundle Key **/
71 | static final String connections = "connections";
72 | /** Clean Session Flag Bundle Key **/
73 | public static final String cleanSession = "cleanSession";
74 | /** Action Bundle Key **/
75 | static final String action = "action";
76 |
77 | /* Property names */
78 |
79 | /** Property name for the history field in {@link Connection} object for use with {@link java.beans.PropertyChangeEvent} **/
80 | static final String historyProperty = "history";
81 |
82 | /** Property name for the connection status field in {@link Connection} object for use with {@link java.beans.PropertyChangeEvent} **/
83 | static final String ConnectionStatusProperty = "connectionStatus";
84 |
85 | /* Useful constants*/
86 |
87 | /** Space String Literal **/
88 | static final String space = " ";
89 | /** Empty String for comparisons **/
90 | public static final String empty = new String();
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/sdk/src/main/java/com/pandocloud/android/agent/MqttCallbackHandler.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 1999, 2014 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * and Eclipse Distribution License v1.0 which accompany this distribution.
7 | *
8 | * The Eclipse Public License is available at
9 | * http://www.eclipse.org/legal/epl-v10.html
10 | * and the Eclipse Distribution License is available at
11 | * http://www.eclipse.org/org/documents/edl-v10.php.
12 | */
13 | package com.pandocloud.android.agent;
14 |
15 | import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
16 | import org.eclipse.paho.client.mqttv3.MqttCallback;
17 | import org.eclipse.paho.client.mqttv3.MqttMessage;
18 | import android.content.Context;
19 | import android.content.Intent;
20 | import com.pandocloud.android.agent.Connection.ConnectionStatus;
21 | import com.pandocloud.android.R;
22 |
23 | /**
24 | * Handles call backs from the MQTT Client
25 | *
26 | */
27 | public class MqttCallbackHandler implements MqttCallback {
28 |
29 | /** {@link Context} for the application used to format and import external strings**/
30 | private Context context;
31 | /** Client handle to reference the connection that this handler is attached to**/
32 | private String clientHandle;
33 |
34 | private Connection c;
35 |
36 | /**
37 | * Creates an MqttCallbackHandler
object
38 | * @param context The application's context
39 | * @param clientHandle The handle to a {@link Connection} object
40 | */
41 | public MqttCallbackHandler(Context context, Connection connection, String clientHandle)
42 | {
43 | this.context = context;
44 | this.c = connection;
45 | this.clientHandle = clientHandle;
46 | }
47 |
48 | /**
49 | * @see org.eclipse.paho.client.mqttv3.MqttCallback#connectionLost(java.lang.Throwable)
50 | */
51 | @Override
52 | public void connectionLost(Throwable cause) {
53 | //cause.printStackTrace();
54 | if (cause != null) {
55 | //Connection c = Connections.getInstance(context).getConnection(clientHandle);
56 | c.addAction("Connection Lost");
57 | c.changeConnectionStatus(ConnectionStatus.DISCONNECTED);
58 |
59 | //format string to use a notification text
60 | Object[] args = new Object[2];
61 | args[0] = c.getId();
62 | args[1] = c.getHostName();
63 |
64 | String message = context.getString(R.string.connection_lost, args);
65 |
66 | //build intent
67 | Intent intent = new Intent();
68 | intent.setClassName(context, "org.eclipse.paho.android.service.sample.ConnectionDetails");
69 | intent.putExtra("handle", clientHandle);
70 |
71 | //notify the user
72 | Notify.notifcation(context, message, intent, R.string.notifyTitle_connectionLost);
73 | }
74 | }
75 |
76 | /**
77 | * @see org.eclipse.paho.client.mqttv3.MqttCallback#messageArrived(java.lang.String, org.eclipse.paho.client.mqttv3.MqttMessage)
78 | */
79 | @Override
80 | public void messageArrived(String topic, MqttMessage message) throws Exception {
81 |
82 | //Get connection object associated with this object
83 | //Connection c = Connections.getInstance(context).getConnection(clientHandle);
84 |
85 | //create arguments to format message arrived notifcation string
86 | String[] args = new String[2];
87 | args[0] = new String(message.getPayload());
88 | args[1] = topic+";qos:"+message.getQos()+";retained:"+message.isRetained();
89 |
90 | //get the string from strings.xml and format
91 | String messageString = context.getString(R.string.messageRecieved, (Object[]) args);
92 |
93 | //create intent to start activity
94 | Intent intent = new Intent();
95 | intent.setClassName(context, "org.eclipse.paho.android.service.sample.ConnectionDetails");
96 | intent.putExtra("handle", clientHandle);
97 |
98 | //format string args
99 | Object[] notifyArgs = new String[3];
100 | notifyArgs[0] = c.getId();
101 | notifyArgs[1] = new String(message.getPayload());
102 | notifyArgs[2] = topic;
103 |
104 | //notify the user
105 | Notify.notifcation(context, context.getString(R.string.notification, notifyArgs), intent, R.string.notifyTitle);
106 |
107 | //update client history
108 | c.addAction(messageString);
109 |
110 | }
111 |
112 | /**
113 | * @see org.eclipse.paho.client.mqttv3.MqttCallback#deliveryComplete(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)
114 | */
115 | @Override
116 | public void deliveryComplete(IMqttDeliveryToken token) {
117 | // Do nothing
118 | }
119 | }
120 |
--------------------------------------------------------------------------------
/sdk/src/main/java/com/pandocloud/android/agent/MqttTraceCallback.java:
--------------------------------------------------------------------------------
1 | package com.pandocloud.android.agent;
2 |
3 | import org.eclipse.paho.android.service.MqttTraceHandler;
4 |
5 | import android.util.Log;
6 |
7 | public class MqttTraceCallback implements MqttTraceHandler {
8 |
9 | public void traceDebug(java.lang.String arg0, java.lang.String arg1) {
10 | Log.i(arg0, arg1);
11 | };
12 |
13 | public void traceError(java.lang.String arg0, java.lang.String arg1) {
14 | Log.e(arg0, arg1);
15 | };
16 |
17 | public void traceException(java.lang.String arg0, java.lang.String arg1,
18 | java.lang.Exception arg2) {
19 | Log.e(arg0, arg1, arg2);
20 | };
21 |
22 | }
--------------------------------------------------------------------------------
/sdk/src/main/java/com/pandocloud/android/agent/Notify.java:
--------------------------------------------------------------------------------
1 | package com.pandocloud.android.agent;
2 |
3 | import java.util.Calendar;
4 | import android.app.Notification;
5 | import android.app.NotificationManager;
6 | import android.app.PendingIntent;
7 | import android.content.Context;
8 | import android.content.Intent;
9 | import android.support.v4.app.NotificationCompat.Builder;
10 | import android.widget.Toast;
11 | import com.pandocloud.android.R;
12 |
13 | /**
14 | * Provides static methods for creating and showing notifications to the user.
15 | *
16 | */
17 | public class Notify {
18 |
19 | /** Message ID Counter **/
20 | private static int MessageID = 0;
21 |
22 | /**
23 | * Displays a notification in the notification area of the UI
24 | * @param context Context from which to create the notification
25 | * @param messageString The string to display to the user as a message
26 | * @param intent The intent which will start the activity when the user clicks the notification
27 | * @param notificationTitle The resource reference to the notification title
28 | */
29 | static void notifcation(Context context, String messageString, Intent intent, int notificationTitle) {
30 |
31 | //Get the notification manage which we will use to display the notification
32 | String ns = Context.NOTIFICATION_SERVICE;
33 | NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
34 |
35 | Calendar.getInstance().getTime().toString();
36 |
37 | long when = System.currentTimeMillis();
38 |
39 | //get the notification title from the application's strings.xml file
40 | CharSequence contentTitle = context.getString(notificationTitle);
41 |
42 | //the message that will be displayed as the ticker
43 | String ticker = contentTitle + " " + messageString;
44 |
45 | //build the pending intent that will start the appropriate activity
46 | PendingIntent pendingIntent = PendingIntent.getActivity(context,
47 | Constants.showHistory, intent, 0);
48 |
49 | //build the notification
50 | Builder notificationCompat = new Builder(context);
51 | notificationCompat.setAutoCancel(true)
52 | .setContentTitle(contentTitle)
53 | .setContentIntent(pendingIntent)
54 | .setContentText(messageString)
55 | .setTicker(ticker)
56 | .setWhen(when)
57 | .setSmallIcon(R.drawable.ic_launcher);
58 |
59 | Notification notification = notificationCompat.build();
60 | //display the notification
61 | mNotificationManager.notify(MessageID, notification);
62 | MessageID++;
63 |
64 | }
65 |
66 | /**
67 | * Display a toast notification to the user
68 | * @param context Context from which to create a notification
69 | * @param text The text the toast should display
70 | * @param duration The amount of time for the toast to appear to the user
71 | */
72 | static void toast(Context context, CharSequence text, int duration) {
73 | Toast toast = Toast.makeText(context, text, duration);
74 | toast.show();
75 | }
76 |
77 | }
--------------------------------------------------------------------------------
/sdk/src/main/java/com/pandocloud/android/agent/PandoService.java:
--------------------------------------------------------------------------------
1 | package com.pandocloud.android.agent;
2 |
3 | import org.eclipse.paho.android.service.MqttAndroidClient;
4 | import org.eclipse.paho.android.service.MqttService;
5 | import org.eclipse.paho.client.mqttv3.MqttClientPersistence;
6 | import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
7 | import org.eclipse.paho.client.mqttv3.MqttException;
8 | import org.eclipse.paho.client.mqttv3.MqttSecurityException;
9 |
10 |
11 | import android.content.BroadcastReceiver;
12 | import android.content.Context;
13 | import android.content.Intent;
14 | import android.content.IntentFilter;
15 | import android.os.Bundle;
16 | import android.os.Handler;
17 | import android.util.Log;
18 |
19 | public class PandoService extends MqttService{
20 |
21 | private String TAG = "PandoService";
22 |
23 | private String serverUri = "tcp://120.24.241.177:1883";
24 | private String cliendId = "11";
25 | private String contextId = "ctxId";
26 | private String clientHandle = serverUri+":"+cliendId+":"+contextId;
27 | private String activityToken = "PandoService";
28 | private MqttConnectOptions conOpt = new MqttConnectOptions();
29 |
30 | private CallbackBroadcastReceiver callback = new CallbackBroadcastReceiver();
31 |
32 | public PandoService() {
33 |
34 | }
35 |
36 | public void setMessageHandler(Handler h) {
37 |
38 | }
39 |
40 | public String getAndroidDeviceKey() {
41 | String s = new String();
42 | return s;
43 | }
44 |
45 | public void connect() {
46 | Log.d(TAG, "do connect === ");
47 | getClient(serverUri, cliendId, contextId, (MqttClientPersistence)null);
48 | try {
49 | super.connect(clientHandle, conOpt, this.toString(), activityToken);
50 | } catch (MqttException e) {
51 | e.printStackTrace();
52 | }
53 | }
54 |
55 | public class CallbackBroadcastReceiver extends BroadcastReceiver {
56 | @Override
57 | public void onReceive(Context context, Intent intent) {
58 | String action = intent.getAction();
59 | Log.d(TAG, action);
60 | if("MqttService.callbackToActivity.v0".equals(action)) {
61 | String ch = intent.getStringExtra("MqttService.clientHandle");
62 | Status sta = intent.getParcelableExtra("MqttService.callbackStatus");
63 |
64 | Bundle b = intent.getExtras();
65 | String at = b.getString("MqttService.activityToken");
66 | String ic = b.getString("MqttService.invocationContext");
67 | String act = b.getString("MqttService.callbackAction");
68 |
69 | Log.d(TAG, "clientHandle="+ch+", status="+sta+", activityToken="+at+", invocationContext="+ic+", callbackAction="+act);
70 | }
71 | }
72 | }
73 |
74 | public void onCreate() {
75 | Log.d(TAG, "PandoService created ===");
76 | super.onCreate();
77 |
78 | int timeout = Constants.defaultTimeOut;
79 | int keepAlive = Constants.defaultKeepAlive;
80 | conOpt.setConnectionTimeout(timeout);
81 | conOpt.setKeepAliveInterval(keepAlive);
82 |
83 | IntentFilter filter = new IntentFilter();
84 | filter.addAction("MqttService.callbackToActivity.v0");
85 | registerReceiver(callback, filter);
86 |
87 | connect();
88 | }
89 |
90 | }
91 |
--------------------------------------------------------------------------------
/sdk/src/main/java/com/pandocloud/android/agent/Status.java:
--------------------------------------------------------------------------------
1 | package com.pandocloud.android.agent;
2 |
3 | /**
4 | * Created by felixqiu on 2015/8/30.
5 | */
6 | enum Status {
7 | OK,
8 | ERROR,
9 | NO_RESULT;
10 |
11 | private Status() {
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/sdk/src/main/java/com/pandocloud/android/agent/UeServerPayload.java:
--------------------------------------------------------------------------------
1 | package com.pandocloud.android.agent;
2 |
3 | public class UeServerPayload {
4 |
5 | private byte flag;
6 | private byte[] timestamp;
7 | private byte[] token;
8 | private UeSubEquipmentPayload payload;
9 | }
10 |
--------------------------------------------------------------------------------
/sdk/src/main/java/com/pandocloud/android/agent/UeSubEquipmentPayload.java:
--------------------------------------------------------------------------------
1 | package com.pandocloud.android.agent;
2 |
3 | public class UeSubEquipmentPayload {
4 |
5 | class TLV {
6 | private byte[] type;
7 | private byte[] length;
8 | private byte[] value;
9 | }
10 |
11 | class Data {
12 | private byte[] subdeviceId;
13 | private byte[] propertyNum;
14 | private TLV[] params;
15 | }
16 |
17 | class Command {
18 | private byte[] subdeviceId;
19 | private byte[] commandNum;
20 | private byte[] priority;
21 | private TLV[] params;
22 | }
23 |
24 | class Event {
25 | private byte[] subdeviceId;
26 | private byte[] eventNum;
27 | private byte[] priority;
28 | private TLV[] params;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/sdk/src/main/java/com/pandocloud/android/agent/codec/Command.java:
--------------------------------------------------------------------------------
1 | package com.pandocloud.android.agent.codec;
2 |
3 | import java.io.ByteArrayInputStream;
4 | import java.io.ByteArrayOutputStream;
5 | import java.io.DataInputStream;
6 | import java.io.DataOutputStream;
7 | import java.util.ArrayList;
8 | import java.util.List;
9 |
10 | /**
11 | * Created by ruizeng on 8/28/15.
12 | */
13 |
14 | public class Command {
15 |
16 | public short subdeviceId;
17 | public short commandNum;
18 | public short priority;
19 | public Listif has access token return true
106 | *else return false
107 | * @return 108 | */ 109 | public boolean hasAccessToken() { 110 | if (TextUtils.isEmpty(token)) { 111 | token = prefs.getString(DeviceState.ACCESS_TOKEN, ""); 112 | if (TextUtils.isEmpty(token)) { 113 | return false; 114 | } 115 | } 116 | return true; 117 | } 118 | 119 | public int getDeviceId() { 120 | if (deviceId <= 0) { 121 | deviceId = prefs.getInt(DeviceState.DEVICE_ID, 0); 122 | } 123 | return deviceId; 124 | } 125 | 126 | 127 | public long getEventSequence() { 128 | if (eventSequence == 0) { 129 | eventSequence = prefs.getLong(EVENT_SEQUENCE, 1); 130 | } else { 131 | eventSequence ++; 132 | prefs.edit().putLong(EVENT_SEQUENCE, eventSequence); 133 | } 134 | return eventSequence; 135 | } 136 | 137 | 138 | public long getDataSequence() { 139 | if (dataSequence == 0) { 140 | dataSequence = prefs.getLong(DATA_SEQUENCE, 1); 141 | } else { 142 | dataSequence ++; 143 | prefs.edit().putLong(EVENT_SEQUENCE, dataSequence); 144 | } 145 | return dataSequence; 146 | } 147 | 148 | 149 | /** 150 | * 判断设备配置信息是否含有key值 151 | * @param key 152 | * @return 153 | */ 154 | public boolean containsKey(String key) { 155 | return prefs.contains(key); 156 | } 157 | 158 | /** 159 | * 清楚所有设备配置信息 160 | */ 161 | public void clear(){ 162 | deviceId = -1; 163 | token = null; 164 | prefs.edit().clear().commit(); 165 | } 166 | 167 | public static class Builder { 168 | 169 | private SharedPreferences.Editor mEditor; 170 | 171 | public Builder(Context context) { 172 | if (mEditor == null) { 173 | mEditor = DeviceState.getInstances(context).getSharedPreferences().edit(); 174 | } 175 | } 176 | 177 | public Builder saveString(String key, String value) { 178 | mEditor.putString(key, value); 179 | return this; 180 | } 181 | 182 | public Builder saveLong(String key, long value) { 183 | mEditor.putLong(key, value); 184 | return this; 185 | } 186 | 187 | public Builder saveInt(String key, int value) { 188 | mEditor.putInt(key, value); 189 | return this; 190 | } 191 | 192 | public Builder saveBoolean(String key, boolean value) { 193 | mEditor.putBoolean(key, value); 194 | return this; 195 | } 196 | 197 | public Builder saveFloat(String key, float value) { 198 | mEditor.putFloat(key, value); 199 | return this; 200 | } 201 | 202 | public void commit(){ 203 | mEditor.commit(); 204 | } 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /sdk/src/main/java/com/pandocloud/android/api/interfaces/RequestListener.java: -------------------------------------------------------------------------------- 1 | package com.pandocloud.android.api.interfaces; 2 | 3 | public interface RequestListener { 4 | 5 | public void onPrepare(); 6 | 7 | public void onSuccess(); 8 | 9 | public void onFail(Exception e); 10 | 11 | public void onFinish(); 12 | } 13 | -------------------------------------------------------------------------------- /sdk/src/main/java/com/pandocloud/android/api/interfaces/SimpleRequestListener.java: -------------------------------------------------------------------------------- 1 | package com.pandocloud.android.api.interfaces; 2 | 3 | public class SimpleRequestListener implements RequestListener { 4 | 5 | @Override 6 | public void onPrepare() { 7 | // TODO Auto-generated method stub 8 | 9 | } 10 | 11 | @Override 12 | public void onSuccess() { 13 | // TODO Auto-generated method stub 14 | 15 | } 16 | 17 | @Override 18 | public void onFail(Exception e) { 19 | // TODO Auto-generated method stub 20 | 21 | } 22 | 23 | @Override 24 | public void onFinish() { 25 | // TODO Auto-generated method stub 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /sdk/src/main/java/com/pandocloud/android/api/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * this package provides deivce register and loign api. 3 | */ 4 | package com.pandocloud.android.api; -------------------------------------------------------------------------------- /sdk/src/main/java/com/pandocloud/android/config/wifi/WifiConfigConsts.java: -------------------------------------------------------------------------------- 1 | package com.pandocloud.android.config.wifi; 2 | 3 | public class WifiConfigConsts { 4 | 5 | // 网关包头设置 6 | public static final short PACKET_START_DATA = 0x7064; 7 | 8 | public static final int PACKET_START_LEN = 2; 9 | 10 | public static final int PACKET_TYPE_LEN = 2; 11 | 12 | public static final int PACKET_LEN = 4; 13 | 14 | public static final int PACKET_TOTAL_LEN = PACKET_START_LEN + PACKET_TYPE_LEN + PACKET_LEN; 15 | 16 | /* 17 | // result 18 | public static final String MESSAGE_OK = "ok"; 19 | 20 | public static final String MESSAGE_ERROR = "error"; 21 | */ 22 | 23 | 24 | 25 | public static String DEVICE_HOST = "192.168.4.1"; 26 | 27 | public static final int DEVICE_PORT = 8890; 28 | 29 | // 网关协议版本 30 | public static final String GATEWAY_VERSION = "0.1.0"; 31 | } 32 | -------------------------------------------------------------------------------- /sdk/src/main/java/com/pandocloud/android/config/wifi/WifiConfigManager.java: -------------------------------------------------------------------------------- 1 | package com.pandocloud.android.config.wifi; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.support.v4.content.LocalBroadcastManager; 6 | import android.util.Log; 7 | 8 | 9 | public final class WifiConfigManager { 10 | // 返回结果 11 | public static final int CONFIG_SUCCESS = 0; 12 | 13 | public static final int CONFIG_FAILED = -1; 14 | 15 | public static final int DEVICE_CONNECT_FAILED = -2; 16 | 17 | public static final int DEVICE_SEND_FAILED = -3; 18 | 19 | public static final int DEVICE_RECV_FAILED = -4; 20 | 21 | public static final int CONFIG_TIMEOUT = -5; 22 | 23 | // 配置模式 24 | public static final String CONFIG_MODE_HOTSPOT = "hotspot"; 25 | 26 | public static final String CONFIG_MODE_SMARTLINK = "smartlink"; 27 | 28 | 29 | public static Context getContext() { 30 | return context; 31 | } 32 | 33 | private static Context context = null; 34 | 35 | private static WifiConfigMessageHandler msgHandler = null; 36 | 37 | /** 38 | * 设置配置结果的回调 39 | * @param handler 配置结果回调处理器 40 | */ 41 | public static void setMsgHandler(WifiConfigMessageHandler handler) { 42 | msgHandler = handler; 43 | } 44 | 45 | public static WifiConfigMessageHandler getMsgHandler(){ 46 | return msgHandler; 47 | } 48 | 49 | /** 50 | * 启动配置模式 51 | * @param context 传入当前activity的context 52 | * @param mode 当前配置模式 53 | * @param ssid 需要连接的wifi名 54 | * @param pwd 对应的wifi密码 55 | */ 56 | public static void startConfig(Context context, String mode, String ssid, String pwd) { 57 | Log.d("pandocloud", "entering config mode "+mode+", ssid: "+ssid+", password: "+pwd); 58 | if(msgHandler == null){ 59 | Log.e("pandocloud", "config message handler is not set!"); 60 | return; 61 | } 62 | WifiConfigManager.context = context.getApplicationContext(); 63 | Intent intent = new Intent(context, WifiConfigService.class); 64 | intent.putExtra("mode", mode); 65 | intent.putExtra("ssid", ssid); 66 | intent.putExtra("wifi_pwd", pwd); 67 | context.startService(intent); 68 | } 69 | 70 | /** 71 | * 退出配置模式 72 | */ 73 | public static void stopConfig() { 74 | if (context == null) { 75 | return; 76 | } 77 | Intent intent = new Intent(WifiConfigService.STOP_CONNECT_DEVICE_ACTION); 78 | LocalBroadcastManager.getInstance(context).sendBroadcast(intent); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /sdk/src/main/java/com/pandocloud/android/config/wifi/WifiConfigMessageHandler.java: -------------------------------------------------------------------------------- 1 | package com.pandocloud.android.config.wifi; 2 | 3 | 4 | import android.os.Handler; 5 | import android.os.Message; 6 | 7 | public class WifiConfigMessageHandler { 8 | 9 | private Handler mHandler; 10 | 11 | 12 | public WifiConfigMessageHandler(Handler handler) { 13 | this.mHandler = handler; 14 | } 15 | 16 | 17 | public void sendMessage(Message msg) { 18 | this.mHandler.sendMessage(msg); 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /sdk/src/main/java/com/pandocloud/android/config/wifi/WifiConfigService.java: -------------------------------------------------------------------------------- 1 | package com.pandocloud.android.config.wifi; 2 | 3 | 4 | import android.app.Service; 5 | import android.content.BroadcastReceiver; 6 | import android.content.Context; 7 | import android.content.Intent; 8 | import android.content.IntentFilter; 9 | import android.os.Bundle; 10 | import android.os.IBinder; 11 | import android.support.v4.content.LocalBroadcastManager; 12 | import android.util.Log; 13 | 14 | 15 | import com.pandocloud.android.config.wifi.deviceconnect.DeviceConnect; 16 | import com.pandocloud.android.config.wifi.smartconfig.SmartConfig; 17 | 18 | 19 | public class WifiConfigService extends Service { 20 | 21 | public static final String STOP_CONNECT_DEVICE_ACTION = "com.pandocloud.android.STOP_WIFI_CONFIG_ACTION"; 22 | 23 | private String ssid; 24 | private String password; 25 | 26 | @Override 27 | public IBinder onBind(Intent intent) { 28 | return null; 29 | } 30 | 31 | 32 | @Override 33 | public void onCreate() { 34 | super.onCreate(); 35 | LocalBroadcastManager.getInstance(this) 36 | .registerReceiver(stopConnectReceiver, new IntentFilter(STOP_CONNECT_DEVICE_ACTION)); 37 | } 38 | 39 | @Override 40 | public int onStartCommand(Intent intent, int flags, int startId) { 41 | Log.i("pandocloud", "wifi config service started..."); 42 | handleServiceStart(intent); 43 | 44 | return Service.START_STICKY; 45 | } 46 | 47 | 48 | public void handleServiceStart(Intent intent) { 49 | if (intent == null) { 50 | return; 51 | } 52 | Bundle bundle = intent.getExtras(); 53 | if (bundle != null) { 54 | String mode = bundle.getString("mode"); 55 | ssid = bundle.getString("ssid"); 56 | password = bundle.getString("wifi_pwd"); 57 | switch (mode){ 58 | case WifiConfigManager.CONFIG_MODE_SMARTLINK: 59 | smartConfig(); 60 | break; 61 | case WifiConfigManager.CONFIG_MODE_HOTSPOT: 62 | connectAndConfigDeviceHotspotMode(); 63 | break; 64 | default: 65 | Log.e("pandocloud", "unknown config mode: " + mode); 66 | break; 67 | } 68 | 69 | } 70 | 71 | } 72 | 73 | BroadcastReceiver stopConnectReceiver = new BroadcastReceiver() { 74 | 75 | @Override 76 | public void onReceive(Context context, Intent intent) { 77 | Log.i("pandocloud", "wifi config service stopped."); 78 | WifiConfigService.this.stopSelf(); 79 | } 80 | }; 81 | 82 | private void connectAndConfigDeviceHotspotMode() { 83 | DeviceConnect dc = new DeviceConnect(this, WifiConfigManager.CONFIG_MODE_HOTSPOT); 84 | dc.setWifiInfo(ssid, password); 85 | dc.setMsgHandler(WifiConfigManager.getMsgHandler()); 86 | dc.conn(); 87 | } 88 | 89 | private void smartConfig() { 90 | SmartConfig sc = new SmartConfig(ssid, password); 91 | sc.setMsgHandler(WifiConfigManager.getMsgHandler()); 92 | sc.start(); 93 | } 94 | 95 | public void onDestroy() { 96 | super.onDestroy(); 97 | 98 | LocalBroadcastManager.getInstance(this) 99 | .unregisterReceiver(stopConnectReceiver); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /sdk/src/main/java/com/pandocloud/android/config/wifi/deviceconnect/bean/GWAction.java: -------------------------------------------------------------------------------- 1 | package com.pandocloud.android.config.wifi.deviceconnect.bean; 2 | 3 | import com.pandocloud.android.config.wifi.deviceconnect.interfaces.StringFormat; 4 | 5 | import org.json.JSONException; 6 | import org.json.JSONStringer; 7 | 8 | 9 | public class GWAction implements StringFormat { 10 | 11 | public String action; 12 | 13 | @Override 14 | public String toJsonString() { 15 | try { 16 | return new JSONStringer().object().key("action").value(action).endObject().toString(); 17 | } catch (JSONException e) { 18 | e.printStackTrace(); 19 | return "{\"action\":" + action + "}"; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /sdk/src/main/java/com/pandocloud/android/config/wifi/deviceconnect/bean/GWResult.java: -------------------------------------------------------------------------------- 1 | package com.pandocloud.android.config.wifi.bean; 2 | 3 | import com.pandocloud.android.config.wifi.deviceconnect.interfaces.StringFormat; 4 | 5 | import org.json.JSONException; 6 | import org.json.JSONStringer; 7 | 8 | 9 | public class GWResult implements StringFormat { 10 | 11 | public int code; 12 | 13 | public String message; 14 | 15 | @Override 16 | public String toJsonString() { 17 | try { 18 | return new JSONStringer().object().key("code").value(code).key("message").value(message).object().toString(); 19 | } catch (JSONException e) { 20 | e.printStackTrace(); 21 | return "{\"code\":"+ code +",\"message:" + message + "}"; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /sdk/src/main/java/com/pandocloud/android/config/wifi/deviceconnect/bean/GWToken.java: -------------------------------------------------------------------------------- 1 | package com.pandocloud.android.config.wifi.deviceconnect.bean; 2 | 3 | import com.pandocloud.android.config.wifi.deviceconnect.interfaces.StringFormat; 4 | 5 | import org.json.JSONException; 6 | import org.json.JSONStringer; 7 | 8 | 9 | public class GWToken implements StringFormat { 10 | 11 | public String token; 12 | 13 | @Override 14 | public String toJsonString() { 15 | try { 16 | return new JSONStringer().object() 17 | .key("token").value(token) 18 | .endObject().toString(); 19 | } catch (JSONException e) { 20 | e.printStackTrace(); 21 | return "{\"token\":" + token + "}"; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /sdk/src/main/java/com/pandocloud/android/config/wifi/deviceconnect/bean/WifiInfo.java: -------------------------------------------------------------------------------- 1 | package com.pandocloud.android.config.wifi.deviceconnect.bean; 2 | 3 | import com.pandocloud.android.config.wifi.deviceconnect.interfaces.StringFormat; 4 | 5 | import org.json.JSONException; 6 | import org.json.JSONStringer; 7 | 8 | 9 | public class WifiInfo implements StringFormat { 10 | 11 | public String ssid; 12 | 13 | public String password; 14 | 15 | public String action; 16 | 17 | @Override 18 | public String toJsonString() { 19 | try { 20 | return new JSONStringer().object() 21 | .key("ssid").value(ssid) 22 | .key("password").value(password) 23 | .key("action").value(action) 24 | .endObject().toString(); 25 | } catch (JSONException e) { 26 | e.printStackTrace(); 27 | return ""; 28 | } 29 | } 30 | 31 | 32 | @Override 33 | public String toString() { 34 | return "WifiInfo [ssid=" + ssid + ", password=" + password 35 | + ", action=" + action + "]"; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /sdk/src/main/java/com/pandocloud/android/config/wifi/deviceconnect/interfaces/OnPacketHandler.java: -------------------------------------------------------------------------------- 1 | package com.pandocloud.android.config.wifi.deviceconnect.interfaces; 2 | 3 | 4 | public interface OnPacketHandler