2012-02-27 9 views
8

MACアドレスがわかっていても必ずしもペアリングされていない特定の数の特定のブルートゥースデバイスとの接続/切断をリッスンしたい(私はペアデバイスのユーザーのリストを混乱させたくない。私は彼らの存在を発見することに興味があり、彼らとコミュニケーションしていません。どの*どの* BluetoothデバイスがACTION_ACL_CONNECTEDブロードキャストを引き起こすかを特定しますか?

これは下のコードでうまく機能します。しかし、私の問題は、どの特定のデバイスが接続/切断されているかを知ることができないということです。何がアクションに関係するかをどのようにして知ることができますか?

まず、私は私の2つの特定の物理的なBluetoothデバイスのためのオブジェクトをインスタンス化し、私のインテントフィルタに追加します:

BluetoothDevice myPinkHeadset = mBluetoothAdapter.getRemoteDevice("18:17:0C:EB:9C:81"); 
    BluetoothDevice myPcBluetoothDongle = mBluetoothAdapter.getRemoteDevice("5A:7A:CC:4B:C5:08"); 

    IntentFilter intentFilter = new IntentFilter(); 
    intentFilter.addAction(myPinkHeadset.ACTION_ACL_CONNECTED); 
    intentFilter.addAction(myPinkHeadset.ACTION_ACL_DISCONNECTED); 
    intentFilter.addAction(myPcBluetoothDongle.ACTION_ACL_CONNECTED); 
    intentFilter.addAction(myPcBluetoothDongle.ACTION_ACL_DISCONNECTED); 

その後、私はそれらについてのブロードキャストを聞く:

final BroadcastReceiver intentReceiver = new BroadcastReceiver() { 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 

は今、私が見つけたいですどの人が接続されているか、または切断されているか、私はそれをどうやって行うことができないのか分かりません。

いずれか1)「BluetoothDevice」を直接使用します。それは放送にはうまく反応しますが、2つの物理デバイスのどちらがアクションに関係しているかはわかりません。彼らの発見方法は? Bluetooth.getName()は静的クラスではないため、許可されていません。

if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { 
     } 

または2)両方のデバイスで両方の処理を待ち受けます。

 if (myPinkHeadset .ACTION_ACL_CONNECTED.equals(action)) { 
      Log.v(TAG, "Connected to myPinkHeadset "); 
     } 
     else if (myPinkHeadset .ACTION_ACL_DISCONNECTED.equals(action)) { 
      Log.v(TAG, "Disconnected from myPinkHeadset "); 
     } 
     else if (myPcBluetoothDongle .ACTION_ACL_CONNECTED.equals(action)) { 
      Log.v(TAG, "Connected to myPcBluetoothDongle "); 
     } 
     else if (myPcBluetoothDongle .ACTION_ACL_DISCONNECTED.equals(action)) { 
      Log.v(TAG, "Disconnected from myPcBluetoothDongle "); 

しかし、それはそれは私が物理的にアクティブにmyPvBluetoothDongleそれであってもmyPinkHeadsetに接続することを記録します。それは常にifテストの最初に来るもののために行く。それは、アクション自体についてのみ関心があり、それがどのオブジェクトに関するものではない。

EXTRA_DEVICEが「このクラスによってブロードキャストされたすべてのインテントのParcelable BluetoothDevice追加フィールドとして使用されています」とわかりました。しかし、それだけで私にはnullを返します。

String extra = intent.getStringExtra(BluetoothDevice.EXTRA_DEVICE); 

答えて

14

これはに接続されたデバイスを与える:

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 

は初心者として、私はparcelable概念を誤解しました。 EXTRA_DEVICEは文字列ですが、オブジェクトの単なるタグです。したがって、BluetoothDeviceの個々のインスタンスを登録または聴く必要はありません。アクションがブロードキャストされると、インテントはそれがどの物理デバイスによって発生したかを示します。

+0

は、パーセルブルと同じミスをした、弦は混乱しています – NikkyD

1
intentFilter.addAction(myPinkHeadset.ACTION_ACL_CONNECTED); 
intentFilter.addAction(myPcBluetoothDongle.ACTION_ACL_CONNECTED); 

intentFilter.addAction(myPinkHeadset.ACTION_ACL_DISCONNECTED); 
intentFilter.addAction(myPcBluetoothDongle.ACTION_ACL_DISCONNECTED); 

を(私はこの:-Dのために自分を+1できます)と同じ値です。それは静的な値です。 BluetoothDevice.ACTION_ACL_CONNECTEDとBluetoothDeviceACTION_ACL_DISCONNECTED

private void register() { 
    context.registerReceiver(bluetoothBroadCast, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED)); 
    context.registerReceiver(bluetoothBroadCast, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED)); 
} 

private final BroadcastReceiver bluetoothBroadCast = new BroadcastReceiver() { 
@Override 
public void onReceive(Context context, Intent intent) { 
    String action = intent.getAction(); 
    switch (action) { 
     case BluetoothDevice.ACTION_ACL_CONNECTED: { 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      if(device.getAddress().equals(myPinkHeadset.getAddress)) { 
       //Do what you want 
      } 
      break; 
     } 

     case BluetoothDevice.ACTION_ACL_DISCONNECTED: { 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      break; 
     } 
    } 
} 

}。

私はこれがあなたを助けることを願っています

関連する問題