zoukankan      html  css  js  c++  java
  • Xamarin如何使用终端设备的NFC功能传递卡号等信息给Web页面(Android)

    一、前提条件,App必须具有NFC权限。

    二、项目中加入监控类NFCCatchActivity.cs

        [Activity(Label = "NFCCatch",Theme = "@android:style/Theme.NoDisplay")]
        [IntentFilter(new[] { "android.nfc.action.TECH_DISCOVERED" })]
        [MetaData("android.nfc.action.TECH_DISCOVERED", Resource = "@xml/nfc_tech_filter")]
        public class NFCCatchActivity : Activity
        {
            NfcAdapter mNfcAdapter;
            PendingIntent mPendingIntent;
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
    
                mNfcAdapter = NfcAdapter.GetDefaultAdapter(this);
                if (mNfcAdapter == null)
                {
                    //textViewNFC.Text = "NFC is not available on this device.";
                }
                else
                {
                    mPendingIntent = PendingIntent.GetActivity(this, 0, new Intent(this,
                    typeof(NFCCatchActivity)), PendingIntentFlags.UpdateCurrent);
    
                    OnNewIntent(Intent);
                }
            }
            protected override void OnResume()
            {
                base.OnResume();
                if (mNfcAdapter != null)
                    mNfcAdapter.EnableForegroundDispatch(this, mPendingIntent, null, null);
            }
            protected override void OnPause()
            {
                base.OnPause();
                if (mNfcAdapter != null)
                    mNfcAdapter.DisableForegroundDispatch(this);
            }
    
            public static string ByteArrayToString(byte[] ba)
            {
                string hex = BitConverter.ToString(ba);
                string rtnStr = "";
                string timeN = DateTime.Now.ToString("ssmmHHddMMyyMMyyddHHmmssMMddHHmm");
                string tmp1 = "";
                string tmp2 = "";
                }
                rtnStr = tmp1 + hex + tmp2;
                return rtnStr;
            }
            protected override void OnNewIntent(Intent intent)
            {
                base.OnNewIntent(intent);
                Intent = intent;
                Tag tag = (Tag)intent.GetParcelableExtra(NfcAdapter.ExtraTag);
                string cardID = ByteArrayToString(tag.GetId());
    //本活动为后台运行,在获取到IC卡号之后,调用主活动中的方法处理实际业务,如反馈给H5页面。
                MainActivity.instance.SetNFCData(cardID);
            }
        }
    NFC处理类

    三、与之相关的主活动方法

            public void SetNFCData(string nfcid)
            {
                //EvaluateJavascript不会刷新页面
                //调用H5页面的函数,赋值给文本框或变量都可以
                string script = "javascript:SetNFCID('"+ nfcid + "');";
                web_view.EvaluateJavascript(script, null);
            }
        

    四、由此,就可以实现在终端上感应源,获取源的信息到Web页面之中了。

  • 相关阅读:
    mysql的缓冲查询和非缓冲查询
    通过中看不中用的代码分析Ioc容器,依赖注入....
    register_shutdown_function AND fastcgi_finish_request
    laravel5源码讲解整理
    ERROR! MySQL is running but PID file could not be found
    读《高性能javascript》笔记(一)
    看php手册2015-03-19版后备注
    rawurlencode / urlencode
    Zen Cart屏蔽中文语言浏览器
    WooCommerce Shortcode 简码使用方式说明
  • 原文地址:https://www.cnblogs.com/imes/p/9770674.html
Copyright © 2011-2022 走看看