蓝牙搜索和列表视图问题增加 [英] Bluetooth searching and adding in the listview problem

查看:103
本文介绍了蓝牙搜索和列表视图问题增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找通过一系列可用的蓝牙设备。由于某些原因,每发现设备被添加到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);
            }
        }

这篇关于蓝牙搜索和列表视图问题增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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