/** * Implementation for the CreateNdefMessageCallback interface */ @Override public NdefMessage createNdefMessage(NfcEvent event) { Time time = new Time(); time.setToNow(); String text = ("Beam me up!\n\n" + "Beam Time: " + time.format("%H:%M:%S")); NdefMessage msg = new NdefMessage(NdefRecord.createMime( "application/com.example.android.beam", text.getBytes()) /** * The Android Application Record (AAR) is commented out. When a device * receives a push with an AAR in it, the application specified in the AAR * is guaranteed to run. The AAR overrides the tag dispatch system. * You can add it back in to guarantee that this * activity starts when receiving a beamed message. For now, this code * uses the tag dispatch system. */ //,NdefRecord.createApplicationRecord("com.example.android.beam") ); return msg; }
@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); cinemas = DBHelper.getInstance(getContext()).getCinemas(); watcher = new Gson().fromJson(getArguments().getString("watcher"), Watcher.class); nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity()); if(nfcAdapter != null) { nfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() { @Override public NdefMessage createNdefMessage(NfcEvent nfcEvent) { return new NdefMessage( new NdefRecord[] { NdefRecord.createUri(BuildConfig.SERVER_BASE_URL + "w/" + watcher.getID()), NdefRecord.createApplicationRecord(BuildConfig.APPLICATION_ID) } ); } }, getActivity()); } }
public void registerNfcCallback(Activity activity) { NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); if (nfcAdapter != null) { // Register callback nfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() { @Override public NdefMessage createNdefMessage(NfcEvent event) { return new NdefMessage( new NdefRecord[] { NdefRecord.createMime( "application/vnd.com.pgrenaud.android.p2p.beam", getPeerRepository().encode().getBytes() ) } ); } }, activity); Log.d("PeerService", "NdefPushMessageCallback registered"); } }
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; }
@Override public Uri[] createBeamUris(NfcEvent event) { String transferFile = FullscreenActivity.songfilename; File extDir; if (FullscreenActivity.whichSongFolder.equals(getString(R.string.mainfoldername))) { extDir =FullscreenActivity.dir; } else { extDir = new File(FullscreenActivity.dir + "/" + FullscreenActivity.whichSongFolder + "/"); } File requestFile = new File(extDir, transferFile); boolean b = requestFile.setReadable(true, false); if (!b) { // Get a URI for the File and add it to the list of URIs Uri fileUri = Uri.fromFile(requestFile); if (fileUri != null) { mFileUris[0] = fileUri; } else { Log.e("My Activity", "No File URI available for file."); } return mFileUris; } else { return null; } }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void setupNfcBeamPush() { NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mAppContext); if (adapter == null) { return; } if (!ApiHelper.HAS_SET_BEAM_PUSH_URIS) { // Disable beaming adapter.setNdefPushMessage(null, CameraActivity.this); return; } adapter.setBeamPushUris(null, CameraActivity.this); adapter.setBeamPushUrisCallback(new CreateBeamUrisCallback() { @Override public Uri[] createBeamUris(NfcEvent event) { return mNfcPushUris; } }, CameraActivity.this); }
@Override public NdefMessage createNdefMessage(NfcEvent event) { return new NdefMessage( new NdefRecord[] { NdefRecord.createMime( "application/vnd.co.loubo.icicle", this.encodedNodeRef.getBytes(Charset.forName("US-ASCII"))) /** * The Android Application Record (AAR) is commented out. When a device * receives a push with an AAR in it, the application specified in the AAR * is guaranteed to run. The AAR overrides the tag dispatch system. * You can add it back in to guarantee that this * activity starts when receiving a beamed message. For now, this code * uses the tag dispatch system. */ //,NdefRecord.createApplicationRecord("com.example.android.beam") }); }
@Override public NdefMessage createNdefMessage(NfcEvent event) { // NFC should be auto granted, but doesn't hurt to check. if (ContextCompat.checkSelfPermission(this, Manifest.permission.NFC) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.NFC}, NFC_PERMISSIONS); } String url = URLEncoder.encodeBuild(this, currentBuild); NdefMessage msg = new NdefMessage( new NdefRecord[] { createMime( "application/vnd.com.jamieadkins.heistr", url.getBytes()) ,NdefRecord.createApplicationRecord("com.jamieadkins.heistr") }); return msg; }
@Override public void start() { if (!nfcAdapter.isEnabled()) { this.activity.startActivity(new Intent(Settings.ACTION_NFC_SETTINGS)); return; } this.nfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() { @Override public NdefMessage createNdefMessage(NfcEvent event) { byte[] payload = new byte[Preferences.getInstance().getPayloadSize()]; Arrays.fill(payload, (byte) 1); return new NdefMessage( new NdefRecord[] { NdefRecord.createMime( MIMETYPE, payload) }); } },this.activity); }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void setupNfcBeamPush() { NfcAdapter adapter = NfcAdapter.getDefaultAdapter(CameraActivity.this); if (adapter == null) { return; } if (!ApiHelper.HAS_SET_BEAM_PUSH_URIS) { // Disable beaming adapter.setNdefPushMessage(null, CameraActivity.this); return; } adapter.setBeamPushUris(null, CameraActivity.this); adapter.setBeamPushUrisCallback(new CreateBeamUrisCallback() { @Override public Uri[] createBeamUris(NfcEvent event) { return mNfcPushUris; } }, CameraActivity.this); }
@Override public NdefMessage createNdefMessage(NfcEvent event) { if (currentUri == null) { // || !isStarted) ignore ... if (Log.logErrorMessages()) { Log.e(LOG_TAG, "I have no URI, can't send NFC message. "); } return null; // don't provide a message } if (Log.logDebugMessages()) { Log.d(LOG_TAG, "NFC transaction is taking place ..."); } NdefMessage msg = new NdefMessage( new NdefRecord[]{ NdefRecord.createUri(currentUri) }); return msg; }
private void initNFC() { NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this); if (mNfcAdapter == null) { return; } mNfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() { @Override public NdefMessage createNdefMessage(NfcEvent event) { String text = (GlobalContext.getInstance().getCurrentAccountName()); NdefMessage msg = new NdefMessage( new NdefRecord[]{createMimeRecord( "application/org.qii.weiciyuan.beam", text.getBytes()), NdefRecord.createApplicationRecord(getPackageName()) }); return msg; } }, this); }
/** * Call this method in an Activity, between onCreate() and onDestroy(), to make its content sharable using Android Beam if available. MIME type of the data * to share will be "application/" followed by the app's package name. Declare it in your Manifest's intent filters as the data type with an action of * android.nfc.action.NDEF_DISCOVERED to handle the NFC Intents on the receiver side. * * @return true if NFC is available and the content was made available, false if not. */ 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 NfcAdapter.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; }
@SuppressLint("NewApi") public void activateNFCSharing(Activity activity) { if (mShAIReHttpServer != null && mShAIReHttpServer.getRealUrl() != null) { // NFC if (android.os.Build.VERSION.SDK_INT > 14) { NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(activity); if (nfcAdapter != null) { nfcAdapter.setNdefPushMessageCallback( new NfcAdapter.CreateNdefMessageCallback() { @Override public NdefMessage createNdefMessage( NfcEvent event) { return new NdefMessage( new NdefRecord[]{NdefRecord.createUri(Uri .parse(mShAIReHttpServer .getRealUrl()))}); } }, activity); } } } }
@Override public NdefMessage createNdefMessage(NfcEvent nfcEvent) { try { String msg = getEncryptedMessage(); NdefRecord mimeRecord = new NdefRecord( NdefRecord.TNF_MIME_MEDIA, Const.ARROW_MIME.getBytes(Charset.forName("US-ASCII")), new byte[0], msg.getBytes(Charset.forName("US-ASCII"))); NdefMessage message = new NdefMessage(new NdefRecord[]{mimeRecord}); return message; } catch (Exception e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. Toast.makeText(ArrowActivity.this, ArrowActivity.this.getString(R.string.arrow_oops), Toast.LENGTH_LONG).show(); } return null; }
/** * Implementation for the CreateNdefMessageCallback interface */ @Override public NdefMessage createNdefMessage(NfcEvent event) { Time time = new Time(); time.setToNow(); String text = ("Beam me up!\n\n" + "Beam Time: " + time.format("%H:%M:%S")); NdefMessage msg = new NdefMessage( new NdefRecord[] { createMimeRecord( "application/edu.cs4730.nfcdemo.beam", text.getBytes()) /** * The Android Application Record (AAR) is commented out. When a device * receives a push with an AAR in it, the application specified in the AAR * is guaranteed to run. The AAR overrides the tag dispatch system. * You can add it back in to guarantee that this * activity starts when receiving a beamed message. For now, this code * uses the tag dispatch system. */ //,NdefRecord.createApplicationRecord("com.example.android.beam") }); return msg; }
private void initNFC() { NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this); if (mNfcAdapter == null) { return; } mNfcAdapter.setNdefPushMessageCallback( new NfcAdapter.CreateNdefMessageCallback() { @Override public NdefMessage createNdefMessage(NfcEvent event) { String text = (GlobalContext.getInstance() .getCurrentAccountName()); NdefMessage msg = new NdefMessage( new NdefRecord[] { createMimeRecord( "application/com.engc.smartedu.beam", text.getBytes()), NdefRecord .createApplicationRecord(getPackageName()) }); return msg; } }, this); }
private boolean enable_nfc() { nfcTextView.setVisibility(View.VISIBLE); NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); if (nfcAdapter != null) { // NFC is available on this device nfcAdapter.setBeamPushUris(null, this); nfcAdapter.setBeamPushUrisCallback(new NfcAdapter.CreateBeamUrisCallback() { @Override public Uri[] createBeamUris(NfcEvent event) { Uri[] nfcPushUris = new Uri[1]; nfcPushUris[0] = imageUri; return nfcPushUris; } }, this); return true; } else { return false; } }
/** * * Implementation of {@link CreateNdefMessageCallback} interface. * * This method is called when another device is within reach (communication is to take place). */ @Override public NdefMessage createNdefMessage(NfcEvent event) { Log.d(TAG, "Create message to be beamed"); // create message to be pushed, for example Message message = new Message(); // add text record message.add(new TextRecord("This is my beam text record")); // add 'my' external type record message.add(new GenericExternalTypeRecord("my.domain", "atype", "My data".getBytes(Charset.forName("UTF-8")))); // encode to NdefMessage, will be pushed via beam (now!) (unless there is a collision) return message.getNdefMessage(); }
@Override @TargetApi(14) public NdefMessage createNdefMessage(NfcEvent event) { ObjectMapper mapper = Serialization.getJsonInstance(); String text = null; try { text = mapper.writeValueAsString(server.exportLogin()); } catch (JsonProcessingException e) { Log.e(THIS, "Could not export Login", e); } NdefMessage msg = new NdefMessage( new NdefRecord[] { new NdefRecord( NdefRecord.TNF_MIME_MEDIA, Std.APP_MIME.getBytes(Charset.forName("US-ASCII")), new byte[0], text.getBytes(Charset.forName("US_ASCII"))), NdefRecord.createApplicationRecord("de.stadtrallye.rallyesoft") }); return msg; }
@Override public NdefMessage createNdefMessage(final NfcEvent event) { byte[] langBytes = Locale.getDefault().getLanguage() .getBytes(Charset.forName("US-ASCII")); byte[] textBytes = mEt.getText().toString().trim() .getBytes(Charset.forName("UTF-8")); char status = (char) (langBytes.length); byte[] data = new byte[1 + langBytes.length + textBytes.length]; data[0] = (byte) status; System.arraycopy(langBytes, 0, data, 1, langBytes.length); System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length); return new NdefMessage(new NdefRecord[] { new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data), NdefRecord.createApplicationRecord(getPackageName()) }); }
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private void trySetBeamCallback() { if (UIUtils.hasICS()) { BeamUtils.setBeamCompleteCallback(this, new NfcAdapter.OnNdefPushCompleteCallback() { @Override public void onNdefPushComplete(NfcEvent event) { // Beam has been sent if (!BeamUtils.isBeamUnlocked(SearchActivity.this)) { BeamUtils.setBeamUnlocked(SearchActivity.this); runOnUiThread(new Runnable() { @Override public void run() { showFirstBeamDialog(); } }); } } }); } }
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private void trySetBeamCallback() { if (UIUtils.hasICS()) { BeamUtils.setBeamCompleteCallback(this, new NfcAdapter.OnNdefPushCompleteCallback() { @Override public void onNdefPushComplete(NfcEvent event) { // Beam has been sent if (!BeamUtils.isBeamUnlocked(SessionsVendorsMultiPaneActivity.this)) { BeamUtils.setBeamUnlocked(SessionsVendorsMultiPaneActivity.this); runOnUiThread(new Runnable() { @Override public void run() { showFirstBeamDialog(); } }); } } }); } }
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private void trySetBeamCallback() { if (UIUtils.hasICS()) { BeamUtils.setBeamCompleteCallback(this, new NfcAdapter.OnNdefPushCompleteCallback() { @Override public void onNdefPushComplete(NfcEvent event) { // Beam has been sent if (!BeamUtils.isBeamUnlocked(SessionDetailActivity.this)) { BeamUtils.setBeamUnlocked(SessionDetailActivity.this); runOnUiThread(new Runnable() { @Override public void run() { showFirstBeamDialog(); } }); } } }); } }
@Override public void onNdefPushComplete(NfcEvent event) { if (mErrorRunnableIfBeamSent != null) { Handler h = new Handler(Looper.getMainLooper()); h.postDelayed(mErrorRunnableIfBeamSent, TOAST_ERROR_DELAY_MS); mErrorRunnableIfBeamSent = null; } }
/** * Implementation for the OnNdefPushCompleteCallback interface */ @Override public void onNdefPushComplete(NfcEvent arg0) { // A handler is needed to send messages to the activity when this // callback occurs, because it happens from a binder thread mHandler.obtainMessage(MESSAGE_SENT).sendToTarget(); }
private void dismissalTask() { if(nfcAdapter != null && getActivity() != null && !getActivity().isFinishing()) { nfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() { @Override public NdefMessage createNdefMessage(NfcEvent nfcEvent) { return new NdefMessage( new NdefRecord[] { NdefRecord.createUri("https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID + "&feature=beam"), NdefRecord.createUri(BuildConfig.APPLICATION_ID) } ); } }, getActivity()); // Don't keep sharing this watcher when the sheet is dismissed, return something similar to Android Beam default } }
@Override public NdefMessage createNdefMessage(final NfcEvent event) { final byte[] paymentRequest = paymentRequestRef.get(); if (paymentRequest != null) return new NdefMessage( new NdefRecord[] { Nfc.createMime(PaymentProtocol.MIMETYPE_PAYMENTREQUEST, paymentRequest) }); else return null; }
@Override public NdefMessage createNdefMessage(final NfcEvent event) { final String uri = currentAddressUriRef.get(); if (uri != null) return new NdefMessage(new NdefRecord[] { NdefRecord.createUri(uri) }); else return null; }
protected void registerNdefPushMessageCallback() { NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); if (nfcAdapter != null && nfcAdapter.isEnabled()) { nfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() { @Override public NdefMessage createNdefMessage(NfcEvent nfcEvent) { return new NdefMessage(new NdefRecord[]{ NdefRecord.createUri(getShareableUri()), NdefRecord.createApplicationRecord("de.tengu.chat") }); } }, this); } }
protected void registerNdefPushMessageCallback() { NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); if (nfcAdapter != null && nfcAdapter.isEnabled()) { nfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() { @Override public NdefMessage createNdefMessage(NfcEvent nfcEvent) { return new NdefMessage(new NdefRecord[]{ NdefRecord.createUri(getShareableUri()), NdefRecord.createApplicationRecord("eu.siacs.conversations") }); } }, this); } }