android訪問NFC的SE所有文檔都在這裡。
可惜的是,目前只能訪問內部SE,對於外部sim或SD的SE,幾乎所有的手機都不支持,原因:支持SIM卡的SE,還沒有AT命令;支持SD卡的SE,還沒有API接口。為什麼會這樣,很簡單,因為它們不是google,沒有開源的精神!所以我們現在只能訪問內部SE,就是存在NFC芯片的SE。
訪問SE使用的是SEService/Smartcard Service。手機上必須要安裝SEService/Smartcard Service,這個由手機研發實現。
創建
[html]
seService = new SEService(this, this);
繼承
[html]
implements SEService.CallBack
實現接口
[html]
public void serviceConnected(SEService service)
{
}
就可以訪問SE了,代碼如下:
[html]
public class MainActivity extends Activity implements SEService.CallBack {
private SEService seService;
NfcAdapter mNfc;
TextView mTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNfc = NfcAdapter.getDefaultAdapter(this);
// do layout and UI stuff
setContentView(R.layout.main);
mTextView = (TextView) this.findViewById(R.id.text);
try {
// 訪問SEService
seService = new SEService(this, this);
} catch (SecurityException e) {
mTextView.append("Binding not allowed, uses-permission SMARTCARD?");
} catch (Exception e) {
mTextView.append("Exception: " + e.getMessage());
}
}
@Override
protected void onDestroy() {
if (seService != null && seService.isConnected()) {
seService.shutdown();
}
super.onDestroy();
}
//連接上SEService
public void serviceConnected(SEService service)
{
Session session = null;
Channel basicChannel = null;
try {
mTextView.append("Getting available readers...\n");
Reader[] readers = seService.getReaders();
//readers[1]就是內部SE
session = readers[1].openSession();
//選擇card manager aid
byte[] aid = {(byte) 0xA0,0x00,0x00,0x00,0x03,0x00,0x00,0x00};
basicChannel = session.openBasicChannel(aid);
//readers[1]獲取card prodution life cycle 數據
byte[] CPLC = {(byte) 0x80,(byte) 0xca,(byte) 0x9f,0x7f,0x00};
byte[] response1 = basicChannel.transmit(CPLC);
if(response1.length>=20)
{
mTextView.append(String.format("SerialID: %02x%02x%02x%02x\n",
response1[15],response1[16],response1[17],response1[18]));
}
} catch (Exception e) {
mTextView.append("Error occured: "+e.getMessage()+"\n");
}
if(basicChannel!=null)
basicChannel.close();
if(session!=null)
session.close();
}
}