當前位置: 首頁>>代碼示例>>Java>>正文


Java NfcAdapter類代碼示例

本文整理匯總了Java中android.nfc.NfcAdapter的典型用法代碼示例。如果您正苦於以下問題:Java NfcAdapter類的具體用法?Java NfcAdapter怎麽用?Java NfcAdapter使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


NfcAdapter類屬於android.nfc包,在下文中一共展示了NfcAdapter類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: readNfcTag

import android.nfc.NfcAdapter; //導入依賴的package包/類
/**
 * 讀取NFC標簽文本數據
 */
private void readNfcTag(Intent intent) {
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
                NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage msgs[] = null;
        int contentSize = 0;
        if (rawMsgs != null) {
            msgs = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; i++) {
                msgs[i] = (NdefMessage) rawMsgs[i];
                contentSize += msgs[i].toByteArray().length;
            }
        }
        try {
            if (msgs != null) {
                NdefRecord record = msgs[0].getRecords()[0];
                String textRecord = parseTextRecord(record);
                mTagText += textRecord + "\n\ntext\n" + contentSize + " bytes";
            }
        } catch (Exception e) {
        }
    }
}
 
開發者ID:jopenbox,項目名稱:android-nfc,代碼行數:27,代碼來源:ReadTextActivity.java

示例2: onNewIntent

import android.nfc.NfcAdapter; //導入依賴的package包/類
@Override
    public void onNewIntent(Intent intent) 
    {
	      String action = intent.getAction();
//	    Log.i("!intent! ", action);

	      Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

        byte[] id = tag.getId();
        String serialNumber = bytesToHex(id);
        WritableMap idData = Arguments.createMap();
        idData.putString("id", serialNumber);

        sendEvent(this.reactContext, "NFCCardID", idData);

    }
 
開發者ID:petersobolev,項目名稱:nfc-react-native-simple,代碼行數:17,代碼來源:NfcReactNativeSimpleModule.java

示例3: onCreate

import android.nfc.NfcAdapter; //導入依賴的package包/類
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    pendingIntent = PendingIntent.getActivity(
            this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    IntentFilter mifare = new IntentFilter((NfcAdapter.ACTION_TECH_DISCOVERED));
    filters = new IntentFilter[] { mifare };
    techs = new String[][] { new String[] {NfcA.class.getName() } };
    if(adapter==null)
    {
        adapter = NfcAdapter.getDefaultAdapter(this);
    }

    imageView = (ImageView) findViewById(R.id.imageView);

    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Animation rotation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotation);

            imageView.startAnimation(rotation);
        }
    });
}
 
開發者ID:ThibaudCrespin,項目名稱:iBeaconReader,代碼行數:27,代碼來源:MainActivity.java

示例4: onNewIntent

import android.nfc.NfcAdapter; //導入依賴的package包/類
@Override
public void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);

    if(intent.hasExtra(NfcAdapter.EXTRA_TAG ))
    {
        if(BackgroundTask.isNetworkAvailable(HubMain.this))
        {
            gifImageView.setVisibility(View.INVISIBLE);
            spinner.setVisibility(View.VISIBLE);
        }
        Parcelable[] parcelables = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

        if(parcelables != null && parcelables.length > 0)
        {
            readTextfromMessage((NdefMessage)parcelables[0]);
        }
    }
}
 
開發者ID:ThomasDelaney,項目名稱:TapIn,代碼行數:21,代碼來源:HubMain.java

示例5: setupForegroundDispatch

import android.nfc.NfcAdapter; //導入依賴的package包/類
/**
 * Setup the recognition of nfc tags when the activity is opened (foreground)
 *
 * @param activity The corresponding {@link Activity} requesting the foreground dispatch.
 * @param adapter The {@link NfcAdapter} used for the foreground dispatch.
 */
public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);

    IntentFilter[] filters = new IntentFilter[1];
    String[][] techList = new String[][]{};

    // Filter for nfc tag discovery
    filters[0] = new IntentFilter();
    filters[0].addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
    filters[0].addCategory(Intent.CATEGORY_DEFAULT);

    adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}
 
開發者ID:digital-voting-pass,項目名稱:polling-station-app,代碼行數:23,代碼來源:PassportConActivity.java

示例6: resolveIntent

import android.nfc.NfcAdapter; //導入依賴的package包/類
private void resolveIntent(Intent data) {
    this.setIntent(data);

    if ((data.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
        return;
    }

    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(data.getAction())) {
        mLastNfcTag = data.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        long now = new Date().getTime();

        if (mContinuousSensorReadingFlag) {
            startContinuousSensorReadingTimer();

        } else if (now - mLastScanTime > 5000) {
            DataPlotFragment dataPlotFragment = (DataPlotFragment)
                    mSectionsPagerAdapter.getRegisteredFragment(R.integer.viewpager_page_show_scan);
            if (dataPlotFragment != null) {
                dataPlotFragment.clearScanData();
            }

            new NfcVReaderTask(this).execute(mLastNfcTag);
        }
    }
}
 
開發者ID:DorianScholz,項目名稱:OpenLibre,代碼行數:26,代碼來源:MainActivity.java

示例7: tryUpdateIntentFromBeam

import android.nfc.NfcAdapter; //導入依賴的package包/類
/**
 * Checks to see if the activity's intent ({@link android.app.Activity#getIntent()}) is
 * an NFC intent that the app recognizes. If it is, then parse the NFC message and set the
 * activity's intent (using {@link Activity#setIntent(android.content.Intent)}) to something
 * the app can recognize (i.e. a normal {@link Intent#ACTION_VIEW} intent).
 */
public static void tryUpdateIntentFromBeam(Activity activity) {
    Intent originalIntent = activity.getIntent();
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(originalIntent.getAction())) {
        Parcelable[] rawMsgs = originalIntent.getParcelableArrayExtra(
                NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage msg = (NdefMessage) rawMsgs[0];
        // Record 0 contains the MIME type, record 1 is the AAR, if present.
        // In iosched, AARs are not present.
        NdefRecord mimeRecord = msg.getRecords()[0];
        if (ScheduleContract.makeContentItemType(
                ScheduleContract.Sessions.CONTENT_TYPE_ID).equals(
                new String(mimeRecord.getType()))) {
            // Re-set the activity's intent to one that represents session details.
            Intent sessionDetailIntent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse(new String(mimeRecord.getPayload())));
            activity.setIntent(sessionDetailIntent);
        }
    }
}
 
開發者ID:dreaminglion,項目名稱:iosched-reader,代碼行數:26,代碼來源:BeamUtils.java

示例8: handleIntent

import android.nfc.NfcAdapter; //導入依賴的package包/類
private void handleIntent(final Intent intent) {
    final String action = intent.getAction();

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        final String inputType = intent.getType();
        final NdefMessage ndefMessage = (NdefMessage) intent
                .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)[0];
        final byte[] input = Nfc.extractMimePayload(Constants.MIMETYPE_TRANSACTION, ndefMessage);

        new BinaryInputParser(inputType, input) {
            @Override
            protected void handlePaymentIntent(final PaymentIntent paymentIntent) {
                cannotClassify(inputType);
            }

            @Override
            protected void error(final int messageResId, final Object... messageArgs) {
                dialog(WalletActivity.this, null, 0, messageResId, messageArgs);
            }
        }.parse();
    }
}
 
開發者ID:guodroid,項目名稱:okwallet,代碼行數:23,代碼來源:WalletActivity.java

示例9: registerTagEvent

import android.nfc.NfcAdapter; //導入依賴的package包/類
@ReactMethod
  private void registerTagEvent(String alertMessage, Boolean invalidateAfterFirstRead, Callback callback) {
      Log.d(LOG_TAG, "registerTag");
isForegroundEnabled = true;

// capture all mime-based dispatch NDEF
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
	ndef.addDataType("*/*");
} catch (MalformedMimeTypeException e) {
	throw new RuntimeException("fail", e);
   }
intentFilters.add(ndef);

// capture all rest NDEF, such as uri-based
      intentFilters.add(new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED));
techLists.add(new String[]{Ndef.class.getName()});

// for those without NDEF, get them as tags
      intentFilters.add(new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED));

if (isResumed) {
	enableDisableForegroundDispatch(true);
}
      callback.invoke();
	}
 
開發者ID:whitedogg13,項目名稱:react-native-nfc-manager,代碼行數:27,代碼來源:NfcManager.java

示例10: onCreate

import android.nfc.NfcAdapter; //導入依賴的package包/類
@Override
public void onCreate(Bundle savedState) {
    super.onCreate(savedState);

    mAdapter = NfcAdapter.getDefaultAdapter(this);

    // Create an NDEF message a URL
    mMessage = new NdefMessage(NdefRecord.createUri("http://www.android.com"));

    setContentView(R.layout.foreground_dispatch);
    mText = (TextView) findViewById(R.id.text);

    if (mAdapter != null) {
        mAdapter.setNdefPushMessage(mMessage, this);
        mText.setText("Tap another Android phone with NFC to push a URL");
    } else {
        mText.setText("This phone is not NFC enabled.");
    }
}
 
開發者ID:appledong,項目名稱:AndroidthingsStudy,代碼行數:20,代碼來源:ForegroundNdefPush.java

示例11: processIntent

import android.nfc.NfcAdapter; //導入依賴的package包/類
private void processIntent(Intent i) {
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(i.getAction())) {
        Parcelable[] rawMsgs =
                i.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage msg = (NdefMessage) rawMsgs[0];
        String url = new String(msg.getRecords()[0].getPayload());
        Utils.debugLog(TAG, "Got this URL: " + url);
        Toast.makeText(this, "Got this URL: " + url, Toast.LENGTH_LONG).show();
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        intent.setClass(this, ManageReposActivity.class);
        startActivity(intent);
        finish();
    }
}
 
開發者ID:uhuru-mobile,項目名稱:mobile-store,代碼行數:15,代碼來源:RepoDetailsActivity.java

示例12: prepareNfcMenuItems

import android.nfc.NfcAdapter; //導入依賴的package包/類
@TargetApi(16)
private void prepareNfcMenuItems(Menu menu) {
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    MenuItem menuItem = menu.findItem(R.id.menu_enable_nfc);

    if (nfcAdapter == null) {
        menuItem.setVisible(false);
        return;
    }

    boolean needsEnableNfcMenuItem;
    if (Build.VERSION.SDK_INT < 16) {
        needsEnableNfcMenuItem = !nfcAdapter.isEnabled();
    } else {
        needsEnableNfcMenuItem = !nfcAdapter.isNdefPushEnabled();
    }

    menuItem.setVisible(needsEnableNfcMenuItem);
}
 
開發者ID:uhuru-mobile,項目名稱:mobile-store,代碼行數:20,代碼來源:RepoDetailsActivity.java

示例13: setAndroidBeam

import android.nfc.NfcAdapter; //導入依賴的package包/類
@TargetApi(16)
public static void setAndroidBeam(Activity activity, String packageName) {
    if (Build.VERSION.SDK_INT < 16) {
        return;
    }
    PackageManager pm = activity.getPackageManager();
    NfcAdapter nfcAdapter = getAdapter(activity);
    if (nfcAdapter != null) {
        ApplicationInfo appInfo;
        try {
            appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
            Uri[] uris = {
                    Uri.parse("file://" + appInfo.publicSourceDir),
            };
            nfcAdapter.setBeamPushUris(uris, activity);
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "Could not get application info", e);
        }
    }
}
 
開發者ID:uhuru-mobile,項目名稱:mobile-store,代碼行數:21,代碼來源:NfcHelper.java

示例14: onCreate

import android.nfc.NfcAdapter; //導入依賴的package包/類
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hub_main);

    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    setTitle("Tap In Hub");


    yeeid = (TextView)findViewById(R.id.yeeid);
    spinner = (ProgressBar)findViewById(R.id.progressBar);
    gifImageView = (GifImageView)findViewById(R.id.gifImageView);

    try
    {
        InputStream inputStream = getAssets().open("nfc.gif");

        byte[] bytes = IOUtils.toByteArray(inputStream);
        gifImageView.setBytes(bytes);
        gifImageView.startAnimation();
    }
    catch (IOException e)
    {}
    gifImageView.setVisibility(View.VISIBLE);
    spinner.setVisibility(View.INVISIBLE);

}
 
開發者ID:ThomasDelaney,項目名稱:TapIn,代碼行數:29,代碼來源:HubMain.java

示例15: onCreate

import android.nfc.NfcAdapter; //導入依賴的package包/類
/**
 * This activity usually be loaded from the starting screen of the app.
 * This method handles the start-up of the activity, it does not need to call any other methods
 * since the activity onNewIntent() calls the intentHandler when a NFC chip is detected.
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();
    documentData = (DocumentData) extras.get(DocumentData.identifier);
    thisActivity = this;

    setContentView(R.layout.activity_passport_con);
    Toolbar appBar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(appBar);
    Util.setupAppBar(appBar, this);
    TextView notice = (TextView) findViewById(R.id.notice);
    progressView = (ImageView) findViewById(R.id.progress_view);

    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    checkNFCStatus();
    notice.setText(R.string.nfc_enabled);
}
 
開發者ID:digital-voting-pass,項目名稱:polling-station-app,代碼行數:25,代碼來源:PassportConActivity.java


注:本文中的android.nfc.NfcAdapter類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。