我正在创建一个应用程序,其中两个设备通过NFC进行通信,然后一个设备传输到服务器。
我正在努力为棒棒糖前后都开发这个。问题是我在一个应用程序中创建了我的NDEF消息,然后在另一个应用程序中接收它。但是,当我尝试在其他应用程序中接收时,会打开NFC“收集新标签”屏幕而不是我的应用程序。对我来说这是一个真正的问题,我只是发送一个我需要发送到Web服务的字符串。
以下是接收申请的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.devcompany.paymentcustomer" >
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".activities.HomeActivity"
android:launchMode="singleTop"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
以下是我的发送代码:
mNfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
//If device is running lollipop remove the touch to beam action
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mNfcAdapter.setNdefPushMessageCallback(nDefCallback, getActivity());
mNfcAdapter.invokeBeam(getActivity());
}
}, 2000);
}else{
//leave touch to beam action
mNfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() {
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
NdefMessage message = new NdefMessage((new NdefRecord[]{createMime("application/com.devcompany.paymentvendor.fragments", mToBeam.getBytes()) }));
return message;
}
}, getActivity());
mNfcAdapter.setOnNdefPushCompleteCallback(
new NfcAdapter.OnNdefPushCompleteCallback() {
@Override
public void onNdefPushComplete(NfcEvent event) {
}
}, getActivity());
}
以下是我的接收代码:
public class HomeActivity extends ActionBarActivity {
private NfcAdapter mAdapter;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_layout);
mAdapter = NfcAdapter.getDefaultAdapter(this);
textView = (TextView)findViewById(R.id.fortressLabel);
}
@Override
public void onResume(){
super.onResume();
if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())){
processIntent(getIntent());
}
}
@Override
public void onNewIntent(Intent intent) {
// onResume gets called after this to handle the intent
setIntent(intent);
}
private void processIntent(Intent intent){
//textView.setText(intent.getDataString());
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
// only one message sent during the beam
NdefMessage msg = (NdefMessage) rawMsgs[0];
Log.i(this.getLocalClassName(), intent.getDataString());
}
}
我已经调试了我的接收代码并逐步完成了,当我将手机放在一起时,代码甚至没有进入onResume方法。标签意图首先启动。我不明白为什么会这样。任何人都可以帮我这个吗?
答案 0 :(得分:3)
您正在发送类型为“application / com.devcompany.paymentvendor.fragments”的MIME类型记录:
DrawOnComponent
因此,您还需要指示您的接收活动实际接收该MIME类型记录。您可以通过为您的活动添加NdefMessage message = new NdefMessage(new NdefRecord[] {
NdefRecord.createMime("application/com.devcompany.paymentvendor.fragments",
mToBeam.getBytes())
});
意图过滤器,在NFC事件中检测到该MIME类型时注册您的应用以打开(并接收):
NDEF_DISCOVERED