PageRenderTime 59ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/guanxin-guanxin/src/main/java/com/shunzhi/guanxin/activity/RecFeedbackActivity.java

https://gitlab.com/call_me_xyz/HelthNut
Java | 666 lines | 575 code | 65 blank | 26 comment | 90 complexity | a8a2d1b53450f7026f06b09fe631f70e MD5 | raw file
  1. package com.shunzhi.guanxin.activity;
  2. import android.app.Activity;
  3. import android.app.ProgressDialog;
  4. import android.bluetooth.BluetoothAdapter;
  5. import android.bluetooth.BluetoothDevice;
  6. import android.bluetooth.BluetoothManager;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.content.SharedPreferences;
  10. import android.content.SharedPreferences.Editor;
  11. import android.os.Bundle;
  12. import android.os.Handler;
  13. import android.os.Message;
  14. import android.text.TextUtils;
  15. import android.view.View;
  16. import android.view.View.OnClickListener;
  17. import android.widget.ArrayAdapter;
  18. import android.widget.Button;
  19. import android.widget.EditText;
  20. import android.widget.LinearLayout;
  21. import android.widget.TextView;
  22. import com.alibaba.fastjson.JSON;
  23. import com.android.volley.Response;
  24. import com.android.volley.VolleyError;
  25. import com.shunzhi.guanxin.MyPreferences;
  26. import com.shunzhi.guanxin.MyVolley;
  27. import com.shunzhi.guanxin.R;
  28. import com.shunzhi.guanxin.ble.bpmonitor.BPCallBack;
  29. import com.shunzhi.guanxin.ble.bpmonitor.BPResult;
  30. import com.shunzhi.guanxin.ble.bpmonitor.BpBtService;
  31. import com.shunzhi.guanxin.entity.RecoveryFeedback;
  32. import com.shunzhi.guanxin.internet.GsonUtil;
  33. import com.shunzhi.guanxin.internet.MyStringRequest;
  34. import com.shunzhi.guanxin.internet.PostParams;
  35. import com.shunzhi.guanxin.utils.T;
  36. import com.shunzhi.guanxin.utils.TimeUtils;
  37. import com.shunzhi.guanxin.utils.UserUtils;
  38. import com.shunzhi.guanxin.views.MyToolBar;
  39. import com.shunzhi.guanxin.views.MyToolBar.BackClickedListener;
  40. import com.shunzhi.guanxin.views.MyToolBar.RightClickedListener;
  41. import com.shunzhi.guanxin.views.ProgressWithText;
  42. import com.shunzhi.guanxin.views.ProgressWithText.OnReConnectListener;
  43. import com.shunzhi.guanxin.views.RecFeedbackStatuRadio;
  44. import com.shunzhi.guanxin.views.SportFeedbackEmojiViews;
  45. import java.sql.Timestamp;
  46. import java.util.ArrayList;
  47. import java.util.List;
  48. public class RecFeedbackActivity extends BaseActivity implements OnClickListener, BackClickedListener, OnReConnectListener, BPCallBack {
  49. public static final String URL_GET_REC_FEEDBACK = "getRecFeedback.action";
  50. public static final String URL_GET_REC_FEEDBACK_BYID = "getRecFeedbackById.action";
  51. public static final String URL_ADD_REC_FEEDBACK = "addRecFeedback.action";
  52. public static final String EXTRA_FEEDBACK_ID = "feedbackId";
  53. // Stops scanning after 10 seconds.
  54. private static final long SCAN_PERIOD = 10000;
  55. private final int REQUEST_ENABLE_BT = 1;
  56. ProgressDialog mProgressDialog;
  57. List<String> strs = new ArrayList<String>();
  58. ArrayAdapter<String> adapter;
  59. // tag for if has submit a new Recfeedback or this week's Recfeedback
  60. // already submit
  61. private boolean ifSubmit;
  62. // tag for if this activity is start from RecFeedbackActivity
  63. private boolean ifFromList;
  64. private RecoveryFeedback mFeedback;
  65. private MyToolBar myToolBar;
  66. private ProgressWithText mProgressWithText;
  67. private EditText mBpHET;
  68. private EditText mBpLET;
  69. private EditText mBgBeforeEatET;
  70. private EditText mBgAfterEatET;
  71. private EditText mTcET;
  72. private EditText mTgET;
  73. private EditText mChdlET;
  74. private EditText mCldlET;
  75. private EditText mWeightET;
  76. private RecFeedbackStatuRadio mRecStatus;
  77. private EditText mSymptomsET;
  78. private EditText mSelfJudgeET;
  79. private SportFeedbackEmojiViews mScore;
  80. private TextView mBpHint1TV;
  81. private TextView mBpHint2TV;
  82. private Button mMeaureBtn;
  83. private LinearLayout mSubmitLL;
  84. private Button mSubmitBtn;
  85. private TextView mTimeTV;
  86. private ProgressDialog mMeasureDialog;
  87. // bp
  88. private boolean ifFirst = false;
  89. private BluetoothAdapter mBluetoothAdapter;
  90. private BpBtService mService;
  91. private Handler mHandler;
  92. private boolean mScanning;
  93. private Handler mBTHandler;
  94. // Device scan callback.
  95. private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
  96. @Override
  97. public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
  98. runOnUiThread(new Runnable() {
  99. @SuppressWarnings("deprecation")
  100. @Override
  101. public void run() {
  102. if (device.getName() != null && device.getName().startsWith("Bluetooth BP")) {
  103. mService.connect(device);
  104. mBluetoothAdapter.stopLeScan(mLeScanCallback);
  105. }
  106. }
  107. });
  108. }
  109. };
  110. @Override
  111. protected void onCreate(Bundle savedInstanceState) {
  112. // TODO Auto-generated method stub
  113. super.onCreate(savedInstanceState);
  114. setContentView(R.layout.acitivity_recovery_feedback);
  115. int id = getIntent().getIntExtra(EXTRA_FEEDBACK_ID, -1);
  116. if (id > 0)
  117. ifFromList = true;
  118. initToolBar(ifFromList);
  119. initViews();
  120. if (-1 == id) {
  121. getFeedback();
  122. } else
  123. getFeedbackById(id);
  124. }
  125. private void initToolBar(boolean ifFromList) {
  126. myToolBar = (MyToolBar) findViewById(R.id.my_toolbar);
  127. myToolBar.setTitle(getString(R.string.recovery_feedback_title));
  128. myToolBar.setBackClickedListener(this);
  129. if (!ifFromList) {
  130. myToolBar.setRightOptionText(R.string.histroy_feedback);
  131. myToolBar.showRight();
  132. myToolBar.setRightClickedListener(new RightClickedListener() {
  133. @Override
  134. public void onRightClick() {
  135. // TODO Auto-generated method stub
  136. startActivity(new Intent(RecFeedbackActivity.this, RecFeedbackListActivity.class));
  137. }
  138. });
  139. }
  140. }
  141. private void initViews() {
  142. mProgressWithText = (ProgressWithText) findViewById(R.id.progress_with_text);
  143. mBpHET = (EditText) findViewById(R.id.high_bp);
  144. mBpLET = (EditText) findViewById(R.id.low_bp);
  145. mBgBeforeEatET = (EditText) findViewById(R.id.glucose_before_eat);
  146. mBgAfterEatET = (EditText) findViewById(R.id.glucose_after_eat);
  147. mTcET = (EditText) findViewById(R.id.tc);
  148. mTgET = (EditText) findViewById(R.id.tg);
  149. mChdlET = (EditText) findViewById(R.id.chdl);
  150. mCldlET = (EditText) findViewById(R.id.cldl);
  151. mWeightET = (EditText) findViewById(R.id.weight);
  152. mRecStatus = (RecFeedbackStatuRadio) findViewById(R.id.rec_status);
  153. mSymptomsET = (EditText) findViewById(R.id.other_symptom);
  154. mScore = (SportFeedbackEmojiViews) findViewById(R.id.self_score);
  155. int[] texts = new int[]{R.string.feedback_score1, R.string.feedback_score2, R.string.feedback_score3};
  156. mScore.setTexts(texts);
  157. mSelfJudgeET = (EditText) findViewById(R.id.other_self_feedback);
  158. mBpHint1TV = (TextView) findViewById(R.id.bp_hint1);
  159. mBpHint2TV = (TextView) findViewById(R.id.bp_hint2);
  160. mMeaureBtn = (Button) findViewById(R.id.bp_btn);
  161. mSubmitBtn = (Button) findViewById(R.id.submit);
  162. mSubmitLL = (LinearLayout) findViewById(R.id.submit_ll);
  163. mTimeTV = (TextView) findViewById(R.id.time);
  164. mProgressWithText.setReConnectListener(this);
  165. mMeaureBtn.setOnClickListener(this);
  166. mSubmitBtn.setOnClickListener(this);
  167. }
  168. @Override
  169. public void onReConnectListenr() {
  170. mProgressWithText.start();
  171. getFeedback();
  172. }
  173. private boolean checkValue(Float value, Timestamp validity) {
  174. return null != value && 0 != value && null != validity && validity.getTime() > System.currentTimeMillis();
  175. }
  176. private void setViews(RecoveryFeedback recFeedback) {
  177. if (checkValue(recFeedback.getTc(), recFeedback.getTcValidity()))
  178. mTcET.setText(recFeedback.getTc() + "");
  179. if (checkValue(recFeedback.getTg(), recFeedback.getTgValidity()))
  180. mTgET.setText(recFeedback.getTg() + "");
  181. if (checkValue(recFeedback.getChdl(), recFeedback.getChdlValidity()))
  182. mChdlET.setText(recFeedback.getChdl() + "");
  183. if (checkValue(recFeedback.getCldl(), recFeedback.getChdlValidity()))
  184. mCldlET.setText(recFeedback.getCldl() + "");
  185. if (checkValue(recFeedback.getWeight(), recFeedback.getWeightValidity()))
  186. mWeightET.setText(recFeedback.getWeight() + "");
  187. if (recFeedback.getDeflag()) {
  188. mTimeTV.setVisibility(View.GONE);
  189. return;
  190. } else
  191. mTimeTV.setVisibility(View.VISIBLE);
  192. ifSubmit = true;
  193. mBpHET.setText(recFeedback.getBpH() + "");
  194. mBpLET.setText(recFeedback.getBpL() + "");
  195. mBgBeforeEatET.setText(recFeedback.getBgBeforeEat() + "");
  196. mBgAfterEatET.setText(recFeedback.getBgAfterEat() + "");
  197. mRecStatus.setType(recFeedback.getStatus().shortValue());
  198. if (null == recFeedback.getSymptom() || 0 == recFeedback.getSymptom().trim().length())
  199. mSymptomsET.setVisibility(View.GONE);
  200. else
  201. mSymptomsET.setText(recFeedback.getSymptom());
  202. if (null == recFeedback.getComplement() || 0 == recFeedback.getComplement().trim().length())
  203. mSelfJudgeET.setVisibility(View.GONE);
  204. else
  205. mSelfJudgeET.setText(recFeedback.getComplement());
  206. mBpHint1TV.setVisibility(View.GONE);
  207. mBpHint2TV.setVisibility(View.GONE);
  208. mSubmitLL.setVisibility(View.GONE);
  209. mMeaureBtn.setVisibility(View.GONE);
  210. mBpHET.setFocusable(false);
  211. mBpLET.setFocusable(false);
  212. mBgBeforeEatET.setFocusable(false);
  213. mBgAfterEatET.setFocusable(false);
  214. mTcET.setFocusable(false);
  215. mTgET.setFocusable(false);
  216. mChdlET.setFocusable(false);
  217. mCldlET.setFocusable(false);
  218. mWeightET.setFocusable(false);
  219. mRecStatus.disableClick();
  220. mSymptomsET.setFocusable(false);
  221. mSelfJudgeET.setFocusable(false);
  222. mScore.disableClick();
  223. mSymptomsET.setFocusable(false);
  224. mSelfJudgeET.setFocusable(false);
  225. mTimeTV.setText("填写日期:" + TimeUtils.timestampToYMDString(recFeedback.getAddTime()));
  226. SharedPreferences preferences = getSharedPreferences(MyPreferences.GLOABAL_VARIBLES_PREFERENCES, Context.MODE_PRIVATE);
  227. Long time = preferences.getLong(MyPreferences.LAST_RECOVERY_FEEDBACk_TIME, 0);
  228. // 0 == time is for patient user,first login ,the time is 0
  229. // IfSameWeek is for relative user
  230. if (0 == time || !TimeUtils.ifSameWeek(time, recFeedback.getAddTime().getTime())) {
  231. Editor editor = preferences.edit();
  232. editor.putLong(MyPreferences.LAST_RECOVERY_REPORT_TIME, recFeedback.getAddTime().getTime());
  233. editor.commit();
  234. ifSubmit = true;
  235. }
  236. }
  237. private void getFeedback() {
  238. PostParams params = new PostParams();
  239. params.putParams("patientId", UserUtils.getPatientId(RecFeedbackActivity.this) + "");
  240. MyStringRequest request = new MyStringRequest(URL_GET_REC_FEEDBACK, new Response.Listener<String>() {
  241. @Override
  242. public void onResponse(String response) {
  243. RecoveryFeedback result = GsonUtil.getRecFeedback(response);
  244. if (null != result) {
  245. mFeedback = result;
  246. setViews(result);
  247. mProgressWithText.setVisibility(View.GONE);
  248. } else
  249. getFeedbackError();
  250. }
  251. }, new Response.ErrorListener() {
  252. @Override
  253. public void onErrorResponse(VolleyError error) {
  254. // TODO Auto-generated method stub
  255. getFeedbackError();
  256. }
  257. }, params);
  258. MyVolley.getInstance(RecFeedbackActivity.this).addToRequestQueue(request);
  259. }
  260. private void getFeedbackById(int id) {
  261. PostParams params = new PostParams();
  262. params.putParams("patientId", UserUtils.getPatientId(RecFeedbackActivity.this) + "");
  263. params.putParams("fId", id + "");
  264. MyStringRequest request = new MyStringRequest(URL_GET_REC_FEEDBACK_BYID, new Response.Listener<String>() {
  265. @Override
  266. public void onResponse(String response) {
  267. RecoveryFeedback result = GsonUtil.getRecFeedback(response);
  268. if (null != result) {
  269. mFeedback = result;
  270. setViews(result);
  271. mProgressWithText.setVisibility(View.GONE);
  272. } else
  273. getFeedbackError();
  274. }
  275. }, new Response.ErrorListener() {
  276. @Override
  277. public void onErrorResponse(VolleyError error) {
  278. // TODO Auto-generated method stub
  279. getFeedbackError();
  280. }
  281. }, params);
  282. MyVolley.getInstance(RecFeedbackActivity.this).addToRequestQueue(request);
  283. }
  284. private void getFeedbackError() {
  285. mProgressWithText.setFailed();
  286. }
  287. private void startSubmit() {
  288. if (TextUtils.isEmpty(mBpHET.getText().toString())) {
  289. mBpHET.requestFocus();
  290. feedbackNotComplete();
  291. return;
  292. }
  293. if (TextUtils.isEmpty(mBpHET.getText().toString())) {
  294. mBpHET.requestFocus();
  295. feedbackNotComplete();
  296. return;
  297. }
  298. if (TextUtils.isEmpty(mBpLET.getText().toString())) {
  299. mBpLET.requestFocus();
  300. feedbackNotComplete();
  301. return;
  302. }
  303. if (TextUtils.isEmpty(mBgBeforeEatET.getText().toString())) {
  304. mBgBeforeEatET.requestFocus();
  305. feedbackNotComplete();
  306. return;
  307. }
  308. if (TextUtils.isEmpty(mBgAfterEatET.getText().toString())) {
  309. mBgAfterEatET.requestFocus();
  310. feedbackNotComplete();
  311. return;
  312. }
  313. if (TextUtils.isEmpty(mTgET.getText().toString())) {
  314. mTgET.requestFocus();
  315. feedbackNotComplete();
  316. return;
  317. }
  318. if (TextUtils.isEmpty(mTcET.getText().toString())) {
  319. mTcET.requestFocus();
  320. feedbackNotComplete();
  321. return;
  322. }
  323. if (TextUtils.isEmpty(mChdlET.getText().toString())) {
  324. mChdlET.requestFocus();
  325. feedbackNotComplete();
  326. return;
  327. }
  328. if (TextUtils.isEmpty(mCldlET.getText().toString())) {
  329. mCldlET.requestFocus();
  330. feedbackNotComplete();
  331. return;
  332. }
  333. if (TextUtils.isEmpty(mWeightET.getText().toString())) {
  334. mWeightET.requestFocus();
  335. feedbackNotComplete();
  336. return;
  337. }
  338. addFeedback();
  339. }
  340. private void feedbackNotComplete() {
  341. T.showShort(RecFeedbackActivity.this, R.string.rec_feedback_not_complete);
  342. }
  343. private Timestamp getValidity() {
  344. long time = System.currentTimeMillis() + 30 * 24 * 3600 * 1000L;
  345. return new Timestamp(time);
  346. }
  347. private boolean ifNullFeedback() {
  348. return null == mFeedback || mFeedback.getDeflag();
  349. }
  350. private void addFeedback() {
  351. mSubmitBtn.setEnabled(false);
  352. if (null == mProgressDialog) {
  353. mProgressDialog = new ProgressDialog(RecFeedbackActivity.this);
  354. mProgressDialog.setTitle(null);
  355. mProgressDialog.setMessage(getResources().getString(R.string.rec_feedback_sumitting));
  356. mProgressDialog.setCancelable(false);
  357. }
  358. mProgressDialog.show();
  359. final RecoveryFeedback feedback = new RecoveryFeedback();
  360. feedback.setPatientId(UserUtils.getPatientId(getApplicationContext()));
  361. feedback.setBpH(Short.parseShort(mBpHET.getText().toString()));
  362. feedback.setBpL(Short.parseShort(mBpLET.getText().toString()));
  363. feedback.setBgAfterEat(Float.parseFloat(mBgAfterEatET.getText().toString()));
  364. feedback.setBgBeforeEat(Float.parseFloat(mBgBeforeEatET.getText().toString()));
  365. Float tc = Float.parseFloat(mTcET.getText().toString());
  366. if (ifNullFeedback() || !mFeedback.getTc().equals(tc)) {
  367. feedback.setTc(tc);
  368. mFeedback.setTc(tc);
  369. feedback.setTcValidity(getValidity());
  370. mFeedback.setTcValidity(getValidity());
  371. } else {
  372. feedback.setTc(mFeedback.getTc());
  373. feedback.setTcValidity(mFeedback.getTcValidity());
  374. }
  375. Float tg = Float.parseFloat(mTgET.getText().toString());
  376. if (ifNullFeedback() || mFeedback.getTg().equals(tg)) {
  377. feedback.setTg(tg);
  378. mFeedback.setTg(tg);
  379. feedback.setTgValidity(getValidity());
  380. mFeedback.setTgValidity(getValidity());
  381. } else {
  382. feedback.setTg(mFeedback.getTg());
  383. feedback.setTgValidity(mFeedback.getTgValidity());
  384. }
  385. Float chdl = Float.parseFloat(mChdlET.getText().toString());
  386. if (ifNullFeedback() || !mFeedback.getChdl().equals(chdl)) {
  387. feedback.setChdl(chdl);
  388. mFeedback.setChdl(chdl);
  389. feedback.setChdlValidity(getValidity());
  390. mFeedback.setChdlValidity(getValidity());
  391. } else {
  392. feedback.setChdl(mFeedback.getChdl());
  393. feedback.setChdlValidity(mFeedback.getChdlValidity());
  394. }
  395. Float cldl = Float.parseFloat(mCldlET.getText().toString());
  396. if (ifNullFeedback() || !mFeedback.getChdl().equals(cldl)) {
  397. feedback.setCldl(cldl);
  398. mFeedback.setCldl(cldl);
  399. feedback.setCldlValidity(getValidity());
  400. mFeedback.setCldlValidity(getValidity());
  401. } else {
  402. feedback.setCldl(mFeedback.getCldl());
  403. feedback.setCldlValidity(mFeedback.getCldlValidity());
  404. }
  405. Float weight = Float.parseFloat(mWeightET.getText().toString());
  406. if (ifNullFeedback() || !mFeedback.getWeight().equals(weight)) {
  407. feedback.setWeight(weight);
  408. mFeedback.setWeight(weight);
  409. feedback.setWeightValidity(getValidity());
  410. mFeedback.setWeightValidity(getValidity());
  411. } else {
  412. feedback.setWeight(mFeedback.getWeight());
  413. feedback.setWeightValidity(mFeedback.getWeightValidity());
  414. }
  415. // feedback.setTg(Float.parseFloat(mTgET.getText().toString()));
  416. // feedback.setChdl(Float.parseFloat(mChdlET.getText().toString()));
  417. // feedback.setCldl(Float.parseFloat(mCldlET.getText().toString()));
  418. // feedback.setWeight(Float.parseFloat(mWeightET.getText().toString()));
  419. feedback.setStatus(mRecStatus.getType());
  420. if (!TextUtils.isEmpty(mSymptomsET.getText().toString()))
  421. feedback.setSymptom(mSymptomsET.getText().toString());
  422. feedback.setFeeling(mScore.getFeedBack());
  423. if (!TextUtils.isEmpty(mSelfJudgeET.getText().toString()))
  424. feedback.setComplement(mSelfJudgeET.getText().toString());
  425. PostParams params = new PostParams();
  426. params.putParams("recFeedbackStr", JSON.toJSONString(feedback));
  427. MyStringRequest request = new MyStringRequest(URL_ADD_REC_FEEDBACK, new Response.Listener<String>() {
  428. @Override
  429. public void onResponse(String response) {
  430. if (null != response) {
  431. {feedback.setAddTime(new Timestamp(System.currentTimeMillis()));
  432. setViews(feedback);
  433. addFeedbackSuccess();
  434. }
  435. } else
  436. addFeedbackError();
  437. }
  438. }, new Response.ErrorListener() {
  439. @Override
  440. public void onErrorResponse(VolleyError error) {
  441. // TODO Auto-generated method stub
  442. addFeedbackError();
  443. }
  444. }, params);
  445. MyVolley.getInstance(getApplicationContext()).addToRequestQueue(request);
  446. }
  447. private void addFeedbackSuccess() {
  448. SharedPreferences varPreferences = getSharedPreferences(MyPreferences.GLOABAL_VARIBLES_PREFERENCES, Context.MODE_PRIVATE);
  449. SharedPreferences.Editor editor = varPreferences.edit();
  450. editor.putLong(MyPreferences.LAST_RECOVERY_FEEDBACk_TIME, System.currentTimeMillis());
  451. editor.commit();
  452. ifSubmit = true;
  453. mProgressDialog.dismiss();
  454. T.showShort(getApplicationContext(), R.string.rec_feedback_add_success);
  455. }
  456. private void addFeedbackError() {
  457. mSubmitBtn.setEnabled(true);
  458. mProgressDialog.dismiss();
  459. }
  460. @Override
  461. public void onBackClick() {
  462. // TODO Auto-generated method stub
  463. this.onBackPressed();
  464. }
  465. @Override
  466. public void onBackPressed() {
  467. // only when this is start from RecoveryPatientInfoFragment and a new
  468. // RecFeedback has been submitted then setResult
  469. if (!ifFromList && ifSubmit)
  470. setResult(Activity.RESULT_OK);
  471. super.onBackPressed();
  472. }
  473. @Override
  474. public void onClick(View v) {
  475. switch (v.getId()) {
  476. case R.id.submit:
  477. startSubmit();
  478. break;
  479. case R.id.bp_btn:
  480. measureStart();
  481. break;
  482. default:
  483. break;
  484. }
  485. }
  486. private void measureStart() {
  487. mMeaureBtn.setEnabled(false);
  488. initHandler();
  489. initBT();
  490. initMeasureDialog();
  491. }
  492. private void startMeasure() {
  493. mService.write(new byte[]{-3, -3, -6, 5, 13, 10});
  494. }
  495. private void endMeasure() {
  496. mService.write(new byte[]{-3, -3, -2, 6, 13, 10});
  497. }
  498. private void initMeasureDialog() {
  499. mMeasureDialog = new ProgressDialog(this);
  500. mMeasureDialog.setMessage("请确保设备已经打开");
  501. }
  502. private boolean checkMeasureDialog() {
  503. return null != mMeasureDialog && mMeasureDialog.isShowing();
  504. }
  505. private void initBT() {
  506. mBTHandler = new Handler();
  507. BluetoothManager bm = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
  508. mBluetoothAdapter = bm.getAdapter();
  509. if (null == mBluetoothAdapter || !mBluetoothAdapter.isEnabled()) {
  510. Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  511. startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  512. } else {
  513. scanLeDevice(true);
  514. }
  515. }
  516. private void initHandler() {
  517. this.mHandler = new Handler() {
  518. public void handleMessage(Message msg) {
  519. if (1 == msg.what) {
  520. BPResult result = (BPResult) msg.getData().getParcelable("result");
  521. if (null != result) {
  522. switch (result.getType()) {
  523. case BPResult.TYPE_MEASURING:
  524. if (checkMeasureDialog())
  525. mMeasureDialog.setMessage("正在测量");
  526. break;
  527. case BPResult.TYPE_ERROR:
  528. if (checkMeasureDialog())
  529. mMeasureDialog.setMessage("设备错误");
  530. break;
  531. case BPResult.TYPE_MEASURE_ERROR:
  532. if (checkMeasureDialog())
  533. mMeasureDialog.setMessage("测量错误");
  534. break;
  535. case BPResult.TYPE_POWER_LOW:
  536. if (checkMeasureDialog())
  537. mMeasureDialog.setMessage("电量过低");
  538. break;
  539. case BPResult.TYPE_DEVICE_SHUTTING:
  540. if (checkMeasureDialog())
  541. mMeasureDialog.setMessage("正在关机");
  542. break;
  543. case BPResult.TYPE_MEASURING_DATA:
  544. if (checkMeasureDialog())
  545. mMeasureDialog.setMessage("正在测量:" + result.getPressure());
  546. break;
  547. case BPResult.TYPE_RESULT_DATA:
  548. if (checkMeasureDialog())
  549. mMeasureDialog.setMessage("测量结果: 高血压" + result.getHbp() + " 低血压:" + result.getLbp() + " 心率: " + result.getHeartRate());
  550. break;
  551. default:
  552. break;
  553. }
  554. }
  555. }
  556. }
  557. };
  558. }
  559. @SuppressWarnings("deprecation")
  560. private void scanLeDevice(final boolean enable) {
  561. if (enable) {
  562. // Stops scanning after a pre-defined scan period.
  563. mBTHandler.postDelayed(new Runnable() {
  564. @Override
  565. public void run() {
  566. mScanning = false;
  567. mBluetoothAdapter.stopLeScan(mLeScanCallback);
  568. }
  569. }, SCAN_PERIOD);
  570. mScanning = true;
  571. mBluetoothAdapter.startLeScan(mLeScanCallback);
  572. } else {
  573. mScanning = false;
  574. mBluetoothAdapter.stopLeScan(mLeScanCallback);
  575. }
  576. }
  577. @Override
  578. public void connected() {
  579. startMeasure();
  580. }
  581. @Override
  582. public void connecting() {
  583. // TODO Auto-generated method stub
  584. }
  585. @Override
  586. public void disconnected() {
  587. // TODO Auto-generated method stub
  588. }
  589. @Override
  590. public void connectFailed() {
  591. // TODO Auto-generated method stub
  592. }
  593. @Override
  594. public void connectLost() {
  595. // TODO Auto-generated method stub
  596. }
  597. @Override
  598. public void stopConnection() {
  599. // TODO Auto-generated method stub
  600. }
  601. }