2011-05-28 18 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