2012-03-23 5 views
0

他のBluetoothデバイスを検出すると、検出された各デバイスに対して2つのブロードキャストが送信されます。最初のスキャンはスキャン中に送信され、スキャンが終了すると、見つかったすべてのデバイスのブロードキャストが一度に送信されます。私はSDKのBluetoothChatサンプルを適合させています。Android Bluetoothディスカバリー

private final BroadcastReceiver foundRec = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     if (action.equals(BluetoothDevice.ACTION_FOUND)) { 

      Log.e(TAG, "--- device found ---"); 

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

      if (dev.getBondState() == BluetoothDevice.BOND_BONDED) { 
       availableDevices.add(dev.getName() + " (paired)"); 
      } else { 
       availableDevices.add(dev.getName()); 
      } 

     } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)){ 

      Log.d(TAG, "DISCOVERY STARTED"); 
      findViewById(R.id.lookup).setVisibility(View.VISIBLE); 

     } 
    } 
}; 

ありがとう:

は、ここに私の 'BroadcastReceiver' です!

+0

私はあなたがSamsungデバイス、儀式であなたのアプリをテストしていると思いますか? – waqaslam

+0

多分私はそれを逃したが、あなたの質問は何ですか? –

+0

私はHTCでテストしています。問題は、どのデバイスにも2回見つけられないようにすることです。 –

答えて

1

私はデバイスの配列を保持しています。 ACTION_FOUNDが受信されるたびに、私はそれが存在するかどうかを確認するためにデバイス配列を調べます。私の構文はブラウザに入力されていないかもしれませんが、うまくいけば、そのアイデアを得ることができます。

availableDevices配列の用途はわかりませんが、文字列配列ではなくBluetoothDevice配列の方が便利です。あなたはいつでも名前を取得し、onReceiveの外でボンド状態をチェックすることができます。

private final BroadcastReceiver foundRec = new BroadcastReceiver() { 
List<BluetoothDevice> BtDevices = new ArrayList<BluetoothDevice>(); 
@Override 
public void onReceive(Context context, Intent intent) { 
    String action = intent.getAction(); 
    if (action.equals(BluetoothDevice.ACTION_FOUND)) { 

     Log.e(TAG, "--- device found ---"); 

     BluetoothDevice dev = intent 
       .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
     if(!IsBtDevPresent(dev)) { 
      BtDevices.add(dev); 
      if (dev.getBondState() == BluetoothDevice.BOND_BONDED) { 
       availableDevices.add(dev.getName() + " (paired)"); 
      } else { 
       availableDevices.add(dev.getName()); 
      } 
     } 
    } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)){ 

     Log.d(TAG, "DISCOVERY STARTED"); 
     findViewById(R.id.lookup).setVisibility(View.VISIBLE); 

    } 
} 

private boolean IsBtDevPresent(BluetoothDevice dev) { 
    int size = BtDevices.size(); 
    for(int i = 0; i<size; i++) { 
     if(BtDevices.get(i).equals(dev)) { 
      return true; 
     } 
    } 
    return false; 
} 

};

+1

あなたの答えは真実ですが、BTAdapterがすべてのデバイスに対して一度だけブロードキャストを送信させる可能性はありませんか?これらの文字列でArrayAdapterを塗りつぶして、ListViewで使用可能なデバイスを表示します。 –

0

実際、ICS以上のデバイスは、照会スキャン用とページスキャン用の2つのブロードキャストを送信します。なぜ我々は二度受け取っているのですか?

しかし、同じコードを2.3.5デバイスでテストしました。このデバイスでは、1回のブロードキャスト受信しか受信できませんでした。どのように管理していますか? 私たちが必要とするのは、個別の放送のための質問とページまたは単一の放送!誰でもこれについて話すことができます!