ios库绑定
This commit is contained in:
parent
763c12f86e
commit
78243352b3
@ -64,6 +64,7 @@
|
|||||||
CommandParameter="Unlock"
|
CommandParameter="Unlock"
|
||||||
BackgroundColor="{Binding Operation, Converter={StaticResource OperationColorConverter}, ConverterParameter=UNLOCK}"
|
BackgroundColor="{Binding Operation, Converter={StaticResource OperationColorConverter}, ConverterParameter=UNLOCK}"
|
||||||
TextColor="Black"
|
TextColor="Black"
|
||||||
|
Clicked="UnlockClicked"
|
||||||
HorizontalOptions="Fill" />
|
HorizontalOptions="Fill" />
|
||||||
|
|
||||||
<!-- 关锁按钮 -->
|
<!-- 关锁按钮 -->
|
||||||
@ -73,6 +74,7 @@
|
|||||||
CommandParameter="Lock"
|
CommandParameter="Lock"
|
||||||
BackgroundColor="{Binding Operation, Converter={StaticResource OperationColorConverter}, ConverterParameter=LOCK}"
|
BackgroundColor="{Binding Operation, Converter={StaticResource OperationColorConverter}, ConverterParameter=LOCK}"
|
||||||
TextColor="Black"
|
TextColor="Black"
|
||||||
|
Clicked="LockClicked"
|
||||||
HorizontalOptions="Fill" />
|
HorizontalOptions="Fill" />
|
||||||
|
|
||||||
<!--~1~ 设置密码按钮 @1@
|
<!--~1~ 设置密码按钮 @1@
|
||||||
|
|||||||
@ -1,4 +1,9 @@
|
|||||||
using NFCLockDemoV2.ViewModels;
|
using NFCLockDemoV2.ViewModels;
|
||||||
|
|
||||||
|
#if IOS
|
||||||
|
using Foundation;
|
||||||
|
using NFCManager = NfcLock.Ios.Binding.NFCManager;
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace NFCLockDemoV2;
|
namespace NFCLockDemoV2;
|
||||||
|
|
||||||
@ -6,10 +11,80 @@ public partial class MainPage : ContentPage
|
|||||||
{
|
{
|
||||||
private MainViewModel _mainMainViewModel;
|
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)
|
public MainPage(MainViewModel mainViewModel)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
_mainMainViewModel = mainViewModel;
|
_mainMainViewModel = mainViewModel;
|
||||||
BindingContext = _mainMainViewModel;
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -20,25 +20,17 @@ public class MainActivity : MauiAppCompatActivity
|
|||||||
{
|
{
|
||||||
private NFCHelper? nfcHelper;
|
private NFCHelper? nfcHelper;
|
||||||
private MainViewModel mainViewModel;
|
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)
|
protected override void OnCreate(Bundle savedInstanceState)
|
||||||
{
|
{
|
||||||
base.OnCreate(savedInstanceState);
|
base.OnCreate(savedInstanceState);
|
||||||
// 初始化 NFC Helper
|
|
||||||
InitializeNFC();
|
InitializeNFC();
|
||||||
/*nfcAdapter = NfcAdapter.GetDefaultAdapter(this);
|
|
||||||
pendingIntent = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(MainActivity)).AddFlags(ActivityFlags.ReceiverReplacePending), PendingIntentFlags.Mutable); */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void InitializeNFC()
|
private void InitializeNFC()
|
||||||
{
|
{
|
||||||
if (!TryInitializeNFC())
|
if (!TryInitializeNFC())
|
||||||
{
|
{
|
||||||
// 延迟到UI线程完全准备好后再尝试
|
|
||||||
Microsoft.Maui.Controls.Application.Current.Dispatcher.Dispatch(() =>
|
Microsoft.Maui.Controls.Application.Current.Dispatcher.Dispatch(() =>
|
||||||
{
|
{
|
||||||
TryInitializeNFC();
|
TryInitializeNFC();
|
||||||
|
|||||||
@ -34,5 +34,7 @@
|
|||||||
</array>
|
</array>
|
||||||
<key>XSAppIconAssets</key>
|
<key>XSAppIconAssets</key>
|
||||||
<string>Assets.xcassets/appicon.appiconset</string>
|
<string>Assets.xcassets/appicon.appiconset</string>
|
||||||
|
<key>NFCReaderUsageDescription</key>
|
||||||
|
<string>此应用需要使用NFC功能来与智能锁设备通信</string>
|
||||||
</dict>
|
</dict>
|
||||||
</plist>
|
</plist>
|
||||||
|
|||||||
@ -28,5 +28,7 @@
|
|||||||
</array>
|
</array>
|
||||||
<key>XSAppIconAssets</key>
|
<key>XSAppIconAssets</key>
|
||||||
<string>Assets.xcassets/appicon.appiconset</string>
|
<string>Assets.xcassets/appicon.appiconset</string>
|
||||||
|
<key>NFCReaderUsageDescription</key>
|
||||||
|
<string>此应用需要使用NFC功能来与智能锁设备通信</string>
|
||||||
</dict>
|
</dict>
|
||||||
</plist>
|
</plist>
|
||||||
|
|||||||
@ -5,6 +5,7 @@ namespace NfcLock.Ios.Binding
|
|||||||
{
|
{
|
||||||
// @interface NFCManager
|
// @interface NFCManager
|
||||||
[DisableDefaultCtor]
|
[DisableDefaultCtor]
|
||||||
|
[BaseType (typeof (NSObject))]
|
||||||
interface NFCManager
|
interface NFCManager
|
||||||
{
|
{
|
||||||
// +(void)startScan;
|
// +(void)startScan;
|
||||||
|
|||||||
@ -20,6 +20,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<XcodeProject Include="NfcLockFramework/NfcLockFramework.xcodeproj">
|
<XcodeProject Include="NfcLockFramework/NfcLockFramework.xcodeproj">
|
||||||
<SchemeName>NfcLockFramework</SchemeName>
|
<SchemeName>NfcLockFramework</SchemeName>
|
||||||
|
<Frameworks>CoreNFC NFCSDK</Frameworks>
|
||||||
<!-- Metadata applicable to @(NativeReference) will be used if set, otherwise the following defaults will be used:
|
<!-- Metadata applicable to @(NativeReference) will be used if set, otherwise the following defaults will be used:
|
||||||
<Kind>Framework</Kind>
|
<Kind>Framework</Kind>
|
||||||
<SmartLink>true</SmartLink>
|
<SmartLink>true</SmartLink>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user