本文介绍了Android的NFC,做的onCreate空支票?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code初始化我NFC适配器,但我不知道怎么了的onCreate期间intialize的变量。该应用程序可以初始化一个NFC标签是否是在接近与否,IE浏览器。如果有人简单地打开该应用程序。所以,当我得到了这条线 NfcV nfcMessage = NfcV.get(新TagGet()getTag()); 它崩溃,因为它是零,也没有任何标签如果你只是加载在你自己的应用程序。如何检查这里!= NULL 我不知道要检查或如何构建这个code哪个部分。

其结果是,我想读取标签的内容敌我识别标签是present。否则,只是加载的布局,等待NfcV标签进行扫描。

  //设置一个意图过滤器都是基于MIME调度
    IntentFilter的nfcv =新的IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    尝试 {
        nfcv.addDataType(* / *);
    }赶上(MalformedMimeTypeException E){
        抛出新的RuntimeException(不及格,E);
    }
    mFilters =新的IntentFilter [] {
            nfcv,
    };

    //设置一个技术列表中的所有NfcF标签
    mTechLists =新的String [] [] {新的String [] {NfcV.class.getName()}};

    //mAdapter.enableForegroundDispatch(this,mPendingIntent,mFilters,mTechLists);

    NfcV nfcMessage = NfcV.get(新TagGet()getTag());

    byte []的数据=新的字节[2048]; //标签长度不能有任何更大
    字符串值=;
    尝试 {
        数据= nfcMessage.transceive(新的字节[2048]);
        值=新的String(数据);
    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
 

解决方案

听是我第一次NFC应用程序的基本方式code轮廓。我试图取代值(我用NFCA代替nfcV),这样您就可以更好地阅读它。这是一个非常基本的结构,你需要填写的部分用自己的code,但它应该给你怎样的NFC应用程序的结构能够正常工作是个好主意。

 公共类Android_nfc_ibox扩展活动实现Runnable {




NfcAdapter mNfcAdapter;
私有String [] [] mTechLists;
PendingIntent pendingIntent;
标签标签;
NFCA MTAG;

/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    //初始化NFC适配器
    mNfcAdapter = NfcAdapter.getDefaultAdapter(本);
    如果(mNfcAdapter!= NULL){
        dialog_text.append(点击一个NFC标签进行访问\ñ\ r);
    } 其他 {
        dialog_text.append(这手机没有NFC功能\ñ\ r);
    }

    //创建PendingIntent对象将包含该标记的已被扫描的细节
    pendingIntent = PendingIntent.getActivity(此,0,新意图(本,的getClass())addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);

    //设置一个技术列表中的所有所需的标签类型
    mTechLists =新的String [] [] {新的String [] {NfcA.class.getName()}};

}

    / **重新启用标签调度,如果应用程序是在前台* /
   @覆盖
    公共无效onResume(){
        super.onResume();
        如果(mNfcAdapter!= NULL)mNfcAdapter.enableForegroundDispatch(这一点,pendingIntent,空,mTechLists);
    }

   / **禁用标记调度时,应用程序不再在前台* /
    @覆盖
    公共无效的onPause(){
        super.onPause();
        如果(!mNfcAdapter = NULL)mNfcAdapter.disableForegroundDispatch(本);
    }

    / **的标记已经发现* /
    @覆盖
    公共无效onNewIntent(意向意图){

        //获取标签的对象为已发现的标签
        标签标记= intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

        //尝试并获得MifareUltralight实例这个标签
        MTAG = NfcV.get(标签);

        //如果null,则这不是一个NfcV标记,以便等待下一次
        如果(MTAG == NULL){
            dialog_text.append(不是一个NFC V标签\ n \ r);
        }

           //开始标签通信线
           螺纹myThread =新主题(本);
           myThread.start();

        }
    }

   //(我们可以创建其他线程用​​于其它类型的标签)
   公共无效的run(){
       //尝试连接到所述NFC V标签
       尝试{

           mTag.connect();
       }赶上(IOException异常E){
            //这里处理错误
       }

       //这将发送原始数据
       //给你想要的值的字节[]
       //只是用逗号添加原始的十六进制值
       // pageBuffer是一个数组,将持有的响应
       尝试{
           pageBuffer = mTag.transceive(新的byte [] {0×11,0X24,为0x11});
       }赶上(IOException异常E){
            //这里处理错误
       }
    }
 

}

I have code to initialize my NFC adapter, but I am not sure how to intialize the variables during the onCreate. The app can be initialized whether an NFC TAG is in proximity or not, ie. if someone simply opened the app. So when I get down to this line NfcV nfcMessage = NfcV.get(new TagGet().getTag()); it crashes because it is null, there is no tag there if you just load the app on your own. How do I check here for != null I'm not sure which part to check or how to structure this code.

The result is that I want to read the contents of the tag IFF a tag is present. Otherwise just load the layout and wait for a NfcV tag to be scanned.

// Setup an intent filter for all MIME based dispatches
    IntentFilter nfcv = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    try {
        nfcv.addDataType("*/*");
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("fail", e);
    }
    mFilters = new IntentFilter[] {
            nfcv,
    };

    // Setup a tech list for all NfcF tags
    mTechLists = new String[][] { new String[] { NfcV.class.getName() } };

    //mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);

    NfcV nfcMessage = NfcV.get(new TagGet().getTag());

    byte[] data = new byte[2048]; //tag length can't be any larger
    String value = "";
    try {
        data = nfcMessage.transceive(new byte[2048]);
        value = new String(data);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
解决方案

Hear is a code outline of the basic way I made my first Nfc app. I tried to replace values (I used nfcA instead of nfcV) so you could read it better. It is a very basic structure, and you would need to fill in parts with your own code, but It should give you a good idea of how the structure of an NFC app can work.

public class Android_nfc_ibox extends Activity implements Runnable {




NfcAdapter mNfcAdapter;
private String[][] mTechLists;
PendingIntent pendingIntent;
Tag tag;
NfcA mTag;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Initialize the NFC adapter
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mNfcAdapter != null) {
        dialog_text.append("Tap an NFC tag for access\n\r");
    } else {
        dialog_text.append("This phone is not NFC enabled\n\r");
    }

    // Create the PendingIntent object which will contain the details of the tag that has been scanned
    pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    // Setup a tech list for all desired tag types
    mTechLists = new String[][] { new String[] { NfcA.class.getName() } };

}

    /** Re-enable the tag dispatch if the app is in the foreground */
   @Override
    public void onResume() {
        super.onResume();
        if (mNfcAdapter != null) mNfcAdapter.enableForegroundDispatch(this, pendingIntent, null, mTechLists);
    }

   /** Disable the tag dispatch when the app is no longer in the foreground */
    @Override
    public void onPause() {
        super.onPause();
        if (mNfcAdapter != null) mNfcAdapter.disableForegroundDispatch(this);
    }

    /** A tag has been discovered */
    @Override
    public void onNewIntent(Intent intent){

        // get the tag object for the discovered tag
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

        // try and get the MifareUltralight instance for this tag
        mTag = NfcV.get(tag);

        // if null then this wasn't a NfcV tag so wait for next time
        if(mTag == null){
            dialog_text.append("Not a Nfc V tag\n\r");
        }

           // Start the tag communications thread
           Thread myThread = new Thread(this);
           myThread.start();

        }
    }

   // (we could create other threads for other types of tags)
   public void run(){
       // try to connect to the Nfc V tag
       try{

           mTag.connect();
       }catch(IOException e){
            //handle the error here
       }

       //this will send raw data
       //send the values you want in the byte[]
       //just add the raw hex values with commas
       //pageBuffer is an array that will hold the response
       try{
           pageBuffer = mTag.transceive(new byte[] {0x11, 0x24, 0x11});
       }catch(IOException e){
            //handle error here
       }
    }

}

这篇关于Android的NFC,做的onCreate空支票?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:08