2016-12-09 1 views
0

Android 애플리케이션에 NFC 기술을 사용하고 싶습니다.알 수없는 태그 유형의 Android NFC 태그 읽기 Xamarin - C#

사용자가 NFC 카드을 스캔하여 응용 프로그램에 로그인 할 수있게하려고합니다. 카드에 번호가 있습니다.

그래서 아이디어는 코드를 검색하고 문자열

로 변환하지만 가상 메소드를 호출하는 Nullobjectreference

시도 '부울 android.nfc.Tag.hasTech 받고 있어요하는 것입니다 널 객체에 대한 참조 (INT) '

UPDATE

소스 코드 :

using System; 
using System.Text; 
using Android.App; 
using Android.Nfc; 
using Android.OS; 
using Android.Widget; 
using Android.Content; 
using Android.Nfc.Tech; 

namespace NFC_NDEF 
{ 
[Activity(Label = "NFC_NDEF_TST", MainLauncher = true, Icon = "@drawable/icon")] 
[IntentFilter(new[] { NfcAdapter.ActionTechDiscovered })] 

public class MainActivity : Activity 
{ 
NfcAdapter nfcAdapter; 
PendingIntent nfcPi; 
IntentFilter nfcFilter; 
Tag nfcTag; 
string newLine = System.Environment.NewLine; 

protected override void OnCreate(Bundle bundle) 
{ 
    base.OnCreate(bundle); 
    SetContentView(Resource.Layout.Main); 

    // Get our button from the layout resource, 
    // and attach an event to it 
    Button btnScan = FindViewById<Button>(Resource.Id.MyButton); 
    btnScan.Click += Scan; 

    //var writerButton = FindViewById<Button>(Resource.Id.WriteButton); 
    //writerButton.Click += Write; 

    var label = FindViewById<TextView>(Resource.Id.textView_rslt); 

    nfcAdapter = NfcAdapter.GetDefaultAdapter(ApplicationContext); 
    if (nfcAdapter == null) 
    { 
     label.Text = "NFC is not available."; 
     return; 
    } 

    if (!nfcAdapter.IsEnabled) 
    { 
     label.Text = "NFC is disabled."; 
     return; 
    } 


    var intent = new Intent(this, this.Class); 
    intent.AddFlags(ActivityFlags.SingleTop); 
    nfcPi = PendingIntent.GetActivity(this, 0, intent, 0); 
    nfcFilter = new IntentFilter(NfcAdapter.ActionTechDiscovered); 
    nfcFilter.AddCategory(Intent.CategoryDefault); 
} 

private void Scan(object sender, EventArgs e) 
{ 
    var label = FindViewById<TextView>(Resource.Id.textView_rslt); 

    try 
    { 
     if (nfcTag == null) 
     { 
      label.Text = "nfc tag is null"; 
      return; 
     } 

     var ndef = Ndef.Get(nfcTag); 
     ndef.Connect(); 
     var data = Encoding.UTF8.GetString(ndef.NdefMessage.ToByteArray()); 
     ndef.Close(); 

     label.Text = $"Data:{newLine}{data}"; 
    } 
    catch (Exception ex) 
    { 
     label.Text += $"{newLine} Exception: {newLine} {ex.Message} {newLine} {ex.StackTrace}"; 
    } 
} 

protected override void OnResume() 
{ 
    base.OnResume(); 

    nfcAdapter.EnableForegroundDispatch(this, nfcPi, new IntentFilter[] { nfcFilter }, null); 

    if (NfcAdapter.ActionTechDiscovered == Intent.Action) 
    { 
     ProcessIntent(Intent); 
    } 
} 

protected override void OnNewIntent(Intent intent) 
{ 
    base.OnNewIntent(intent); 
    Intent = intent; 
    if (NfcAdapter.ActionTechDiscovered == intent.Action) 
    { 
     ProcessIntent(Intent); 
    } 
} 

private void ProcessIntent(Intent intent) 
{ 
    var label = FindViewById<TextView>(Resource.Id.textView_rslt); 

    try 
    { 
     nfcTag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag; 

     label.Text = $"Id: 0x{Smartisan.Nfc.Utils.ByteToHex(nfcTag.GetId())}"; 
     label.Text += $"{newLine}Techs: {newLine}"; 
     label.Text += string.Join(newLine, nfcTag.GetTechList()); 
    } 
    catch (Exception ex) 
    { 
     label.Text += $"{newLine} Exception: {newLine} {ex.Message} {newLine} {ex.StackTrace}"; 
    } 
} 

} 
} 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="NFC_NDEF.NFC_NDEF" android:versionCode="1" android:versionName="1.0" android:installLocation="auto"> 
    <uses-sdk android:minSdkVersion="16" /> 
    <uses-permission android:name="android.permission.NFC" /> 
    <uses-sdk android:targetSdkVersion="15" /> 
    <uses-feature android:name="android.hardware.nfc" android:required="true" /> 
<application android:label="NFC_NDEF"></application> 
</manifest> 
+0

https://github.com/poz1/NFCForms 사용 후 일 카드의 종류 NFCA 에 필터링 불과했다 지금까지. – BytesGuy

+0

UR 시간을내어 주셔서 감사합니다. 세부 정보, 소스 코드 및 예외가 포함 된 게시물을 읽어주십시오. –

+0

어디서 오류가 발생 했습니까? from nfcTag.GetTechList()' –

답변

0

문제는 당신이 시도 무엇에 대한 비용을 게시하시기 바랍니다