【问题标题】:CoreBluetooth API MISUSE - Issues with CBCentralManager in iOS 13 features like Siri ShortcutsCoreBluetooth API MISUSE - iOS 13 中的 CBCentralManager 问题,如 Siri Shortcuts
【发布时间】:2020-01-31 20:33:54
【问题描述】:

我在我的应用程序中添加了 Siri 快捷方式并使用语音命令

我在调试器中遇到的错误是:

[CoreBluetooth] API MISUSE:只能在 开机状态

我在这里查看了大量重复的问题,一致认为CBCentralManager 需要是一个类级别的变量,我有。

我仍然无法让 UpdatedState 委托方法在 Siri 语音命令或新的 iOS 13 快捷方式应用程序的上下文中执行多次。这意味着我的应用程序无法执行连续功能。

请记住,它运行一次,然后如果我再次打开应用程序或重新运行调试会话,它将再次运行一次,但随后停止运行,因为从未调用过 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 快捷方式 Intent 处理程序:

[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();
}

视图模型:

public class AppExtensionViewModel
{
   IBleConnectionManager BleConnectionManager; 

   public AppExtensionViewModel()
   {
     this.BleConnectionManager = new BleConnectionManagerIos();
   }
}

【问题讨论】:

  • Allan Ritchie,Backgrounding 大师创建了 Shiny 库 github.com/shinyorg/shiny/blob/… 尝试通过 twitter 或其他方式联系他
  • 我猜你已经在 info.plist 下添加了蓝牙背景模式

标签: ios xamarin core-bluetooth ios13 sirishortcuts


【解决方案1】:

我发现将我的视图模型的钩子设为静态解决了这个问题。

我猜 siri 快捷方式不喜欢多次创建 CBCentralManger。然而我觉得这很奇怪,因为这个逻辑在带有 TodayViewController 的普通 iOS 小部件的上下文中运行良好

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;
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-18
    • 1970-01-01
    • 2019-06-18
    • 2016-04-22
    • 2020-03-12
    • 2020-05-01
    • 1970-01-01
    • 2018-12-25
    相关资源
    最近更新 更多