cd13aca65b35149b2dae667cb5f54cc18d056e16.svn-base 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package eVVM.apk.ui.first;
  2. import android.content.Intent;
  3. import android.location.Location;
  4. import android.nfc.NdefMessage;
  5. import android.nfc.NdefRecord;
  6. import android.nfc.NfcAdapter;
  7. import android.os.Parcelable;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import java.math.BigInteger;
  11. import java.util.ArrayList;
  12. import java.util.Arrays;
  13. import java.util.LinkedList;
  14. import java.util.List;
  15. import eVVM.apk.R;
  16. import eVVM.apk.helper.GPS.GPSLocationListener;
  17. import eVVM.apk.helper.GPS.GPSLocationManager;
  18. import eVVM.apk.helper.SoundPoolHelper;
  19. import eVVM.apk.ui.bean.UploadChipBean;
  20. import eVVM.apk.ui.home.BaseNfcActivity;
  21. import eVVM.apk.helper.ActivityStackManager;
  22. import eVVM.apk.helper.DoubleClickHelper;
  23. import eVVM.apk.ui.home.uploadChip.UploadChipContract;
  24. public class ReadingActivity extends BaseNfcActivity implements UploadChipContract.View {
  25. private String textRecord;
  26. private GPSLocationManager gpsLocationManager;
  27. private Location location;
  28. @Override
  29. protected int getLayoutId() {
  30. return R.layout.activity_reading;
  31. }
  32. @Override
  33. protected int getTitleId() {
  34. return R.id.reading_title;
  35. }
  36. @Override
  37. protected void initView() {
  38. soundPoolHelper = new SoundPoolHelper(4,SoundPoolHelper.TYPE_MUSIC)
  39. .setRingtoneType(SoundPoolHelper.RING_TYPE_MUSIC)
  40. .loadDefault(ReadingActivity.this)
  41. .load(ReadingActivity.this,"factory_operator_success",R.raw.factory_operator_success)
  42. .load(ReadingActivity.this,"factory_operator_error",R.raw.factory_operator_error);
  43. }
  44. @Override
  45. protected void initData() {
  46. mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
  47. gpsLocationManager = GPSLocationManager.getInstances(ReadingActivity.this);
  48. }
  49. @Override
  50. public void onNewIntent(Intent intent) {
  51. readNfcTag(intent);
  52. if(textRecord != null && textRecord.length() > 94){
  53. if(location != null){
  54. String chipNumber = textRecord.substring(6,86);
  55. // getPresenter().forDoctor(chipNumber,textRecord,location.getLongitude(),location.getLatitude());
  56. }else{
  57. toast("位置信息获取失败");
  58. }
  59. }else{
  60. toast("读取失败,请重试");
  61. }
  62. }
  63. private SoundPoolHelper soundPoolHelper;
  64. @Override
  65. public void uploadChipError(String msg) {
  66. if(msg.equals("芯片不存在")){
  67. soundPoolHelper.play("factory_operator_error",false);
  68. }
  69. toast(msg);
  70. }
  71. @Override
  72. public void uploadChipSuccess(UploadChipBean data) {
  73. soundPoolHelper.play("factory_operator_success",false);
  74. Log.e("FactoryOperatorActivity",data.getCode()+"");
  75. }
  76. /**
  77. * 读取NFC标签文本数据
  78. */
  79. private void readNfcTag(Intent intent) {
  80. if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
  81. Parcelable[] rawMsgs = intent.getParcelableArrayExtra( NfcAdapter.EXTRA_NDEF_MESSAGES);
  82. NdefMessage msgs[] = null;
  83. int contentSize = 0;
  84. if (rawMsgs != null) {
  85. msgs = new NdefMessage[rawMsgs.length];
  86. for (int i = 0; i < rawMsgs.length; i++) {
  87. msgs[i] = (NdefMessage) rawMsgs[i];
  88. contentSize += msgs[i].toByteArray().length;
  89. }
  90. }
  91. try {
  92. if (msgs != null) {
  93. NdefRecord record = msgs[0].getRecords()[0];
  94. textRecord = parseTextRecord(record);
  95. }
  96. } catch (Exception e) {
  97. }
  98. }
  99. }
  100. public static byte[] parseHexStr2Byte(String hexStr) {
  101. if (hexStr.length() < 1)
  102. return null;
  103. byte[] result = new byte[hexStr.length() / 2];
  104. for (int i = 0; i < hexStr.length() / 2; i++) {
  105. int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
  106. int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
  107. result[i] = (byte) (high * 16 + low);
  108. }
  109. return result;
  110. }
  111. /**
  112. * 解析NDEF文本数据
  113. */
  114. public String parseTextRecord(NdefRecord ndefRecord) {
  115. /**
  116. * 判断数据是否为NDEF格式
  117. */
  118. //判断TNF
  119. if (ndefRecord.getTnf() != NdefRecord.TNF_WELL_KNOWN) {
  120. return null;
  121. }
  122. //判断可变的长度的类型
  123. if (!Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
  124. return null;
  125. }
  126. try {
  127. byte[] bArray = ndefRecord.getPayload();
  128. alarmDetection(bArray);
  129. StringBuffer sb = new StringBuffer(bArray.length);
  130. String sTemp;
  131. for (int i = 0; i < bArray.length; i++) {
  132. sTemp = Integer.toHexString(0xFF & bArray[i]);
  133. if (sTemp.length() < 2)
  134. sb.append(0);
  135. sb.append(sTemp.toUpperCase());
  136. }
  137. return sb.toString();
  138. } catch (Exception e) {
  139. throw new IllegalArgumentException();
  140. }
  141. }
  142. public void alarmDetection(byte[] bytes){
  143. try{
  144. //将byte[]转为各种进制的字符串
  145. String binaryStr = new BigInteger(1, bytes).toString(2);
  146. int startIndex = (3 + 16 + 16 + 1 + 4 + 7 ) * 4 + 1 * 4 ; //减去前边非温度部分和启用后的一小时
  147. int marginRight = 2 * 4; //减去疫苗使用前的两小时
  148. String binaryArr[] = new String[(binaryStr.length() - marginRight) / 2 - startIndex];
  149. for (int i = startIndex; i < (binaryStr.length() - marginRight) / 2; i++) {
  150. binaryArr[i - startIndex] = String.valueOf(binaryStr.charAt(i * 2)) + binaryStr.charAt(i * 2 + 1);
  151. }
  152. LinkedList<Integer> exceedingIndexs = new LinkedList<Integer>();
  153. exceedingIndexs.add(-1);
  154. for (int i = 0; i < binaryArr.length; i++) { //不计算开始的一小时和最后两小时
  155. if(!binaryArr[i].equals("01")){
  156. if(i != 0 && !exceedingIndexs.get(exceedingIndexs.size() - 1).equals(i)){
  157. exceedingIndexs.add(i);
  158. }
  159. if(i != binaryArr.length - 1 && !exceedingIndexs.get(exceedingIndexs.size() - 1).equals(i + 1)){
  160. exceedingIndexs.add(i + 1);
  161. }
  162. }
  163. }
  164. // 48小时
  165. if((exceedingIndexs.size() - 1) / 2.f > 10){
  166. toast("报警");
  167. }else{
  168. toast("正常");
  169. }
  170. }catch (Exception e){
  171. e.printStackTrace();
  172. }
  173. }
  174. @Override
  175. protected void onStart() {
  176. super.onStart();
  177. //开启定位
  178. gpsLocationManager.start(new ReadingActivity.MyListener());
  179. }
  180. @Override
  181. protected void onStop() {
  182. super.onStop();
  183. //终止定位
  184. gpsLocationManager.stop();
  185. }
  186. @Override
  187. protected void onDestroy() {
  188. super.onDestroy();
  189. }
  190. class MyListener implements GPSLocationListener {
  191. @Override
  192. public void UpdateLocation(Location _location) {
  193. if (_location != null) {
  194. location = _location;
  195. }
  196. }
  197. @Override
  198. public void UpdateStatus(String provider, int status, Bundle extras) {
  199. if ("gps" == provider) {
  200. //Toast.makeText(FactoryOperatorActivity.this, "定位类型:" + provider, Toast.LENGTH_SHORT).show();
  201. }
  202. }
  203. @Override
  204. public void UpdateGPSProviderStatus(int gpsStatus) {
  205. }
  206. }
  207. //
  208. // @Override
  209. // public void onBackPressed() {
  210. // if (DoubleClickHelper.isOnDoubleClick()) {
  211. // //移动到上一个任务栈,避免侧滑引起的不良反应
  212. // moveTaskToBack(false);
  213. // postDelayed(new Runnable() {
  214. //
  215. // @Override
  216. // public void run() {
  217. // // 进行内存优化,销毁掉所有的界面
  218. // ActivityStackManager.getInstance().finishAllActivities();
  219. // // 销毁进程
  220. // System.exit(0);
  221. // }
  222. // }, 300);
  223. // } else {
  224. // toast(getResources().getString(R.string.home_exit_hint));
  225. // }
  226. // }
  227. @Override
  228. public boolean isSupportSwipeBack() {
  229. // 不使用侧滑功能
  230. return false;
  231. }
  232. }