我怎样才能通过NFC,而丝网钢钉发送一个字符串? [英] How can I send a string through NFC while Screen-Pinning?

查看:209
本文介绍了我怎样才能通过NFC,而丝网钢钉发送一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过发送字符串 NFC 而我的应用程序使用的屏幕钉住。它不工作:转会不会发生;但如果我禁用屏幕寄托的字符串作品转让。

我可以禁用屏幕寄托了一下,然后进行转让,但这​​是一个安全隐患。

我怎样才能做到这一点?


下面是全部,如果你想尝试code。所有你需要做的是让手动屏幕寄希望于通过你的应用程序设置(所以它不太code,仍然产生相同的结果)。我测试了使用两个的Nexus 7两种运行Android 5.0。

您不必阅读本code,这个问题或许可以得到解决,如果你知道的东西,我可以添加到我的清单,让NFC,同时屏幕牵制。


的Andr​​oidManifest.xml

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.androidnfc
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <用途-SDK
        安卓的minSdkVersion =16
        机器人:targetSdkVersion =19/>
    <使用-权限的Andr​​oid:名称=android.permission.NFC/>

    <应用
        机器人:allowBackup =真
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名称=com.example.androidnfc.MainActivity
            机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>
                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>

            <意向滤光器>
                <作用机器人:名称=android.nfc.action.NDEF_DISCOVERED/>
                <类机器人:名称=android.intent.category.DEFAULT/>
                <数据机器人:MIMETYPE =text / plain的/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>
< /舱单>
 

MainActivity.java

 公共类MainActivity扩展活动实现CreateNdefMessageCallback,OnNdefPushCompleteCallback
{
   TextView的TextInfo中;
   EditText上的TextOut;
   NfcAdapter nfcAdapter;

   @覆盖
   保护无效的onCreate(包savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      的setContentView(R.layout.activity_main);
      TextInfo中=(TextView中)findViewById(R.id.info);
      的TextOut =(EditText上)findViewById(R.id.textout);

      nfcAdapter = NfcAdapter.getDefaultAdapter(本);
      nfcAdapter.setNdefPushMessageCallback(这一点,这一点);
      nfcAdapter.setOnNdefPushCompleteCallback(这一点,这一点);
   }

   @覆盖
   保护无效onResume()
   {
      super.onResume();
      意向意图= getIntent();
      串动= intent.getAction();

      如果(action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED))
      {
         Parcelable [] parcelables的= intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
         NdefMessage inNdefMessage =(NdefMessage)parcelables的[0];
         NdefRecord [] inNdefRecords = inNdefMessage.getRecords();
         NdefRecord NdefRecord_0 = inNdefRecords [0];
         字符串inMsg =新的String(NdefRecord_0.getPayload());
         textInfo.setText(inMsg);
      }
   }

   @覆盖
   保护无效onNewIntent(意向意图){
     setIntent(意向);
   }

   @覆盖
   公共无效onNdefPushComplete(NfcEvent事件){
      最后弦乐eventString =onNdefPushComplete \ N+ event.toString();
      runOnUiThread(新的Runnable(){

        @覆盖
        公共无效的run(){
          Toast.makeText(getApplicationContext(),eventString,Toast.LENGTH_LONG).show();
        }
       });
   }

   @覆盖
   公共NdefMessage createNdefMessage(NfcEvent事件){
      字符串stringOut = textOut.g​​etText()的toString()。
      byte []的bytesOut = stringOut.g​​etBytes();

      NdefRecord n​​defRecordOut =新NdefRecord(
         NdefRecord.TNF_MIME_MEDIA,
         text / plain的.getBytes()
                新的byte [] {},
                bytesOut);

      NdefMessage ndefMessageout =新NdefMessage(ndefRecordOut);
      返回ndefMessageout;
   }
}
 

布局

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    机器人:方向=垂直
    工具:上下文=com.example.androidnfc.MainActivity>

    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =center_horizo​​ntal
        机器人:TEXTSTYLE =黑体/>

    <的EditText
        机器人:ID =@ + ID /的TextOut
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT/>

    <的TextView
        机器人:ID =@ + ID /信息
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中>
 

解决方案

我不知道如果这实际上回答你的问题,但我想总结一下我的结论:

在(在Nexus 4 LRX22C)试图在Android 5.0.1的例子,接收端会自动收到的NDEF消息,并(重新)启动活动unpins屏幕。如此看来,这是注册在清单的意图过滤器获取优先于(人工?)屏幕牵制。

我知道,这不完全匹配的问题描述的经验。的我不知道这是否是由于不同的Andr​​oid版本(5.0与5.0.1)或由于在使用手动屏幕牵制,而不是程序化的屏幕钉扎...

在我的测试设置中,我能够解决的问题(即prevent从得到自动取消固定的活动)使用前景调度系统登记活动,收到了NDEF消息:

在你的 onResume()方法创建一个悬而未决的意图是这样,使前景调度:

  PendingIntent PI = this.createPendingResult(0x00A,新意图(),0);
nfcAdapter.enableForegroundDispatch(这一点,PI,NULL,NULL);
 

您将收到意图通过活动的 onActivityResult()办法通知你有关发现标签:

  @覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    开关(要求code){
        案例0x00A:
            onNewIntent(数据);
        打破;
    }
}
 

此外,你需要禁用前景派遣你的的onPause()方法:

  nfcAdapter.disableForegroundDispatch(本);
 

I am trying to send a String through NFC while my app is using screen pinning. It does not work: The transfer does not happen; but if I disable the screen pinning the transfer of the String works.

I can disable screen pinning for a bit and then perform the transfer, but that is a security risk.

How can I do this?


Here is all the code if you want to try. All you need to do is enable screen pinning manually through your app settings (so it is less code and still produces the same result). I tested this using two Nexus 7 both running Android 5.0.

You don't have to read all this code, this question can probably be solved if you know something I can add to my manifest that would allow NFC while screen pinning.


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidnfc"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.NFC"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.androidnfc.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
        </activity>
    </application>
</manifest>

MainActivity.java

public class MainActivity extends Activity implements CreateNdefMessageCallback, OnNdefPushCompleteCallback
{ 
   TextView textInfo;
   EditText textOut;  
   NfcAdapter nfcAdapter;

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      textInfo = (TextView)findViewById(R.id.info);
      textOut = (EditText)findViewById(R.id.textout);

      nfcAdapter = NfcAdapter.getDefaultAdapter(this);
      nfcAdapter.setNdefPushMessageCallback(this, this);
      nfcAdapter.setOnNdefPushCompleteCallback(this, this);
   }

   @Override
   protected void onResume() 
   {
      super.onResume();
      Intent intent = getIntent();
      String action = intent.getAction();

      if(action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED))
      {
         Parcelable[] parcelables = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
         NdefMessage inNdefMessage = (NdefMessage)parcelables[0];
         NdefRecord[] inNdefRecords = inNdefMessage.getRecords();
         NdefRecord NdefRecord_0 = inNdefRecords[0];
         String inMsg = new String(NdefRecord_0.getPayload());
         textInfo.setText(inMsg);
      }
   }

   @Override
   protected void onNewIntent(Intent intent) {
     setIntent(intent);
   }

   @Override
   public void onNdefPushComplete(NfcEvent event) {
      final String eventString = "onNdefPushComplete\n" + event.toString();
      runOnUiThread(new Runnable() {

        @Override
        public void run() {
          Toast.makeText(getApplicationContext(), eventString, Toast.LENGTH_LONG).show();
        }
       });
   }

   @Override
   public NdefMessage createNdefMessage(NfcEvent event) {
      String stringOut = textOut.getText().toString();
      byte[] bytesOut = stringOut.getBytes();

      NdefRecord ndefRecordOut = new NdefRecord(
         NdefRecord.TNF_MIME_MEDIA, 
         "text/plain".getBytes(),
                new byte[] {}, 
                bytesOut);

      NdefMessage ndefMessageout = new NdefMessage(ndefRecordOut);
      return ndefMessageout;
   }
}

layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidnfc.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/textout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

解决方案

I'm not sure if this actually answers your question, but I'd like to summarize my findings:

When trying your example on Android 5.0.1 (LRX22C on Nexus 4), the receiving side automatically unpins the screen upon receiving the NDEF message and (re-)starts the activity. So it seems that the intent filter that is registered in the manifest gets priority over (manual?) screen pinning.

I'm aware that this does not quite match the experiences described in the question. I'm wondering if this is due to the different Android version (5.0 vs. 5.0.1) or due to the use of manual screen pinning instead of programatic screen pinning...

In my test setup, I was able to solve the problem (i.e. prevent the activity from getting automatically unpinned) by using the foreground dispatch system to register the activity to receive its NDEF message:

In your onResume() method create a pending intent like this and enable foreground dispatch:

PendingIntent pi = this.createPendingResult(0x00A, new Intent(), 0);
nfcAdapter.enableForegroundDispatch(this, pi, null, null);

You will then receive intents notifying you about discovered tags through the activity's onActivityResult() method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case 0x00A:
            onNewIntent(data);
        break;
    }
}

Moreover, you have to disable the foreground dispatch in your onPause() method:

nfcAdapter.disableForegroundDispatch(this);

这篇关于我怎样才能通过NFC,而丝网钢钉发送一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆