Bluetoothオーディオデバイス(具体的には車内のもの)からの接続や切断時に、OSからの通知を受け取るには、アプリケーションが必要です。 BTデバイスが最初に接続したときにiOSアプリがXamarinでバックグラウンドされているときに、Bluetooth接続を通知するにはどうすればよいですか?
アプリが通知され、その後すぐに切断するようだと、エラーをログに記録します。
BTCentralManager::DisconnectedPeripheral > SoundCore mini(ATT) ERROR Error Domain=CBErrorDomain Code=7 "The specified device has disconnected from us."
...と「DisconnectedPeripheral」イベントが実際に発射されることはありません。
アプリがバックグラウンドになっているときに接続や切断イベントを受信する方法がわかりません。
周辺機器を中央マネージャに接続する必要がありますか?オーディオデバイスが接続されているかどうかを知る必要があります。何らかの方法でオーディオデバイスと通信する必要はありません。
イベントは、切断ペリフェラルを取得した後、バックグラウンドから2度目にコールすることはありません。おそらく、私たちが受け取っているエラーメッセージのためです。以下
サンプルコード:
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);
}
}