2017-08-10 46 views
5

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

答えて

0

限り、私はあなたの質問を理解して、あなたは、Bluetoothオーディオデバイスの可用性を観察する必要があります。私は数時間前にこれについていくつか調査しましたが、その結果は私を本当に満足させるものではありませんでした。ここで私の結論です:

1.)CoreBluetoothは、Bluetooth 4.0/Bluetooth Low Energyデバイス専用です。多くのBluetoothヘッドフォンやカーラジオはまだBluetooth 4.xではありません。したがって、ユーザーのBluetoothオーディオデバイスを4.xにすることができない限り、CoreBluetoothは時間の無駄です。

2.)あなたのアプリは、あなたのアプリがバックグラウンドのときに接続されているオーディオデバイスについて通知されません。

これまでのところ良いことはありません。しかし、いくつかのアプローチが役立つかもしれません。

1.)CLLocationManagerを使用すると、たとえば次のような観測を開始できます。電話が動かされるたびに通知を受け取るコンパス(バッテリーを保管する場所ではない)。アプリケーションがCLLocationManagerDelegateを呼び出しているときに接続されているオーディオデバイスを確認するだけです。これはあまり効率的ではありませんが、うまくいくかもしれません。

2.)可能であれば、iBeaconsとCLBeaconRegionsを使用します。ユーザーの車にiBeaconを置き、ビーコンの観測を開始します。

私は知っている、これはあなたが聞きたいものではありませんが、私はあなたの問題のための直接的な解決策はないと思います。

声援 Peter

関連する問題