蓝牙添加到ListView两次,而不是一次 [英] Bluetooth added to the ListView twice instead once

查看:235
本文介绍了蓝牙添加到ListView两次,而不是一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过现有的蓝牙设备在范围内搜索。出于某种原因,每发现设备被添加到ListView 两次,当时只能表明它的一次

有没有人有任何想法,我做错了什么? code包含下面。


  

当发现找到的设备


 如果(BluetoothDevice.ACTION_FOUND.equals(动作)){
   //从意向获取BluetoothDevice类对象
   BluetoothDevice类设备;
   设备= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);   //如果它已经配对,跳过它,因为它已经被列入
   如果(device.getBondState()== BluetoothDevice.BOND_BONDED){
      Log.v(测试,测试);      mNewDevicesArrayAdapter.add(device.getName()+\\ n+ device.getAddress());
   }}否则如果(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(动作)){
   //当发现完成后,更改活动标题   setProgressBarIndeterminateVisibility(假);
   的setTitle(R.string.select_device);   如果(mNewDevicesArrayAdapter.getCount()== 0){
      。字符串=使用nodevices getResources()的getText(R.string.none_found)的ToString();
      mNewDevicesArrayAdapter.add(nodevices选项);
   }
}


解决方案

在您的code使用equals操作。这就是为什么它增加了两倍。之一,已经配对的设备列表。和相同的项目的int新发明列表再次添加。试试这个code。

  //当发现找到的设备
        如果(BluetoothDevice.ACTION_FOUND.equals(动作)){
            //从意向获取BluetoothDevice类对象
            BluetoothDevice类设备= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            //如果它已经配对,跳过它,因为它已经被列入
            如果(device.getBondState()!= BluetoothDevice.BOND_BONDED){
                mNewDevicesArrayAdapter.add(device.getName()+\\ n+ device.getAddress());
            }
        //当发现完成后,更改活动标题
        }否则如果(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(动作)){
            setProgressBarIndeterminateVisibility(假);
            的setTitle(R.string.select_device);
            如果(mNewDevicesArrayAdapter.getCount()== 0){
                。字符串=使用nodevices getResources()的getText(R.string.none_found)的ToString();
                mNewDevicesArrayAdapter.add(nodevices选项);
            }
        }

I'm searching through available Bluetooth devices in range. For some reason, each found device is added to the ListView twice, when it should only be shown once.

Does anyone have any idea what I am doing wrong here? Code included below.

When discovery finds a device

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

解决方案

In your code you use the equals operation. that's why it adds the two times. one in already paired device list. And the same items are added again int the newly invented list. Try this code.

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

这篇关于蓝牙添加到ListView两次,而不是一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆