NFC csak Androidra javítás ...
This commit is contained in:
@@ -10,5 +10,5 @@
|
||||
<uses-permission android:name="android.permission.CAMERA"/>
|
||||
|
||||
|
||||
<uses-feature android:name="android.hardware.nfc" android:required="false" />
|
||||
<uses-feature android:name="android.hardware.nfc" android:required="true" />
|
||||
</manifest>
|
||||
@@ -1,40 +1,68 @@
|
||||
using Android.App;
|
||||
using Android.Content;
|
||||
using Android.Content.PM;
|
||||
using Android.Nfc;
|
||||
using Android.OS;
|
||||
using Plugin.NFC;
|
||||
|
||||
|
||||
namespace WorkFlowCheck.MAUI
|
||||
{
|
||||
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
|
||||
public class MainActivity : MauiAppCompatActivity
|
||||
{
|
||||
NfcAdapter nfcAdapter;
|
||||
PendingIntent pendingIntent;
|
||||
static Action<string> _onTagRead;
|
||||
|
||||
protected async override void OnDestroy()
|
||||
{
|
||||
SecureStorage.Default.Remove("WFCUser");
|
||||
SecureStorage.Default.Remove("AuthToken");
|
||||
base.OnDestroy();
|
||||
}
|
||||
|
||||
protected override void OnCreate(Bundle savedInstanceState)
|
||||
{
|
||||
// Plugin NFC: Initialization before base.OnCreate(...) (Important on .NET MAUI)
|
||||
CrossNFC.Init(this);
|
||||
|
||||
base.OnCreate(savedInstanceState);
|
||||
}
|
||||
protected override void OnResume()
|
||||
{
|
||||
base.OnResume();
|
||||
nfcAdapter = NfcAdapter.GetDefaultAdapter(this);
|
||||
pendingIntent = PendingIntent.GetActivity(this, 0,
|
||||
new Intent(this, typeof(MainActivity)).AddFlags(ActivityFlags.SingleTop),
|
||||
Build.VERSION.SdkInt >= BuildVersionCodes.S ? PendingIntentFlags.Mutable : PendingIntentFlags.UpdateCurrent);
|
||||
|
||||
// Plugin NFC: Restart NFC listening on resume (needed for Android 10+)
|
||||
CrossNFC.OnResume();
|
||||
}
|
||||
|
||||
protected override void OnNewIntent(Intent intent)
|
||||
{
|
||||
base.OnNewIntent(intent);
|
||||
|
||||
// Plugin NFC: Tag Discovery Interception
|
||||
CrossNFC.OnNewIntent(intent);
|
||||
if (intent.Action == NfcAdapter.ActionTagDiscovered)
|
||||
{
|
||||
var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
|
||||
var id = tag?.GetId();
|
||||
if (id != null)
|
||||
{
|
||||
var hex = BitConverter.ToString(id).Replace("-", "");
|
||||
_onTagRead?.Invoke(hex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void StartNfc(Action<string> onTagRead)
|
||||
{
|
||||
_onTagRead = onTagRead;
|
||||
if (nfcAdapter.IsEnabled)
|
||||
{
|
||||
nfcAdapter?.EnableForegroundDispatch(this, pendingIntent, null, null);
|
||||
}
|
||||
}
|
||||
|
||||
public void StopNfc()
|
||||
{
|
||||
_onTagRead = null;
|
||||
//if (!IsFinishing && !IsDestroyed)
|
||||
//{
|
||||
// nfcAdapter?.DisableForegroundDispatch(this);
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user