2011-05-28 50 views
2

我正在通過可用範圍內的Bluetooth devices進行搜索。出於某種原因,每個找到的設備將被添加到ListView 兩次,當它只應顯示一次藍牙添加到ListView兩次而不是一次

有沒有人有任何想法我在做什麼錯在這裏?代碼如下。

當發現找到的設備

if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
    // Get the BluetoothDevice object from the Intent 
    BluetoothDevice device; 
    device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 

    // If it's already paired, skip it, because it's been listed already 
    if (device.getBondState() == BluetoothDevice.BOND_BONDED) { 
     Log.v("test", "test"); 

     mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
    } 

} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
    // When discovery is finished, change the Activity title 

    setProgressBarIndeterminateVisibility(false); 
    setTitle(R.string.select_device); 

    if (mNewDevicesArrayAdapter.getCount() == 0) { 
     String noDevices = getResources().getText(R.string.none_found).toString(); 
     mNewDevicesArrayAdapter.add(noDevices); 
    } 
} 

回答

4

在你的代碼使用equals操作。這就是爲什麼它增加了兩次。一個在已配對的設備列表中。並且在新發明的列表中再次添加相同的項目。試試這個代碼。

// When discovery finds a device 
     if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
      // Get the BluetoothDevice object from the Intent 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      // If it's already paired, skip it, because it's been listed already 
      if (device.getBondState() != BluetoothDevice.BOND_BONDED) { 
       mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
      } 
     // When discovery is finished, change the Activity title 
     } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
      setProgressBarIndeterminateVisibility(false); 
      setTitle(R.string.select_device); 
      if (mNewDevicesArrayAdapter.getCount() == 0) { 
       String noDevices = getResources().getText(R.string.none_found).toString(); 
       mNewDevicesArrayAdapter.add(noDevices); 
      } 
     } 
+1

然後它又添加了兩次,而不是一個...我檢查兩種方式,但得到相同的結果.... !!! == ==我應用!=這個,但沒有得到相同的結果...和感謝您的答覆 – RobinHood 2011-05-30 07:10:23