赞
踩
方法一,使用TelephonyManager的getSimSerialNumber()获取
- TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(TELEPHONY_SERVICE);
- String iccid = telephonyManager.getSimSerialNumber()
需要添加权限:<uses-permission android:name="android.permission.READ_PHONE_STATE" />
方法二,通过发AT命令读取SIM卡ICCID(可以解决读取某些SIM卡ICCID长度不对的问题)
- new Thread(new Runnable() {
- @Override
- public void run() {
- String com = "/dev/ttyUSB2";
- String cmd = "at+qccid\r\n";
- if(adbcommand("chmod 0777 "+com) && sendAtCmd(com,cmd)){
- iccidtv.setText("ICCID:"+recAtresult(com));
- }
- iccidtv.setText("ICCID:"+recAtresult(com));
- //iccidtv.setText("ICCID:"+getSimSerialNumber(getApplicationContext()));
- }
-
- }).start();
-
- public boolean adbcommand(String command) {
- Process process = null;
- DataOutputStream os = null;
- //String excresult = "";
- try {
- process = Runtime.getRuntime().exec("su");
- os = new DataOutputStream(process.getOutputStream());
- os.writeBytes(command + "\n");
- os.writeBytes("exit\n");
- os.flush();
-
- if (process.waitFor() != 0) {
- // System.err.println("exit value = " + process.exitValue());
- }
- BufferedReader in = new BufferedReader(new InputStreamReader(
- process.getInputStream()));
- StringBuffer stringBuffer = new StringBuffer();
- String line = null;
- while ((line = in.readLine()) != null) {
- stringBuffer.append(line + " ");
- }
- //excresult = stringBuffer.toString();
- // System.out.println(excresult);
- return true;
-
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- //return excresult;
- }
- public static boolean sendAtCmd(String fileName, String atcmd) {
- boolean atResult = true;// at指令的返回结果
- Log.i(TAG, "fileName = " + fileName + ", atcmd = " + atcmd);
- try {
- BufferedWriter atWriter = new BufferedWriter(new FileWriter(
- fileName));
- atWriter.write(atcmd, 0, atcmd.getBytes().length);
- atWriter.flush();
- atWriter.close();
- atWriter = null;
- // 以上是往fileName该通道写入AT指令
-
- } catch (FileNotFoundException e2) {
- Log.e(TAG, "FileNotFoundException :" + fileName);
- atResult = false;
- } catch (IOException e) {
- atResult = false;
- e.printStackTrace();
- }
- return atResult;
- }
-
- public String recAtresult(String fileName) {
- int read_len = 0;
- // 下面来读取该通道里的AT指令返回结果
- BufferedReader atReader;
- String readAtString = null;
- char[] cReaderBuffer = new char[255];
- try {
-
- atReader = new BufferedReader(new FileReader(fileName));
- try {
- atReader.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- atReader = null;
-
- try {
- read_len = atReader.read(cReaderBuffer, 0,
- cReaderBuffer.length - 1);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Log.i(TAG, "read_len = " + read_len);
- if (read_len > 0) {
- if (null == readAtString) {
- readAtString = String.copyValueOf(cReaderBuffer, 0, read_len);
- } else {
- readAtString = readAtString
- + String.copyValueOf(cReaderBuffer, 0, read_len);
- }
- Log.i(TAG, "readAtString = " + readAtString);
-
- }
- return readAtString;
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。