Beispiel #1
0
  private void enableNfc() {

    if (android.os.Build.VERSION.SDK_INT >= 14) {

      // Check for available NFC Adapter
      mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
      if (mNfcAdapter == null) {
        // NFC is not available on this device
      } else {

        if (!(mNfcAdapter.isEnabled() && mNfcAdapter.isNdefPushEnabled())) {

          AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder
              .setMessage(R.string.nfc_activate_required_services)
              .setTitle(R.string.message_activate_nfc)
              .setPositiveButton(
                  R.string.yes,
                  new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                      Toast.makeText(
                              getApplicationContext(),
                              getString(R.string.message_activate_nfc),
                              Toast.LENGTH_LONG)
                          .show();

                      startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
                    }
                  })
              .setNegativeButton(
                  R.string.no,
                  new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {}
                  })
              .create()
              .show();
        }
      }
    }
  }
Beispiel #2
0
  private String initNfcAdapter(NfcAdapter adapter) {
    StringBuilder sb = new StringBuilder();

    // nfc 功能是否开启
    if (nfcAdapter == null) {
      sb.append("支持NFC功能:" + false + "\n");
      return sb.toString();
    } else {
      sb.append("支持NFC功能:" + true + "\n");
    }
    isNfcEnable = nfcAdapter.isEnabled();
    sb.append("NFC是否开启:" + isNfcEnable + "\n");

    isNdefPushEnabled = nfcAdapter.isNdefPushEnabled();
    sb.append("Android Beam是否开启:" + isNdefPushEnabled + "\n");

    nfcAdapter.setNdefPushMessageCallback(this, this);
    nfcAdapter.setOnNdefPushCompleteCallback(this, this);

    return sb.toString();
  }