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