public static boolean setAppDataPushMessageCallbackIfAvailable(Activity activity, final CreateNfcAppDataCallback callback) { NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity); if (adapter == null) { return false; } final String packageName = activity.getPackageName(); adapter.setNdefPushMessageCallback(new CreateNdefMessageCallback() { @Override public NdefMessage createNdefMessage(NfcEvent event) { byte[] appData = callback.createNfcAppData(); if (appData == null) { return null; } NdefRecord[] records = new NdefRecord[] { createMimeRecord("application/" + packageName, appData), NdefRecord.createApplicationRecord(packageName) }; return new NdefMessage(records); } }, activity); return true; }
public static boolean setAppDataPushMessageCallbackIfAvailable(Activity activity, final CreateNfcAppDataCallback callback) { NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity); if (adapter == null) { return false; } final String packageName = activity.getPackageName(); adapter.setNdefPushMessageCallback(new CreateNdefMessageCallback() { @Override public NdefMessage createNdefMessage(NfcEvent event) { byte[] appData = callback.createNfcAppData(); if (appData == null) { return null; } NdefRecord[] records = new NdefRecord[]{createMimeRecord("application/" + packageName, appData), NdefRecord.createApplicationRecord(packageName)}; return new NdefMessage(records); } }, activity); return true; }
private void setCallback(NfcAdapter.CreateNdefMessageCallback paramCreateNdefMessageCallback) { if (!this.mFragment.canChangeFragmentManagerState()) {} while (this.mNfcAdapter == null) { return; } this.mNfcAdapter.setNdefPushMessageCallback(paramCreateNdefMessageCallback, this.mFragment.getActivity(), new Activity[0]); }
/** * Ndef封装消息 * * @param ndefRecords */ public void PushText(String message, CreateNdefMessageCallback callback) { PushNdefMessage(new NdefRecord[] { buildNdefRecord(message, Locale.ENGLISH, true) }, callback); }
/** * Ndef封装消息 * * @param message */ public void PushUri(Uri uri, CreateNdefMessageCallback callback) { PushNdefMessage(new NdefRecord[] { NdefRecord.createUri(uri) }, callback); }
/** * Ndef封装消息 * * @param ndefRecords */ public void PushNdefMessage(NdefRecord[] ndefRecords, CreateNdefMessageCallback callback) { mNdefMessage = new NdefMessage(ndefRecords); mNfcAdapter.setNdefPushMessage(mNdefMessage, mActivity); mNfcAdapter.setNdefPushMessageCallback(callback, mActivity); }