NFC支付开发

来源:互联网 发布:qq陌生群发软件 编辑:程序博客网 时间:2024/05/22 03:18

新建自定义service:

最近需要做NFC支付相关的项目,本来对这方面不太了解,查阅了相关资料:
可以参考google官方文档:https://developer.android.com/guide/topics/connectivity/nfc/hce.html
有的手机可能不支持默认NFC,所以得做些判断,手机是否支持NFC,以及NFC是否开启,手动开启还是直接跳转开启
//手机是否支持NFC功能
@TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)public static boolean isSupportNFC(Context context){    // 初始化设备支持NFC功能    boolean isNFC_support = true;    // 得到默认nfc适配器    NfcAdapter nfcAdapter  = NfcAdapter.getDefaultAdapter(context);    // 提示信息定义    String metaInfo = "";    // 判定设备是否支持NFC或启动NFC    if (nfcAdapter == null) {        //metaInfo = "设备不支持NFC!";        isNFC_support = false;    }    return isNFC_support;}
//NFC是否开启@TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)public static boolean isOpenNFC(Context context){    // 初始化设备支持NFC功能    boolean isNFC_support = true;    // 得到默认nfc适配器    NfcAdapter nfcAdapter  = NfcAdapter.getDefaultAdapter(context);    // 提示信息定义    String metaInfo = "";    if (!nfcAdapter.isEnabled()) {       // metaInfo = "请在系统设置中先启用NFC功能!";        isNFC_support = false;    }    return isNFC_support;}

//设置默认支付卡
private void checkIsDefaultApp() {    CardEmulation cardEmulationManager = CardEmulation.getInstance(NfcAdapter.getDefaultAdapter(this));    ComponentName paymentServiceComponent = new ComponentName(getApplicationContext(),  MyHostApduService.class.getCanonicalName());    if (!cardEmulationManager.isDefaultServiceForCategory(paymentServiceComponent, CardEmulation.CATEGORY_PAYMENT)) {        Intent intent = new Intent(CardEmulation.ACTION_CHANGE_DEFAULT);        intent.putExtra(CardEmulation.EXTRA_CATEGORY, CardEmulation.CATEGORY_PAYMENT);        intent.putExtra(CardEmulation.EXTRA_SERVICE_COMPONENT, paymentServiceComponent);        startActivityForResult(intent, 0);    } else {    }}
import android.annotation.TargetApi;import android.content.Intent;import android.nfc.cardemulation.HostApduService;import android.os.Build;import android.os.Bundle;import org.json.JSONException;import org.json.JSONObject;@TargetApi(Build.VERSION_CODES.KITKAT)public class MyHostApduService extends HostApduService { private String data; private String token; public static String userId; @Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent != null) { if (intent.hasExtra("token")) { this.token = intent.getStringExtra("token"); } if (intent.hasExtra("userId")) { this.userId = intent.getStringExtra("userId"); } } return super.onStartCommand(intent, flags, startId); } @Override public void onDeactivated(int arg0) { // TODO Auto-generated method stub } @Override public byte[] processCommandApdu(byte[] apdu, Bundle extra) { String APDU_B64 = HexUtil.bytesToHexString(apdu); if (APDU_B64.equalsIgnoreCase(API.sendCode)) { return HexUtil.hexStringToBytes(API.responseCode); } else { //该处可以做需要的一些操作 return null; } }}
xml文件:apduservice.xml
<host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android"    android:description="@string/service_name"    android:apduServiceBanner="@drawable/icon"    android:requireDeviceUnlock="false" >    <aid-group        android:category="payment"//该处还可以改为other        android:description="@string/service_name" >        <aid-filter android:name="F000000333010101" />        <aid-filter android:name="F000000333010102" />    </aid-group></host-apdu-service>
别忘了在清单文件中注册改service
<service    android:name=".MyHostApduService"    android:exported="true"    android:permission="android.permission.BIND_NFC_SERVICE">    <intent-filter>        <action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" />        <category android:name="android.intent.category.DEFAULT" />    </intent-filter>    <meta-data        android:name="android.nfc.cardemulation.host_apdu_service"        android:resource="@xml/apduservice" /></service>


原创粉丝点击