安卓初次完美调试,并成功编程!

Posted 极客阿正

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓初次完美调试,并成功编程!相关的知识,希望对你有一定的参考价值。

语言都是相通的,爽歪歪2017-12-0317:33:58

这是GosDeviceControlActivity.java的代码

  1 package com.gizwits.opensource.appkit.ControlModule;
  2 
  3 import android.app.AlertDialog;
  4 import android.app.Dialog;
  5 import android.content.DialogInterface;
  6 import android.content.DialogInterface.OnDismissListener;
  7 import android.content.Intent;
  8 import android.os.Bundle;
  9 import android.os.Handler;
 10 import android.os.Message;
 11 import android.text.TextUtils;
 12 import android.util.Log;
 13 import android.view.KeyEvent;
 14 import android.view.Menu;
 15 import android.view.MenuItem;
 16 import android.view.View;
 17 import android.view.Window;
 18 import android.view.View.OnClickListener;
 19 import android.widget.AdapterView;
 20 import android.widget.AdapterView.OnItemSelectedListener;
 21 import android.widget.EditText;
 22 import android.widget.Button;
 23 import android.widget.LinearLayout;
 24 import android.widget.SeekBar;
 25 import android.widget.SeekBar.OnSeekBarChangeListener;
 26 import android.widget.Spinner;
 27 import android.widget.Switch;
 28 import android.widget.TextView;
 29 import android.widget.TextView.OnEditorActionListener;
 30 import android.widget.Toast;
 31 
 32 import java.util.concurrent.ConcurrentHashMap;
 33 
 34 import com.gizwits.gizwifisdk.api.GizWifiDevice;
 35 import com.gizwits.gizwifisdk.enumration.GizWifiDeviceNetStatus;
 36 import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
 37 import com.gizwits.opensource.appkit.R;
 38 import com.gizwits.opensource.appkit.utils.HexStrUtils;
 39 import com.gizwits.opensource.appkit.view.HexWatcher;
 40 
 41 public class GosDeviceControlActivity extends GosControlModuleBaseActivity
 42         implements OnClickListener, OnEditorActionListener, OnSeekBarChangeListener {
 43 
 44     /** 设备列表传入的设备变量 */
 45     private GizWifiDevice mDevice;
 46     private Switch sw_bool_open;
 47     private Switch sw_bool_test;
 48     private Button btn_led;
 49     private enum handler_key {
 50         /** 更新界面 */
 51         UPDATE_UI,
 52 
 53         DISCONNECT,
 54     }
 55 
 56     private Runnable mRunnable = new Runnable() {
 57         public void run() {
 58             if (isDeviceCanBeControlled()) {
 59                 progressDialog.cancel();
 60             } else {
 61                 toastDeviceNoReadyAndExit();
 62             }
 63         }
 64 
 65     };
 66 
 67     /** The handler. */
 68     Handler mHandler = new Handler() {
 69         public void handleMessage(Message msg) {
 70             super.handleMessage(msg);
 71             handler_key key = handler_key.values()[msg.what];
 72             switch (key) {
 73             case UPDATE_UI:
 74                 updateUI();
 75                 break;
 76             case DISCONNECT:
 77                 toastDeviceDisconnectAndExit();
 78                 break;
 79             }
 80         }
 81     };
 82 
 83     @Override
 84     protected void onCreate(Bundle savedInstanceState) {
 85         super.onCreate(savedInstanceState);
 86         setContentView(R.layout.activity_gos_device_control);
 87         initDevice();
 88         setActionBar(true, true, getDeviceName());
 89         initView();
 90         initEvent();
 91     }
 92                           /**
 93                           * 初始化控件
 94                            */
 95     
 96     private void initView() {
 97         
 98         sw_bool_open = (Switch) findViewById(R.id.sw_bool_open);
 99         sw_bool_test = (Switch) findViewById(R.id.sw_bool_test);
100           btn_led  = (Button) findViewById(R.id.btn_led);  
101     }
102 
103     private void initEvent() {
104 
105         sw_bool_open.setOnClickListener(this);
106         sw_bool_test.setOnClickListener(this);
107         btn_led.setOnClickListener(this);
108     
109     }
110      /**
111      * 初始化设备
112       */
113     private void initDevice() {
114         Intent intent = getIntent();
115         mDevice = (GizWifiDevice) intent.getParcelableExtra("GizWifiDevice");
116         mDevice.setListener(gizWifiDeviceListener);
117         Log.i("Apptest", mDevice.getDid());
118     }
119 
120     private String getDeviceName() {
121         if (TextUtils.isEmpty(mDevice.getAlias())) {
122             return mDevice.getProductName();
123         }
124         return mDevice.getAlias();
125     }
126 
127     @Override
128     protected void onResume() {
129         super.onResume();
130         getStatusOfDevice();
131     }
132 
133     @Override
134     protected void onDestroy() {
135         super.onDestroy();
136         mHandler.removeCallbacks(mRunnable);
137         // 退出页面,取消设备订阅
138         mDevice.setSubscribe(false);
139         mDevice.setListener(null);
140     }
141 
142     @Override
143     public void onClick(View v) {
144         switch (v.getId()) {
145         case R.id.sw_bool_open:
146             sendCommand(KEY_OPEN, sw_bool_open.isChecked());
147             break;
148         case R.id.sw_bool_test:
149             sendCommand(KEY_TEST, sw_bool_test.isChecked());
150              /**
151              * 初始化设备
152              * 激动人心的时刻,我利用纯属的想象,猜测sw_bool_test.isChecked()必定会返回ture 或者false,就
153              * 利用判断及挖掘了动画效果
154              * 2017年12月3日
155               */
156             if(sw_bool_test.isChecked()) {
157                 btn_led.setSelected(true);    
158                 }
159                 else 
160                 btn_led.setSelected(false);    
161             break;
162         default:
163             break;    
164     
165                             
166         }
167         
168         
169         
170         
171     }
172 
173     /*
174      * ========================================================================
175      * EditText 点击键盘“完成”按钮方法
176      * ========================================================================
177      */
178     @Override
179     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
180 
181         switch (v.getId()) {
182         default:
183             break;
184         }
185         hideKeyBoard();
186         return false;
187 
188     }
189     
190     /*
191      * ========================================================================
192      * seekbar 回调方法重写
193      * ========================================================================
194      */
195     @Override
196     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
197         
198         switch (seekBar.getId()) {
199         default:
200             break;
201         }
202     }
203 
204     @Override
205     public void onStartTrackingTouch(SeekBar seekBar) {
206 
207     }
208 
209     @Override
210     public void onStopTrackingTouch(SeekBar seekBar) {
211         switch (seekBar.getId()) {
212         default:
213             break;
214         }
215     }
216 
217     /*
218      * ========================================================================
219      * 菜单栏
220      * ========================================================================
221      */
222     @Override
223     public boolean onCreateOptionsMenu(Menu menu) {
224         getMenuInflater().inflate(R.menu.device_more, menu);
225         return super.onCreateOptionsMenu(menu);
226     }
227 
228     @Override
229     public boolean onOptionsItemSelected(MenuItem item) {
230         switch (item.getItemId()) {
231 
232         case R.id.action_setDeviceInfo:
233             setDeviceInfo();
234             break;
235 
236         case R.id.action_getHardwareInfo:
237             if (mDevice.isLAN()) {
238                 mDevice.getHardwareInfo();
239             } else {
240                 myToast("只允许在局域网下获取设备硬件信息!");
241             }
242             break;
243 
244         case R.id.action_getStatu:
245             mDevice.getDeviceStatus();
246             break;
247 
248         default:
249             break;
250         }
251 
252         return super.onOptionsItemSelected(item);
253     }
254 
255     /**
256      * Description:根据保存的的数据点的值来更新UI
257      */
258     protected void updateUI() {
259         
260         sw_bool_open.setChecked(data_open);
261         sw_bool_test.setChecked(data_test);
262     
263     }
264 
265     private void setEditText(EditText et, Object value) {
266         et.setText(value.toString());
267         et.setSelection(value.toString().length());
268         et.clearFocus();
269     }
270 
271     /**
272      * Description:页面加载后弹出等待框,等待设备可被控制状态回调,如果一直不可被控,等待一段时间后自动退出界面
273      */
274     private void getStatusOfDevice() {
275         // 设备是否可控
276         if (isDeviceCanBeControlled()) {
277             // 可控则查询当前设备状态
278             mDevice.getDeviceStatus();
279         } else {
280             // 显示等待栏
281             progressDialog.show();
282             if (mDevice.isLAN()) {
283                 // 小循环10s未连接上设备自动退出
284                 mHandler.postDelayed(mRunnable, 10000);
285             } else {
286                 // 大循环20s未连接上设备自动退出
287                 mHandler.postDelayed(mRunnable, 20000);
288             }
289         }
290     }
291 
292     /**
293      * 发送指令,下发单个数据点的命令可以用这个方法
294      * 
295      * <h3>注意</h3>
296      * <p>
297      * 下发多个数据点命令不能用这个方法多次调用,一次性多次调用这个方法会导致模组无法正确接收消息,参考方法内注释。
298      * </p>
299      * 
300      * @param key
301      *            数据点对应的标识名
302      * @param value
303      *            需要改变的值
304      */
305     private void sendCommand(String key, Object value) {
306         if (value == null) {
307             return;
308         }
309         int sn = 5;
310         ConcurrentHashMap<String, Object> hashMap = new ConcurrentHashMap<String, Object>();
311         hashMap.put(key, value);
312         // 同时下发多个数据点需要一次性在map中放置全部需要控制的key,value值
313         // hashMap.put(key2, value2);
314         // hashMap.put(key3, value3);
315         mDevice.write(hashMap, sn);
316         Log.i("liang", "下发命令:" + hashMap.toString());
317     }
318 
319     private boolean isDeviceCanBeControlled() {
320         return mDevice.getNetStatus() == GizWifiDeviceNetStatus.GizDeviceControlled;
321     }
322 
323     private void toastDeviceNoReadyAndExit() {
324         Toast.makeText(this, "设备无响应,请检查设备是否正常工作", Toast.LENGTH_SHORT).show();
325         finish();
326     }
327 
328     private void toastDeviceDisconnectAndExit() {
329         Toast.makeText(GosDeviceControlActivity.this, "连接已断开", Toast.LENGTH_SHORT).show();
330         finish();
331     }
332 
333     /**
334      * 展示设备硬件信息
335      * 
336      * @param hardwareInfo
337      */
338     private void showHardwareInfo(String hardwareInfo) {
339         String hardwareInfoTitle = "设备硬件信息";
340         new AlertDialog.Builder(this).setTitle(hardwareInfoTitle).setMessage(hardwareInfo)
341                 .setPositiveButton(R.string.besure, null).show();
342     }
343 
344     /**
345      * Description:设置设备别名与备注
346      */
347     private void setDeviceInfo() {
348 
349         final Dialog mDialog = new AlertDialog.Builder(this).setView(new EditText(this)).create();
350         mDialog.show();
351 
352         Window window = mDialog.getWindow();
353         window.setContentView(R.layout.alert_gos_set_device_info);
354 
355         final EditText etAlias;
356         final EditText etRemark;
357         etAlias = (EditText) window.findViewById(R.id.etAlias);
358         etRemark = (EditText) window.findViewById(R.id.etRemark);
359 
360         LinearLayout llNo, llSure;
361         llNo = (LinearLayout) window.findViewById(R.id.llNo);
362         llSure = (LinearLayout) window.findViewById(R.id.llSure);
363 
364         if (!TextUtils.isEmpty(mDevice.getAlias())) {
365             setEditText(etAlias, mDevice.getAlias());
366         }
367         if (!TextUtils.isEmpty(mDevice.getRemark())) {
368             setEditText(etRemark, mDevice.getRemark());
369         }
370 
371         llNo.setOnClickListener(new OnClickListener() {
372 
373             @Override
374             public void onClick(View v) {
375                 mDialog.dismiss();
376             }
377         });
378 
379         llSure.setOnClickListener(new OnClickListener() {
380 
381             @Override
382             public void onClick(View v) {
383                 if (TextUtils.isEmpty(etRemark.getText().toString())
384                         && TextUtils.isEmpty(etAlias.getText().toString())) {
385                     myToast("请输入设备别名或备注!");
386                     return;
387                 }
388                 mDevice.setCustomInfo(etRemark.getText().toString(), etAlias.getText().toString());
389                 mDialog.dismiss();
390                 String loadingText = (String) getText(R.string.loadingtext);
391                 progressDialog.setMessage(loadingText);
392                 progressDialog.show();
393             }
394         });
395 
396         mDialog.setOnDismissListener(new OnDismissListener() {
397             @Override
398             public void onDismiss(DialogInterface dialog) {
399                 hideKeyBoard();
400             }
401         });
402     }
403     
404     /*
405      * 获取设备硬件信息回调
406      */
407     @Override
408     protected void didGetHardwareInfo(GizWifiErrorCode result, GizWifiDevice device,
409             ConcurrentHashMap<String, String> hardwareInfo) {
410         super.didGetHardwareInfo(result, device, hardwareInfo);
411         StringBuffer sb = new StringBuffer();
412         if (GizWifiErrorCode.GIZ_SDK_SUCCESS != result) {
413             myToast("获取设备硬件信息失败:" + result.name());
414         } else {
415             sb.append("Wifi Hardware Version:" + hardwareInfo.get(WIFI_HARDVER_KEY) + "\r\n");
416             sb.append("Wifi Software Version:" + hardwareInfo.get(WIFI_SOFTVER_KEY) + "\r\n");
417             sb.append("MCU Hardware Version:" + hardwareInfo.get(MCU_HARDVER_KEY) + "\r\n");
418             sb.append("MCU Software Version:" + hardwareInfo.get(MCU_SOFTVER_KEY) + "\r\n");
419             sb.append("Wifi Firmware Id:" + hardwareInfo.get(WIFI_FIRMWAREID_KEY) + "\r\n");
420             sb.append("Wifi Firmware Version:" + hardwareInfo.get(WIFI_FIRMWAREVER_KEY) + "\r\n");
421             sb.append("Product Key:" + "\r\n" + hardwareInfo.get(PRODUCT_KEY) + "\r\n");
422 
423             // 设备属性
424             sb.append("Device ID:" + "\r\n" + mDevice.getDid() + "\r\n");
425             sb.append("Device IP:" + mDevice.getIPAddress() + "\r\n");
426             sb.append("Device MAC:" + mDevice.getMacAddress() + "\r\n");
427         }
428         showHardwareInfo(sb.toString());
429     }
430 
431     /*
432      * 设置设备别名和备注回调
433      */
434     @Override
435     protected void didSetCustomInfo(GizWifiErrorCode result, GizWifiDevice device) {
436         super.didSetCustomInfo(result, device);
437         if (GizWifiErrorCode.GIZ_SDK_SUCCESS == result) {
438             myToast("设置成功");
439             progressDialog.cancel();
440             finish();
441         } else {
442             myToast("设置失败:" + result.name());
443         }
444     }
445 
446     /*
447      * 设备状态改变回调,只有设备状态为可控才可以下发控制命令
448      */
449     @Override
450     protected void didUpdateNetStatus(GizWifiDevice device, GizWifiDeviceNetStatus netStatus) {
451         super.didUpdateNetStatus(device, netStatus);
452         if (netStatus == GizWifiDeviceNetStatus.GizDeviceControlled) {
453             mHandler.removeCallbacks(mRunnable);
454             progressDialog.cancel();
455         } else {
456             mHandler.sendEmptyMessage(handler_key.DISCONNECT.ordinal());
457         }
458     }
459     
460     /*
461      * 设备上报数据回调,此回调包括设备主动上报数据、下发控制命令成功后设备返回ACK
462      */
463     @Override
464     protected void didReceiveData(GizWifiErrorCode result, GizWifiDevice device,
465             ConcurrentHashMap<String, Object> dataMap, int sn) {
466         super.didReceiveData(result, device, dataMap, sn);
467         Log.i("liang", "接收到数据");
468         if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS && dataMap.get("data") != null) {
469             getDataFromReceiveDataMap(dataMap);
470             mHandler.sendEmptyMessage(handler_key.UPDATE_UI.ordinal());
471         }
472     }
473 
474 }

 

以上是关于安卓初次完美调试,并成功编程!的主要内容,如果未能解决你的问题,请参考以下文章

OPPO K3在哪里打开USB调试模式的完美方法

OPPO R17在哪里打开usb调试模式的完美流程

OPPO F1s在哪里打开Usb调试模式的完美方法

OPPO R11s Plus在哪里开启usb调试模式的完美教程

魅族15完美开启usb调试模式的经验

运行/调试你的PHP代码