2016-07-16 7 views
1

私はXamarinでC#でコーディングしています。私はあるデバイスから別のデバイスにNFCを介してデータを共有しようとしました。他のNFC対応デバイスをタップしてもonNewIntentが呼び出されない

ブラウザを開き - >オプション - >共有 - > APP4 MainActivity

私のデバイスの両方に同じアプリケーションを実行していると私は他のデバイスに自分のデバイスをタップするんだけど、何も起こりません。

onNewIntent()には届かないと思います。

私は何かを見逃しましたか?私は混乱して1週間は探し続けています。ここで

は私のコードです:

using System; 
using Android.App; 
using Android.Content; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.OS; 
using Android.Nfc; 
using System.Collections.Generic; 
using System.Text; 
using Xamarin.Forms; 

namespace App4 
{ 
    [Activity(Label = "App4", MainLauncher = false, Icon = "@drawable/icon",LaunchMode = Android.Content.PM.LaunchMode.SingleTop)] 
    [IntentFilter(new[] { Intent.ActionSend , NfcAdapter.ActionNdefDiscovered }, Categories = new[] { 
    Intent.CategoryDefault, 
    Intent.CategoryBrowsable 
    }, DataMimeType = "text/plain")] 
    public class MainActivity : Activity 
    { 
     string share; 
     PendingIntent mPendingIntent; 
     IntentFilter ndefDetected; 
     IntentFilter[] intentF; 
     TextView testTV; 
     protected override void OnCreate(Bundle bundle) 
     { 

      base.OnCreate(bundle); 
      SetContentView(Resource.Layout.Main); 

      Intent Myintent = new Intent(this, GetType()); 
      Myintent.SetFlags(ActivityFlags.SingleTop); 
      mPendingIntent = PendingIntent.GetActivity(this, 0, Myintent, 0); 
      ndefDetected = new IntentFilter(NfcAdapter.ActionNdefDiscovered); 
      try 
      { 
       ndefDetected.AddDataType("text/plain"); 
       ndefDetected.AddCategory(Intent.CategoryDefault); 
      } 
      catch { }; 

      intentF = new IntentFilter[] { ndefDetected }; 
      NfcAdapter NA = NfcAdapter.GetDefaultAdapter(this); 

      if (NA!=null && NA.IsEnabled) 
      { 
       Toast.MakeText(this, "Nfc Found", ToastLength.Long).Show(); 
      }else 
      { 
       Toast.MakeText(this, "Nfc Not Found", ToastLength.Long).Show(); 
      } 
      testTV = FindViewById<TextView>(Resource.Id.text_view); 
      share = Intent.GetStringExtra(Intent.ExtraText); 
      testTV.Text = share; 
    } 

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

      NfcManager manager = (NfcManager)GetSystemService(NfcService); 
      NfcAdapter adapter = manager.DefaultAdapter; 
      adapter.DisableForegroundNdefPush(this); 
      adapter.DisableForegroundDispatch(this); 
     } 

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


      var result2 = new byte[NdefRecord.RtdText.Count]; 
      NdefRecord.RtdUri.CopyTo(result2, 0); 

      NfcManager manager = (NfcManager)GetSystemService(NfcService); 
      NdefRecord record = new NdefRecord(NdefRecord.TnfAbsoluteUri,new byte[0], new byte[0], System.Text.Encoding.Default.GetBytes(share)); 
      manager.DefaultAdapter.EnableForegroundNdefPush(this, new NdefMessage(record)); 
      manager.DefaultAdapter.EnableForegroundDispatch(this, mPendingIntent, intentF, null); 
     } 

     protected override void OnNewIntent(Intent intent) 
     { 

      base.OnNewIntent(intent); 
      testTV.Text = "onNewIntent";   
     } 
    } 
} 

、ここでは私のAndroidManifest.xmlです:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="App4.App4" android:versionCode="1" android:versionName="1.0" android:installLocation="auto"> 
    <uses-sdk android:minSdkVersion="10" /> 
    <uses-permission android:name="android.permission.NFC"></uses-permission> 
    <application android:label="App4"></application> 
</manifest> 

答えて

0

はい、あなたは何かが欠けている:あなたはのNDEFメッセージをリッスンするために、フォアグラウンドディスパッチを登録しました"text/plain"と入力します。これは、TextレコードまたはMIMEタイプtext/plainのレコードが必要であることを意味します。

ndefDetected = new IntentFilter(NfcAdapter.ActionNdefDiscovered); 
ndefDetected.AddDataType("text/plain"); 
ndefDetected.AddCategory(Intent.CategoryDefault); 
intentF = new IntentFilter[] { ndefDetected }; 
manager.DefaultAdapter.EnableForegroundDispatch(this, mPendingIntent, intentF, null); 

ただし、無効な(空!)タイプ名の絶対URIレコードがプッシュされます。

NdefRecord record = new NdefRecord(NdefRecord.TnfAbsoluteUri,new byte[0], new byte[0], System.Text.Encoding.Default.GetBytes(share)); 
manager.DefaultAdapter.EnableForegroundNdefPush(this, new NdefMessage(record)); 

インテントフィルタを一致させるためには、あなたが代わりにテキストレコードをプッシュする必要がありますが:

byte[] text = System.Text.Encoding.UTF8.GetBytes(share); 
byte[] language = System.Text.Encoding.ASCII.GetBytes("en"); 
byte[] payload = new byte[1 + language.Count + text.Count]; 
payload[0] = (byte)language.Count; 
System.Array.Copy(language, 0, payload, 1, language.Count); 
System.Array.Copy(text, 0, payload, 1 + language.Count, text.Count); 
NdefRecord record = new NdefRecord(NdefRecord.TnfWellKnown, new List<byte>(NdefRecord.RtdText).ToArray(), new byte[0], payload); 
manager.DefaultAdapter.EnableForegroundNdefPush(this, new NdefMessage(record)); 
+0

私は後でそれをしようとします。非常に助けてくれてありがとう! – wuken

+0

これは動作します!!!!!どうもありがとうございました!あなたは私の救い主です! – wuken

+0

Btw、あなたのコードのようにNedfRecord.RtdTextを使用する方法?私は今NdefRecord.CreateTextRecord( "en"、share)を使用しています。 "System.Collections.Generic.IList 'から'byte []'" に変換できません。新しいバイト[]:result2にコピーしようとしています。それは働くだろうか? – wuken

関連する問題