2017-05-31 4 views
1

私はxamarinフォームアプリケーションを作成しました。私はiOSのブルートゥースの状態を確認したい。私は以下のコードを使用しましたが、if (state == CBCentralManagerState.PoweredOn)は私にUnknownを返しています。 Bluetooth状態の実際の状態を提供していません。誰かが何が間違っているのか理解してもらえますか?ありがとう。iOS上のXamarinフォームでBluetoothステータスを確認してください

このメソッドの参照はここにある:https://developer.xamarin.com/api/type/MonoMac.CoreBluetooth.CBCentralManagerState/

private CBCentralManagerState state; 

    public bool CheckBluetoothStatus() 
    { 
     bool status; 
     if (state == CBCentralManagerState.PoweredOn) 
     { 
      status= true; 
     } 
     else 
     { 
      status = false; 
     } 
     return status; 

    } 
+0

あなたのデバイスは何ですか? OSのバージョンは何ですか? CBCentralManagerオブジェクトを作成しましたか?また、チェックするためにhttps://developer.xamarin.com/api/member/MonoMac.CoreBluetooth.CBCentralManagerDelegate.UpdatedState/p/MonoMac.CoreBluetooth.CBCentralManager/が呼び出されるまで待ちます。 – Larme

+0

@Larmeそのiphone 6s。 – jones

+0

CBCentralManagerオブジェクトを作成しましたか? – Larme

答えて

0

あなたが実際にapple documentationにここで説明したように、あなたはあなたができる前にCBCentralManagerを初期化する必要がCBCentralManager

_mgr = new CBCentralManager(); 
_mgr.UpdatedState += CBCentralManager_UpdatedState; 
void CBCentralManager_UpdatedState(object sender, EventArgs e){ 
    switch (_mgr.State) 
} 
+0

より詳細な例を教えてください。 – jones

1

にUpdatedStateイベントを添字必要がありますこれを使って。

CBCentralManagerのXamarin Documentationを見ると、それらはすべてのコンストラクタのリストを提供します。 2番目を下から実装する

CBCentralManager(CBCentralManagerDelegate, DispatchQueue, CBCentralInitOptions) 

しかし、これを実装する前に、CBCentralManagerDelegateを実装する必要があります。今、あなたは、デリゲートを実装していることを、あなたは私がそのされて知っているこの

var bluetoothManager = new CBCentralManager(new CbCentralDelegate(),DispatchQueue.DefaultGlobalQueue, 
                 new CBCentralInitOptions{ShowPowerAlert = true}); 

return bluetoothManager.State == CBCentralManagerState.PoweredOn; 

ようCBCentralManagerからブルートゥースの状態を取得することができるはずです。この

public class CbCentralDelegate : CBCentralManagerDelegate 
{ 
    public override void UpdatedState(CBCentralManager central) 
    { 
     if(central.State == CBCentralManagerState.PoweredOn) 
     { 
      System.Console.WriteLine("Powered On"); 
     } 
    } 
} 

ような単純なものでしたしかし、これが尋ねられて以来、うまくいけば、これはあなたを助けます。(または他の誰か)

関連する問題