2017-08-16 27 views
1

我试图做到这一点,当我打开我的应用程序并扫描一个标记时,它会显示一个带有标记ID的烤面包。问题是,每次我扫描一个标签时,它会列出一个应用程序列表,我可以在其中选择我的应用程序。NFC前台调度不工作

我试图实现前台调度机制,但它并没有显示任何东西(它既不崩溃,也不显示吐司)。

这是主代码:

public class loginPage extends AppCompatActivity { 
    NfcAdapter mAdapter; 
    PendingIntent pendingIntent; 
    private String serialId = ""; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login_page); 
     mAdapter = NfcAdapter.getDefaultAdapter(this); 
     pendingIntent = PendingIntent.getActivity(
       this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 
     if (mAdapter == null) { 
      Toast.makeText(this, 
        "NFC NOT supported on this devices!", 
        Toast.LENGTH_LONG).show(); 
      finish(); 
     } else if (!mAdapter.isEnabled()) { 
      Toast.makeText(this, 
        "NFC NOT Enabled!", 
        Toast.LENGTH_LONG).show(); 
      finish(); 
     } 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     mAdapter.enableForegroundDispatch(this, pendingIntent, null, null); 
    } 
    @Override 
    public void onPause(){ 
     super.onPause(); 
     mAdapter.disableForegroundDispatch(this); 

    } 
    @Override 
    public void onNewIntent(Intent intent) { 
     Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
     String action = intent.getAction(); 
     if (action.equals(NfcAdapter.ACTION_TECH_DISCOVERED)) { 
      try { 
       byte[] tagId = tag.getId(); 
       serialId = toHexString(tagId); 
       Log.d("[ReadCardTools]", "Serial Number: " + serialId); 
       Toast.makeText(this, serialId, Toast.LENGTH_SHORT).show(); 
      } catch (NullPointerException ex) { 
       ex.printStackTrace(); 
       serialId = "ERROR"; 
      } 
     } 
    } 

    public static String toHexString(byte[] bytes) { 
     char[] hexArray = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 
     char[] hexChars = new char[bytes.length * 2]; 
     int v; 
     for (int j = 0; j < bytes.length; j++) { 
      v = bytes[j] & 0xFF; 
      hexChars[j * 2] = hexArray[v/16]; 
      hexChars[j * 2 + 1] = hexArray[v % 16]; 
     } 
     return new String(hexChars); 
    } 
} 

清单包含下列添加物:

<uses-permission android:name="android.permission.NFC"/> 
<uses-feature android:name="android.hardware.nfc" 
    android:required="true"/> 

<intent-filter> 
    <action android:name="android.nfc.action.TECH_DISCOVERED"/> 
</intent-filter> 
<meta-data android:name="android.nfc.action.TECH_DISCOVERED" 
      android:resource="@xml/nfc_tech_filter" /> 

该过滤器是:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> 
<tech-list> 
    <tech>android.nfc.tech.IsoDep</tech> 
</tech-list> 
<tech-list> 
    <tech>android.nfc.tech.NfcA</tech> 
</tech-list> 
<tech-list> 
    <tech>android.nfc.tech.NfcB</tech> 
</tech-list> 
<tech-list> 
    <tech>android.nfc.tech.NfcF</tech> 
</tech-list> 
<tech-list> 
    <tech>android.nfc.tech.NfcV</tech> 
</tech-list> 
<tech-list> 
    <tech>android.nfc.tech.Ndef</tech> 
</tech-list> 
<tech-list> 
    <tech>android.nfc.tech.NdefFormatable</tech> 
</tech-list> 
<tech-list> 
    <tech>android.nfc.tech.MifareClassic</tech> 
</tech-list> 
<tech-list> 
    <tech>android.nfc.tech.MifareUltralight</tech> 
</tech-list> 
</resources> 

每次我扫描一个标签时,logcat只会说“D/PersonaManager:isNFCAllowed”。

回答

1

当前台调度系统启用为捕获全局(即方法enableForegroundDispatch()的最后两个参数设置为null)时,将向您的活动发送TAG_DISCOVERED意图。由于您只听取TECH_DISCOVERED意图(并忽略所有其他意图),因此您不会在标签发现时显示敬酒。

你会因此需要在在onNewIntent接收TAG_DISCOVERED意图也作用:

@Override 
public void onNewIntent(Intent intent) { 
    String action = intent.getAction(); 
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) || 
     NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) { 
     Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
     if (tag != null) { 
      serialId = toHexString(tag.getId()); 
      Log.d("[ReadCardTools]", "Serial Number: " + serialId); 
      Toast.makeText(this, serialId, Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 
+0

非常感谢你!这按预期工作。 –