问题
I added Siri Shortcuts to my application and to utilize voice commands
The error Im getting in the debugger is:
[CoreBluetooth] API MISUSE: can only accept this command while in the powered on state
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.
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.
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
What could be going on here? How can I keep my CBCentralManager
alive?
Connection Manager:
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 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) { }
}
Intent Handler:
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();
}
}
回答1:
I found that making the hook to my view model static solved the issue.
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;
}
}
来源:https://stackoverflow.com/questions/58203613/corebluetooth-api-misuse-issues-with-cbcentralmanager-in-ios-13-features-like