对安卓工程师来说,在安卓下使用USB设备需要了解很多硬件的内容,这可能会导致工程周期的延长或者app的不稳定。为了将这种风险降到最低,友我科技发布了NFC读写器在安卓下的sdk,使用NFC读写器的类接口,安卓工程师就可以直接调用NFC读写器了,简单可靠。
NFC读写器的安卓库文件为:yoworfidreader.jar
在ADT工程中导入yoworfidreader.jar库文件,即可在工程中引用NFC读写器的类文件
在代码中调用:
public usbreader rfidreader;
rfidreader = new usbreader();
这样就创建了NFC读写器的类的实体,下面只要使用rfidreader的各种方法就可以了,以下为读写M1卡的例子代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_m1);
rbKeyA=(RadioButton) findViewById(R.id.rbkeya);
txtCardNo = (TextView) findViewById(R.id.txtcardno);
edtKey = (EditText) findViewById(R.id.edtkey);
edtKey.setText(""FFFFFFFFFFFF"");
edtBlockID = (EditText) findViewById(R.id.edtblock);
edtBlockID.setText(""1"");
edtData = (EditText) findViewById(R.id.edtdata);
edtData.setText(""11111111111111112222222222222222"");
Button button = (Button) findViewById(R.id.btnreadCard);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txtCardNo.setText("""");
edtData.setText("""");
int BlockID = MainActivity.ValidInt( edtBlockID.getText().toString() , 63);
if(BlockID<0)
{
MainActivity.dialog(""块号必须是0-63"",M1.this);
return;
}
if(!MainActivity.ValidHexString(edtKey.getText().toString(), 6))
{
MainActivity.dialog(""密钥必须是6字节16进制"",M1.this);
return;
}
if( MainActivity.rfidreader.ISO14443A.YW_RequestCard(MainActivity.rfidreader.ISO14443A.REQUESTMODE_ALL)<0)return;
byte[] CardNo=MainActivity.rfidreader.ISO14443A.YW_AntiCollideAndSelect(MainActivity.rfidreader.ISO14443A.MULTIMODE_ONE);
if(CardNo==null)return;
txtCardNo.setText(""卡号:"" + MainActivity.bytesToHexString(CardNo));
byte KeyMode=MainActivity.rfidreader.ISO14443A.Key_B;
if(rbKeyA.isChecked())KeyMode=MainActivity.rfidreader.ISO14443A.Key_A;
if(MainActivity.rfidreader.ISO14443A.YW_KeyAuthorization(KeyMode, BlockID, MainActivity.HexStringtoBytes( edtKey.getText().toString()))<0)
{
MainActivity.dialog(""密钥认证错误"",M1.this);
return;
}
byte[] Data = MainActivity.rfidreader.ISO14443A.YW_ReadaBlock(BlockID);
if(Data==null)
{
MainActivity.dialog(""读取块失败"",M1.this);
return;
}
edtData.setText(MainActivity.bytesToHexString(Data));
MainActivity.rfidreader.ReaderHardware.YW_Buzzer(5, 5, 1);
}
});
Button button2 = (Button) findViewById(R.id.btnwritedata);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txtCardNo.setText("""");
int BlockID = MainActivity.ValidInt( edtBlockID.getText().toString() , 63);
if(BlockID<0)
{
MainActivity.dialog(""块号必须是0-63"",M1.this);
return;
}
if(!MainActivity.ValidHexString(edtKey.getText().toString(), 6))
{
MainActivity.dialog(""密钥必须是6字节16进制"",M1.this);
return;
}
if(!MainActivity.ValidHexString(edtData.getText().toString(), 16))
{
MainActivity.dialog(""数据必须是16字节16进制"",M1.this);
return;
}
if( MainActivity.rfidreader.ISO14443A.YW_RequestCard(MainActivity.rfidreader.ISO14443A.REQUESTMODE_ALL)<0)return;
byte[] CardNo=MainActivity.rfidreader.ISO14443A.YW_AntiCollideAndSelect(MainActivity.rfidreader.ISO14443A.MULTIMODE_ONE);
if(CardNo==null)return;
txtCardNo.setText(""卡号:"" + MainActivity.bytesToHexString(CardNo));
byte KeyMode=MainActivity.rfidreader.ISO14443A.Key_B;
if(rbKeyA.isChecked())KeyMode=MainActivity.rfidreader.ISO14443A.Key_A;
if(MainActivity.rfidreader.ISO14443A.YW_KeyAuthorization(KeyMode, BlockID, MainActivity.HexStringtoBytes( edtKey.getText().toString()))<0)
{
MainActivity.dialog(""密钥认证错误"",M1.this);
return;
}
byte[] Data = MainActivity.HexStringtoBytes(edtData.getText().toString());
if(MainActivity.rfidreader.ISO14443A.YW_WriteaBlock(BlockID, Data)>0)
{
MainActivity.dialog(""数据写入成功"",M1.this);
MainActivity.rfidreader.ReaderHardware.YW_Buzzer(5, 5, 1);
}
else
MainActivity.dialog(""数据写入失败"",M1.this);
}
});
}
}
支持安卓的NFC读写器有YW-605系列和YW-607等
安卓NFC读写器的在线开发指南:NFC读写器安卓开发指南