我将Siri快捷方式添加到我的应用程序中并利用语音命令
我进入调试器的错误是:
[[CoreBluetooth] API MISUSE:仅当在开机状态
我在这里讨论了很多与此问题相关的重复项,并且一致认为CBCentralManager
必须是我拥有的类级别变量。
我仍然无法在Siri语音命令或新的iOS 13快捷方式应用程序的上下文中使UpdatedState
委托方法执行多次。这意味着我的应用程序无法执行连续的功能。
请记住,它可以一次运行,然后,如果我再次打开该应用程序或重新运行调试会话,它将再次运行一次,但是由于从未调用UpdatedState
而停止了工作,>
这里可能会发生什么?如何使我的CBCentralManager
保持生命?
连接管理器:
public class BleConnectionManagerIos : CBCentralManagerDelegate { CBCentralManager centralManager; public BleConnectionManagerIos() { var dict = NSDictionary.FromObjectsAndKeys(new object[] { false }, new object[] { CBCentralManager.OptionShowPowerAlertKey }); this.centralManager = new CBCentralManager(this, null, dict); } //THIS METHOD IS ONLY EXECUTED ONE TIME ONLY public override void UpdatedState(CBCentralManager central) { if (central.State == CBCentralManagerState.PoweredOn) { //powered on } else { //not powered on } } }
Siri快捷方式意图处理程序:
[Register("IntentHandler")] public class IntentHandler : INExtension { public override NSObject GetHandler(INIntent intent) { if (intent is MyIntent) { return new MyIntentHandler(); } throw new Exception("Unhandled intent type: ${intent}"); } protected IntentHandler(IntPtr handle) : base(handle) { } }
意图处理程序:
public class MyIntentHandler : MyIntentHandling { AppExtensionViewModel viewModel; AppExtensionViewModel ViewModel { get { if (this.viewModel == null) { this.viewModel = new AppExtensionViewModel(); } return this.viewModel; } } public override void HandleTheIntent(MyIntent intent, Action<MyIntentResponse> completion) { this.ViewModel.DoSomething(); }
ViewModel:
public class AppExtensionViewModel
{
IBleConnectionManager BleConnectionManager;
public AppExtensionViewModel()
{
this.BleConnectionManager = new BleConnectionManagerIos();
}
}
[我在应用程序中添加了Siri快捷方式并利用语音命令,我在调试器中遇到的错误是:[CoreBluetooth] API误用:仅在开机时可以接受此命令...
我发现使与视图模型的连接静态化解决了这个问题。