iOS应用程序在Xamarin中后台时,如何获得蓝牙连接的通知?
我们需要我们的应用程序在从蓝牙音频设备(特别是他们的汽车中)连接或断开连接时从操作系统接收通知。当BT设备初始连接iOS应用程序在Xamarin中后台时,如何获得蓝牙连接的通知?
的应用程序得到通知,但随后立即似乎断开并记录错误:
BTCentralManager::DisconnectedPeripheral > SoundCore mini(ATT) ERROR Error Domain=CBErrorDomain Code=7 "The specified device has disconnected from us."
...和“DisconnectedPeripheral”事件实际上从未被解雇。
我们不确定如何在应用程序背景时简单地接收连接和断开连接事件。
我们是否需要将外设与中央管理器连接?我们只需要知道音频设备是否连接 - 我们不需要以任何方式与它进行交互。
在获得断开外围设备后,事件从不会从后台第二次调用。可能是因为我们收到了错误信息。
示例代码如下:
public class BTCentralManager : CBCentralManagerDelegate
{
public CBCentralManager centralManager;
public static CBPeripheral peripheral;
public BTCentralManager()
{
System.Diagnostics.Debug.WriteLine("BTCentralManager::Constructor > ");
centralManager = new CBCentralManager(this, new DispatchQueue("myqueue"),
new CBCentralInitOptions { ShowPowerAlert = true, RestoreIdentifier = "myRestoreIdentifier" });
NSUuid[] arr = { new NSUuid("7e9002be-547f-42bc-8d56-209736f70aa2") }; //Sound core mini
var devices = centralmanager.retrieveperipheralswithidentifiers(arr);
peripheral = devices.firstordefault();
//is the only way to trigger the events, we need to first connect the peripheral to central manager???
centralManager.connectPeripheral(peripheral, new PeripheralConnectionOptions
{
NotifyOnConnection = true,
NotifyOnDisconnection = true,
NotifyOnNotification = true
});
}
//Always is triggered inclusive in background
public override void UpdatedState(CBCentralManager central)
{
System.Diagnostics.Debug.WriteLine("BTCentralManager::UpdatedState > " + central.State.ToString());
}
//Only is triggered when the device is first time connected (Inclusive in background)
public override void ConnectedPeripheral(CBCentralManager central, CBPeripheral peripheral)
{
System.Diagnostics.Debug.WriteLine("BTCentralManager::ConnectedPeripheral > " + peripheral.Name);
//After the connect made successfully I receive this error, and never connect again to the device
//Error Domain=CBErrorDomain Code=7 "The specified device has disconnected from us."
}
public override void DisconnectedPeripheral(CBCentralManager central, CBPeripheral peripheral, NSError error)
{
System.Diagnostics.Debug.WriteLine("BTCentralManager::DisconnectedPeripheral > " + peripheral.Name + " ERROR>" + error.Description);
}
public override void DiscoveredPeripheral(CBCentralManager central, CBPeripheral peripheral, NSDictionary advertisementData, NSNumber RSSI)
{
System.Diagnostics.Debug.WriteLine("BTCentralManager::DiscoveredPeripheral > " + peripheral.Name);
// base.DiscoveredPeripheral(central, peripheral, advertisementData, RSSI);
}
}
据我理解你的问题,你需要观察的蓝牙音频设备的可用性。我在前几次进行了一些调查,结果并不令人满意。这里是我的结论:
1)CoreBluetooth只适用于蓝牙4.0 /蓝牙低功耗设备。许多蓝牙耳机甚至汽车收音机仍然不是蓝牙4.x.所以,除非你可以依靠你的用户的蓝牙音频设备为4.x,否则CoreBluetooth可能会浪费时间。
2.)当您的应用程序处于后台时,您的应用程序将不会收到有关正在连接的音频设备的通知。
到目前为止没有好处。但是,可能有一些方法可能有所帮助。
1.)通过使用CLLocationManager,您可以开始观察指南针(不是保存电池的位置)在手机移动时得到通知。当应用程序调用CLLocationManagerDelegate时,只需检查连接的音频设备。这当然不是非常有效,但它可能工作。
2.)使用iBeacons和CLBeaconRegions(如果可用)。将iBeacon放入用户的车内,开始观察信标。
我知道,这并不完全是你想听到的,但恐怕没有直接的解决方案来解决你的问题。
干杯, 彼得