当前位置:   article > 正文

Android读取SIM卡ICCID_android iccid

android iccid

方法一,使用TelephonyManager的getSimSerialNumber()获取

  1. TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(TELEPHONY_SERVICE);
  2. String iccid = telephonyManager.getSimSerialNumber()

需要添加权限:<uses-permission android:name="android.permission.READ_PHONE_STATE" />

方法二,通过发AT命令读取SIM卡ICCID(可以解决读取某些SIM卡ICCID长度不对的问题)

  1. new Thread(new Runnable() {
  2. @Override
  3. public void run() {
  4. String com = "/dev/ttyUSB2";
  5. String cmd = "at+qccid\r\n";
  6. if(adbcommand("chmod 0777 "+com) && sendAtCmd(com,cmd)){
  7. iccidtv.setText("ICCID:"+recAtresult(com));
  8. }
  9. iccidtv.setText("ICCID:"+recAtresult(com));
  10. //iccidtv.setText("ICCID:"+getSimSerialNumber(getApplicationContext()));
  11. }
  12. }).start();
  13. public boolean adbcommand(String command) {
  14. Process process = null;
  15. DataOutputStream os = null;
  16. //String excresult = "";
  17. try {
  18. process = Runtime.getRuntime().exec("su");
  19. os = new DataOutputStream(process.getOutputStream());
  20. os.writeBytes(command + "\n");
  21. os.writeBytes("exit\n");
  22. os.flush();
  23. if (process.waitFor() != 0) {
  24. // System.err.println("exit value = " + process.exitValue());
  25. }
  26. BufferedReader in = new BufferedReader(new InputStreamReader(
  27. process.getInputStream()));
  28. StringBuffer stringBuffer = new StringBuffer();
  29. String line = null;
  30. while ((line = in.readLine()) != null) {
  31. stringBuffer.append(line + " ");
  32. }
  33. //excresult = stringBuffer.toString();
  34. // System.out.println(excresult);
  35. return true;
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. return false;
  39. }
  40. //return excresult;
  41. }
  42. public static boolean sendAtCmd(String fileName, String atcmd) {
  43. boolean atResult = true;// at指令的返回结果
  44. Log.i(TAG, "fileName = " + fileName + ", atcmd = " + atcmd);
  45. try {
  46. BufferedWriter atWriter = new BufferedWriter(new FileWriter(
  47. fileName));
  48. atWriter.write(atcmd, 0, atcmd.getBytes().length);
  49. atWriter.flush();
  50. atWriter.close();
  51. atWriter = null;
  52. // 以上是往fileName该通道写入AT指令
  53. } catch (FileNotFoundException e2) {
  54. Log.e(TAG, "FileNotFoundException :" + fileName);
  55. atResult = false;
  56. } catch (IOException e) {
  57. atResult = false;
  58. e.printStackTrace();
  59. }
  60. return atResult;
  61. }
  62. public String recAtresult(String fileName) {
  63. int read_len = 0;
  64. // 下面来读取该通道里的AT指令返回结果
  65. BufferedReader atReader;
  66. String readAtString = null;
  67. char[] cReaderBuffer = new char[255];
  68. try {
  69. atReader = new BufferedReader(new FileReader(fileName));
  70. try {
  71. atReader.close();
  72. } catch (IOException e) {
  73. // TODO Auto-generated catch block
  74. e.printStackTrace();
  75. }
  76. atReader = null;
  77. try {
  78. read_len = atReader.read(cReaderBuffer, 0,
  79. cReaderBuffer.length - 1);
  80. } catch (IOException e) {
  81. // TODO Auto-generated catch block
  82. e.printStackTrace();
  83. }
  84. } catch (FileNotFoundException e) {
  85. // TODO Auto-generated catch block
  86. e.printStackTrace();
  87. }
  88. Log.i(TAG, "read_len = " + read_len);
  89. if (read_len > 0) {
  90. if (null == readAtString) {
  91. readAtString = String.copyValueOf(cReaderBuffer, 0, read_len);
  92. } else {
  93. readAtString = readAtString
  94. + String.copyValueOf(cReaderBuffer, 0, read_len);
  95. }
  96. Log.i(TAG, "readAtString = " + readAtString);
  97. }
  98. return readAtString;
  99. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/279202
推荐阅读
相关标签
  

闽ICP备14008679号