CoreBluetooth API MISUSE-iOS 13中的CBCentralManager问题,例如Siri快捷方式 [英] CoreBluetooth API MISUSE - Issues with CBCentralManager in iOS 13 features like Siri Shortcuts
问题描述
我将Siri快捷方式添加到了我的应用程序中,并利用了语音命令
I added Siri Shortcuts to my application and to utilize voice commands
我进入调试器的错误是:
The error Im getting in the debugger is:
[CoreBluetooth] API MISUSE:仅在 开机状态
[CoreBluetooth] API MISUSE: can only accept this command while in the powered on state
我已经在此处讨论了很多重复的问题,共识是CBCentralManager
必须是我拥有的类级别变量.
I have looked at a vast number of duplicates of this for issue on here and the consensus is that CBCentralManager
needs to be a class level variable, which I have.
在Siri语音命令或新的iOS 13快捷方式应用程序的上下文中,我仍然无法获得UpdatedState
委托方法来执行多次.这意味着我的应用程序无法执行连续的功能.
I still cannot get UpdatedState
delegate method to execute more than one time in the context of Siri Voice Commands or the new iOS 13 shortcuts app. This means my application cannot do consecutive functions.
请记住,它可以一次运行,然后,如果我再次打开该应用程序或重新运行调试会话,它将再次运行一次,但是由于从未调用UpdatedState
而停止了工作
Keep in mind, it works ONCE, and then if I open the app again or re-run the debug session, it will work ONCE again, but then stop working because UpdatedState
is never called
这可能是怎么回事?我如何保持CBCentralManager
的生命?
What could be going on here? How can I keep my CBCentralManager
alive?
连接管理器:
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快捷方式意图处理程序:
Siri Shortcuts Intent Handler:
[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:
ViewModel:
public class AppExtensionViewModel
{
IBleConnectionManager BleConnectionManager;
public AppExtensionViewModel()
{
this.BleConnectionManager = new BleConnectionManagerIos();
}
}
推荐答案
我发现使对我的视图模型的依赖成为静态问题可以解决此问题.
I found that making the hook to my view model static solved the issue.
我想siri快捷方式不喜欢多次创建CBCentralManger.但是我发现这很奇怪,因为这种逻辑在带有TodayViewController的普通iOS小部件的上下文中可以正常工作
I guess siri shortcuts does not like to create CBCentralManger more than once. I find this strange however, because this logic works fine in the context of an ordinary iOS widget with a TodayViewController
public class MyIntentHandler : MyIntentHandling
{
static AppExtensionViewModel viewModel; //I needed to make this static
AppExtensionViewModel ViewModel
{
get
{
if (this.viewModel == null)
{
this.viewModel = new AppExtensionViewModel();
}
return this.viewModel;
}
}
这篇关于CoreBluetooth API MISUSE-iOS 13中的CBCentralManager问题,例如Siri快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!