Bluetooth добавлен в ListView дважды вместо одного - PullRequest
2 голосов
/ 28 мая 2011

Я ищу в диапазоне доступных Bluetooth devices.По какой-то причине каждое найденное устройство добавляется в 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);
   }
}

1 Ответ

4 голосов
/ 28 мая 2011

В вашем коде вы используете операцию равенства. вот почему это добавляет два раза. один в списке уже спаренных устройств. И те же самые предметы снова добавляются в новый список. Попробуйте этот код.

// 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);
            }
        }
...