英文垃圾站wordpress外链网盘
本文接Android程序运行分析——中等复杂程度的NTAG I2C Demo为例(二)
主要针对launchDemo继续进行分析
launchDemo的源代码已经在(二)中贴过了,(二)中只分析了开头的一点点认证代码,本文主要分析launchDemo的主体
launchDemo的源代码如下
private void launchDemo(String currTab) {if(mAuthStatus == AuthStatus.Authenticated.getValue()) {demo.Auth(mPassword, AuthStatus.Protected_RW.getValue());}// ===========================================================================// LED Test// ===========================================================================if (currTab.equalsIgnoreCase("leds")) {// This demo is available even if the product is protected // as long as the SRAM is unprotected if(mAuthStatus == AuthStatus.Disabled.getValue()|| mAuthStatus == AuthStatus.Unprotected.getValue()|| mAuthStatus == AuthStatus.Authenticated.getValue()|| mAuthStatus == AuthStatus.Protected_W.getValue()|| mAuthStatus == AuthStatus.Protected_RW.getValue() ) {try {// if (LedFragment.getChosen()) {demo.LED();} catch (Exception e) {e.printStackTrace();LedFragment.setAnswer(getString(R.string.Tag_lost));}} else {Toast.makeText(getApplicationContext(), "NTAG I2C Plus memory is protected", Toast.LENGTH_LONG).show();showAuthDialog();}}// ===========================================================================// NDEF Demo// ===========================================================================if (currTab.equalsIgnoreCase("ndef")) {// This demo is only available when the tag is not protected if(mAuthStatus == AuthStatus.Disabled.getValue()|| mAuthStatus == AuthStatus.Unprotected.getValue()|| mAuthStatus == AuthStatus.Authenticated.getValue()) {try {demo.NDEF();} catch (Exception e) {// NdefFragment.setAnswer(getString(R.string.Tag_lost));}} else {Toast.makeText(getApplicationContext(), "NTAG I2C Plus memory is protected", Toast.LENGTH_LONG).show();showAuthDialog();}}// ===========================================================================// Config// ===========================================================================if (currTab.equalsIgnoreCase("config")) {}// ===========================================================================// Speedtest// ===========================================================================if (currTab.equalsIgnoreCase("ntag_rf")) {try {// SRAM Testif ((SpeedTestFragment.isSRamEnabled() == true)) {// This demo is available even if the product is protected // as long as the SRAM is unprotected if(mAuthStatus == AuthStatus.Disabled.getValue()|| mAuthStatus == AuthStatus.Unprotected.getValue()|| mAuthStatus == AuthStatus.Authenticated.getValue()|| mAuthStatus == AuthStatus.Protected_W.getValue()|| mAuthStatus == AuthStatus.Protected_RW.getValue()) {demo.SRAMSpeedtest();} else {Toast.makeText(getApplicationContext(), "NTAG I2C Plus memory is protected", Toast.LENGTH_LONG).show();showAuthDialog();}}// EEPROM Testif ((SpeedTestFragment.isSRamEnabled() == false)) {// This demo is only available when the tag is not protected if(mAuthStatus == AuthStatus.Disabled.getValue()|| mAuthStatus == AuthStatus.Unprotected.getValue()|| mAuthStatus == AuthStatus.Authenticated.getValue()) {demo.EEPROMSpeedtest();} else {Toast.makeText(getApplicationContext(), "NTAG I2C Plus memory is protected", Toast.LENGTH_LONG).show();showAuthDialog();}} // end if eeprom test} catch (Exception e) {SpeedTestFragment.setAnswer(getString(R.string.Tag_lost));e.printStackTrace();}} }
完成认证之后,也就是完成以下代码之后
看第一个Task
如果你的tab选择了这个,那么就进入if语句中,开始执行LED task
查看一下你的认证状态,如果你的认证状态可以(或者说mAuthStatus变量的值可以)那就进行LED demo的演示
如果你的认证状态不行,那就先告诉你不行,然后弹出一个Dialog让你重新选,也就是这句话
showAuthDialog
在showAuthDialogFlag为true且mAuthStatus这个变量不等于Disabled的前提下
跳转到 AuthActivity
关注一下这个函数体里的最后一个方法,StartActivityForResult,这个方法接受2个参数,第一个参数是intent,第二个参数是请求码,用于在之后的回调中判断数据的来源
简单的理解一下这个函数的作用,我在当前的Activity中调用StartActivityForResult方法,请求码只要是一个唯一的值即可
然后在第二个我们要去的Activity中,我们构建一个Intent,不过这个intent仅仅用于传递数据,我们把要传递的数据存放在intent中,然后调用setResult方法,这个方法用于向上一个Activity返回数据,setResult接受2个参数,第一个参数用于向上一个Activity返回处理结果,一般只要用RESULT_OK 或RESULT_CANCELED这两个值,第二个参数把带有数据的intent传递回去
demo.LED
private class LedTask extends AsyncTask<Void, Byte[], Void> {private final byte deviceToTag = 1;private final byte tagToDevice = 2;private final byte noTransfer = 0;private final byte invalidTransfer = 4;private Boolean exit = false;@Overrideprotected Void doInBackground(Void... params) {byte[] dataTx = new byte[reader.getSRAMSize()];byte[] dataRx = new byte[reader.getSRAMSize()];byte[] Led;Byte[][] result;// We have to make sure that the Pass-Through mode is activatedlong RegTimeOutStart = System.currentTimeMillis();boolean RTest = false;try {do {if (reader.checkPTwritePossible()) {break;}long RegTimeOut = System.currentTimeMillis();RegTimeOut = RegTimeOut - RegTimeOutStart;RTest = (RegTimeOut < 5000);} while (RTest);// Do as long as no Exception is thrownwhile (true) {// Get the color to be transmittedLed = LedFragment.getOption().getBytes();// Write the color into the block to be transmitted to the// NTAG boarddataTx[reader.getSRAMSize() - 4] = Led[0];dataTx[reader.getSRAMSize() - 3] = Led[1];// Indicate whether Temperate and LCD are enabled or notif (LedFragment.isTempEnabled()) {dataTx[reader.getSRAMSize() - 9] = 'E';} else {dataTx[reader.getSRAMSize() - 9] = 0x00;}if (LedFragment.isLCDEnabled()) {dataTx[reader.getSRAMSize() - 10] = 'E';} else {dataTx[reader.getSRAMSize() - 10] = 0x00;}// NDEF Scrolling activationif (LedFragment.isScrollEnabled()) {dataTx[reader.getSRAMSize() - 11] = 'E';} else {dataTx[reader.getSRAMSize() - 11] = 0x00;}double tempC = LedFragment.getTemperatureC();double tempF = LedFragment.getTemperatureF();if (tempC > 0.0 && tempC < 75.0) {DecimalFormat df = new DecimalFormat("00.00");byte[] tempB = df.format(tempC).getBytes();// The '.' is omitteddataTx[reader.getSRAMSize() - 24] = tempB[0];dataTx[reader.getSRAMSize() - 23] = tempB[1];dataTx[reader.getSRAMSize() - 22] = tempB[3];dataTx[reader.getSRAMSize() - 21] = tempB[4];}if (tempF > 0.0 && tempF < 120.0) {DecimalFormat df = new DecimalFormat("000.00");byte[] tempB = df.format(tempF).getBytes();// The '.' is omitteddataTx[reader.getSRAMSize() - 19] = tempB[0];dataTx[reader.getSRAMSize() - 18] = tempB[1];dataTx[reader.getSRAMSize() - 17] = tempB[2];dataTx[reader.getSRAMSize() - 16] = tempB[4];dataTx[reader.getSRAMSize() - 15] = tempB[5];}double voltD = LedFragment.getVoltage();if (voltD > 0.0 && voltD < 5.0) {DecimalFormat df = new DecimalFormat("0.0");byte[] voltB = df.format(voltD).getBytes();// The '.' is omitteddataTx[reader.getSRAMSize() - 8] = voltB[0];dataTx[reader.getSRAMSize() - 7] = voltB[2];}displayTransferDir(deviceToTag);// wait to prevent that a RF communication is// at the same time as µC I2CThread.sleep(10);reader.waitforI2Cread(DELAY_TIME);reader.writeSRAMBlock(dataTx, null);displayTransferDir(tagToDevice);// wait to prevent that a RF communication is// at the same time as µC I2CThread.sleep(10);reader.waitforI2Cwrite(100);dataRx = reader.readSRAMBlock();if (exit) {// switch off the LED on the µC before terminatingdataTx[reader.getSRAMSize() - 4] = Led[0];dataTx[reader.getSRAMSize() - 3] = '0';// wait to prevent that a RF communication is// at the same time as µC I2CThread.sleep(10);reader.waitforI2Cread(100);reader.writeSRAMBlock(dataTx, null);// wait to prevent that a RF communication is// at the same time as µC I2CThread.sleep(10);reader.waitforI2Cwrite(100);dataRx = reader.readSRAMBlock();cancel(true);return null;}// Convert byte[] to Byte[]Byte[] bytes = new Byte[dataRx.length];for (int i = 0; i < dataRx.length; i++) {bytes[i] = Byte.valueOf(dataRx[i]);}result = new Byte[2][];result[0] = new Byte[1];result[0][0] = Byte.valueOf(invalidTransfer);result[1] = bytes;// Write the result to the UI threadpublishProgress(result);}} catch (FormatException e) {displayTransferDir(noTransfer);cancel(true);e.printStackTrace();} catch (IOException e) {displayTransferDir(noTransfer);cancel(true);e.printStackTrace();} catch (CommandNotSupportedException e) {showDemoNotSupportedAlert();displayTransferDir(noTransfer);cancel(true);e.printStackTrace();} catch (Exception e) {displayTransferDir(noTransfer);cancel(true);e.printStackTrace();}return null;}private void displayTransferDir(byte dir) {Byte[][] result;result = new Byte[2][];result[0] = new Byte[1];result[0][0] = Byte.valueOf(dir);publishProgress(result);}@Overrideprotected void onProgressUpdate(Byte[]... bytes) {if (bytes[0][0] == noTransfer) {LedFragment.setTransferDir("Transfer: non");} else if (bytes[0][0] == deviceToTag) {LedFragment.setTransferDir("Transfer: Device --> Tag");} else if (bytes[0][0] == tagToDevice) {LedFragment.setTransferDir("Transfer: Device <-- Tag");} else {LedFragment.setButton(bytes[1][reader.getSRAMSize() - 2]);int temp = 0;// Adding first "Byte"temp = ((bytes[1][reader.getSRAMSize() - 5] >> 5) & 0x00000007);// Adding second Bytetemp |= ((bytes[1][reader.getSRAMSize() - 6] << 3) & 0x000007F8);// Voltageint voltage = 0;voltage = ((bytes[1][reader.getSRAMSize() - 7] << 8) & 0xff00)+ (bytes[1][reader.getSRAMSize() - 8] & 0x00ff);// if Temp is 0 no Temp sensor is on the µCif (temp != 0) {// Set the values on the screenLedFragment.setAnswer("Temperature: "+ calcTempCelsius(temp) + " µC / "+ calcTempFarenheit(temp) + " °F"+ "\nEnergy Harvesting Voltage: "+ calcVoltage(voltage));} else {LedFragment.setAnswer("Temperature: " + "Not available"+ "\nEnergy Harvesting Voltage: "+ calcVoltage(voltage));LedFragment.setTemperatureC(0);LedFragment.setTemperatureF(0);}byte version = bytes[1][reader.getSRAMSize() - 1];if (version > 0) {int uppVersion = (version >> 4) & 0x0f;int lowVersion = version & 0x0f;MainActivity.setBoardFirmwareVersion(String.valueOf(uppVersion)+ "."+ String.valueOf(lowVersion));} else {MainActivity.setBoardFirmwareVersion("1.0");}}}}