1. 程式人生 > >安卓初次完美調試,並成功編程!

安卓初次完美調試,並成功編程!

顯示 gettext options miss listener ptr chan 控制 列表

語言都是相通的,爽歪歪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 }

安卓初次完美調試,並成功編程!