ios库绑定

This commit is contained in:
denggaofeng 2026-01-23 15:46:13 +08:00
parent 763c12f86e
commit 78243352b3
7 changed files with 85 additions and 10 deletions

View File

@ -64,6 +64,7 @@
CommandParameter="Unlock"
BackgroundColor="{Binding Operation, Converter={StaticResource OperationColorConverter}, ConverterParameter=UNLOCK}"
TextColor="Black"
Clicked="UnlockClicked"
HorizontalOptions="Fill" />
<!-- 关锁按钮 -->
@ -73,6 +74,7 @@
CommandParameter="Lock"
BackgroundColor="{Binding Operation, Converter={StaticResource OperationColorConverter}, ConverterParameter=LOCK}"
TextColor="Black"
Clicked="LockClicked"
HorizontalOptions="Fill" />
<!--~1~ 设置密码按钮 @1@

View File

@ -1,4 +1,9 @@
using NFCLockDemoV2.ViewModels;
using NFCLockDemoV2.ViewModels;
#if IOS
using Foundation;
using NFCManager = NfcLock.Ios.Binding.NFCManager;
#endif
namespace NFCLockDemoV2;
@ -6,10 +11,80 @@ public partial class MainPage : ContentPage
{
private MainViewModel _mainMainViewModel;
#if IOS
private void SetNFCCallbacks()
{
NFCManager.SetLoopCbWithCb(new LoopCallback(this));
NFCManager.SetFinishedCbWithCb(() =>
{
System.Diagnostics.Debug.WriteLine("finished");
_mainMainViewModel.DeviceId = "--";
_mainMainViewModel.DeviceRssi = "--";
_mainMainViewModel.DeviceStatus = "NFC操作完成";
_mainMainViewModel.Operation = null;
_mainMainViewModel.ArcProgress = 0;
});
NFCManager.SetChargingCbWithCb((NSString res, int level) =>
{
System.Diagnostics.Debug.WriteLine($"res: {res}, level: {level}");
_mainMainViewModel.UpdateMessage($"充电中({level}%)");
_mainMainViewModel.ArcProgress = level;
});
NFCManager.SetControllingCbWithCb((NSString res, int progress) =>
{
System.Diagnostics.Debug.WriteLine($"res: {res}, progress: {progress}");
_mainMainViewModel.UpdateMessage($"控制中({progress}%)");
});
}
private class LoopCallback : Foundation.NSObject
{
private readonly MainPage mainPage;
public LoopCallback(MainPage page)
{
this.mainPage = page;
}
[Export("callAsFunctionWithId:isNew:rssi:")]
public bool CallAsFunction(IntPtr id, bool isNew, int rssi)
{
string deviceId = NSString.FromHandle(id);
mainPage._mainMainViewModel.DeviceId = deviceId;
mainPage._mainMainViewModel.DeviceRssi = rssi.ToString();
return true; // 继续循环
}
}
#endif
public MainPage(MainViewModel mainViewModel)
{
InitializeComponent();
_mainMainViewModel = mainViewModel;
BindingContext = _mainMainViewModel;
#if IOS
SetNFCCallbacks();
#endif
}
private void LockClicked(object? sender, EventArgs e)
{
#if IOS
NFCManager.LockWithPassword(_mainMainViewModel.Password);
NFCManager.StartScan();
#endif
}
private void UnlockClicked(object? sender, EventArgs e)
{
#if IOS
NFCManager.UnlockWithPassword(_mainMainViewModel.Password);
NFCManager.StartScan();
#endif
}
}

View File

@ -20,25 +20,17 @@ public class MainActivity : MauiAppCompatActivity
{
private NFCHelper? nfcHelper;
private MainViewModel mainViewModel;
/*private NfcAdapter? nfcAdapter;
private PendingIntent? pendingIntent;
private IntentFilter[]? intentFilters = [];
private string[][] techLict = new string[1][] { [typeof(NfcA).FullName] };*/
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// 初始化 NFC Helper
InitializeNFC();
/*nfcAdapter = NfcAdapter.GetDefaultAdapter(this);
pendingIntent = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(MainActivity)).AddFlags(ActivityFlags.ReceiverReplacePending), PendingIntentFlags.Mutable); */
}
private void InitializeNFC()
{
if (!TryInitializeNFC())
{
// 延迟到UI线程完全准备好后再尝试
Microsoft.Maui.Controls.Application.Current.Dispatcher.Dispatch(() =>
{
TryInitializeNFC();

View File

@ -34,5 +34,7 @@
</array>
<key>XSAppIconAssets</key>
<string>Assets.xcassets/appicon.appiconset</string>
<key>NFCReaderUsageDescription</key>
<string>此应用需要使用NFC功能来与智能锁设备通信</string>
</dict>
</plist>

View File

@ -28,5 +28,7 @@
</array>
<key>XSAppIconAssets</key>
<string>Assets.xcassets/appicon.appiconset</string>
<key>NFCReaderUsageDescription</key>
<string>此应用需要使用NFC功能来与智能锁设备通信</string>
</dict>
</plist>

View File

@ -5,6 +5,7 @@ namespace NfcLock.Ios.Binding
{
// @interface NFCManager
[DisableDefaultCtor]
[BaseType (typeof (NSObject))]
interface NFCManager
{
// +(void)startScan;

View File

@ -20,6 +20,7 @@
<ItemGroup>
<XcodeProject Include="NfcLockFramework/NfcLockFramework.xcodeproj">
<SchemeName>NfcLockFramework</SchemeName>
<Frameworks>CoreNFC NFCSDK</Frameworks>
<!-- Metadata applicable to @(NativeReference) will be used if set, otherwise the following defaults will be used:
<Kind>Framework</Kind>
<SmartLink>true</SmartLink>