using Android.App; using Android.Content; using Android.Content.PM; using Android.Nfc; using Android.Nfc.Tech; using Android.OS; using Com.Lvcheng.Lock.Shared.Nfc.Example.Java; using NFCLockDemoV2; using NFCLockDemoV2.Platforms.Android; using System.Diagnostics; namespace NFCLockAppV2; [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density, Exported = true)] [IntentFilter(new[] { Intent.ActionMain }, Categories = new[] { Intent.CategoryLauncher })] [IntentFilter(new[] { NfcAdapter.ActionNdefDiscovered, NfcAdapter.ActionTechDiscovered, NfcAdapter.ActionTagDiscovered }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = "text/plain")] public class MainActivity : MauiAppCompatActivity { private NFCLockHelper nfcHelper; private NFCCallbacks _callbacks; //private NfcAdapter _nfcAdapter; //private PendingIntent _pendingIntent; //private string[][] _techLists = [[typeof(NfcA).FullName, typeof(NdefFormatable).FullName, typeof(MifareUltralight).FullName]]; //private IntentFilter[] _intentFilters = [new(NfcAdapter.ActionNdefDiscovered), new(NfcAdapter.ActionTechDiscovered), new(NfcAdapter.ActionTagDiscovered)]; //private volatile bool _processing = false; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); _callbacks = new NFCCallbacks(); nfcHelper = new NFCLockHelper(this, _callbacks); //_nfcAdapter = NfcAdapter.GetDefaultAdapter(this); //_pendingIntent = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(MainActivity)).AddFlags(ActivityFlags.ReceiverReplacePending), PendingIntentFlags.Mutable); } protected override void OnResume() { base.OnResume(); nfcHelper.OnResume(); //if (_nfcAdapter != null) //{ // _nfcAdapter.EnableForegroundDispatch(this, _pendingIntent, _intentFilters, _techLists); //} } protected override void OnPause() { base.OnPause(); nfcHelper.OnPause(); //if (_nfcAdapter != null) //{ // _nfcAdapter.DisableForegroundDispatch(this); //} } protected override void OnNewIntent(Intent intent) { base.OnNewIntent(intent); nfcHelper.OnNewIntent(intent); //if (!_processing) //{ // if (intent != null && (intent.Action == NfcAdapter.ActionNdefDiscovered || // intent.Action == NfcAdapter.ActionTechDiscovered || // intent.Action == NfcAdapter.ActionTagDiscovered)) // { // Task.Run(async () => // { // try // { // _processing = true; // Tag? tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag; // if (tag != null) // { // NfcA? nfcA = NfcA.Get(tag); // if (nfcA != null) // { // nfcA.Connect(); // nfcA.Timeout = 1000; // Process(nfcA); // } // } // } // catch (Exception e) // { // _callbacks.OnLost(); // Console.WriteLine(e); // } // finally // { // await Task.Delay(2000); // _processing = false; // _callbacks.OnFinished(); // } // }); // } //} } private void Process(NfcA nfcA) { DeviceManagerWrapper wrapper = new DeviceManagerWrapper(nfcA); try { Com.Lvcheng.Lock.Shared.Nfc.Result initRes = wrapper.Init(); if (initRes.Equals(Com.Lvcheng.Lock.Shared.Nfc.Result.Ok)) { long? id; bool? isNew; int? mRssi; // 无限循环,替代 do-while while (true) { Com.Lvcheng.Lock.Shared.Nfc.Result getInfoRes = wrapper.Info; if (!getInfoRes.Equals(Com.Lvcheng.Lock.Shared.Nfc.Result.Ok)) return; _ = wrapper.Rssi; id = wrapper.MGetId().LongValue(); isNew = wrapper.MGetIsNew().BooleanValue(); mRssi = wrapper.MGetRssi().IntValue(); // 判断是否继续循环 bool continueLoop = _callbacks.OnLoop(wrapper, id, isNew, mRssi); System.Diagnostics.Debug.WriteLine("continueLoop:" + continueLoop); // 不继续就退出循环 if (!continueLoop) break; } } } catch (Exception e) { Console.WriteLine(e); } } }