2012-03-23 3 views
0

다른 블루투스 장치를 검색 할 때 발견 된 각 장치에 대해 2 개의 브로드 캐스트가 전송됩니다. 첫 번째는 스캔하는 동안 전송되고 한 번에 검색된 모든 장치에 대해 브로드 캐스트가 전송됩니다. SDK의 BluetoothChat 샘플을 적용하고 있습니다.Android 블루투스 검색

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

     } 
    } 
}; 

감사합니다 :

여기 내 '브로드 캐스트 리시버'입니다!

+0

삼성 기기에서 앱을 테스트하고 있다고 생각하십니까? – waqaslam

+0

어쩌면 나는 그것을 놓쳤다. 그러나 당신의 질문은 무엇인가? –

+0

HTC에서 테스트 중입니다. 질문은 모든 장치가 두 번 발견되는 것을 피하는 방법입니다. –

답변

1

장치 배열을 유지합니다. ACTION_FOUND가 수신 될 때마다 장치 배열을 통해 그것이 있는지 확인합니다. 내 구문은 브라우저에 입력 한대로 올바르지 않을 수 있지만 잘하면 U 아이디어를 얻을.

availableDevices 배열을 어떤 용도로 사용할지 모르겠지만 String 배열 대신 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가 모든 장치에 대해 한 번만 브로드 캐스트를 전송할 수있는 가능성은 없습니까? ListAdapter에서 사용 가능한 장치를 표시하기 위해 이러한 문자열로 ArrayAdapter를 채 웁니다. –

0

실제로 ICS 이상의 장치는 조회 스캔 용과 기타 페이지 스캔 용으로 두 개의 브로드 캐스트를 전송합니다. 그게 우리가 두 번받는 이유 !!

그러나 2.3.5 장치에서 동일한 코드를 테스트했는데 하나의 브로드 캐스트 수신 만 받았습니다! 어떻게 관리합니까? 우리가 요구하는 것은 개별 방송으로 조회 및 페이지 또는 단일 방송 !! 누구든지이 말을 할 수 있어요 !!