蓝牙设备发现和自定义列表适配器

时间:2016-11-21 11:14:30

标签: android bluetooth broadcastreceiver listadapter

我正在尝试检索可见蓝牙设备列表,并使用自定义适配器将收集的信息传递给ListView。 我的功能中唯一有问题的部分是将已发现的项目(设备)添加到ListView,在这里测试不同的方法时, ListView根本没有填充,项目被覆盖或项目在ListView中变成重复。 / p>

我的当前函数根本没有填充ListView:

public void startDevicesDiscovery(){

    rowItemsDevices.clear();
    devicesAdapter =new DevicesListAdapter(tContext, rowItemsDevices);

    //cancel discovery if discovering
    if (mBluetoothAdapter.isDiscovering()){
        mBluetoothAdapter.cancelDiscovery();
    }
    general.log("BT","start list size: "+rowItemsDevices.size());
    bluetoothReceiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {
            General general = new General(tContext);
            String action = intent.getAction();

            // if device found
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {

                btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                if(btDevice == null){
                    general.log("BT","device NULL");
                }else{

                    Device device = new Device(btDevice.getName(),btDevice.getAddress());

                    general.log("BT","device: "+device.getDeviceName()+"\ndevice MAC: "+device.getMacAddress());

                    RowItemDevices items = new RowItemDevices(device.getDeviceName(),device.getMacAddress());
                    rowItemsDevices.add(items);
                    devicesAdapter.notifyDataSetChanged();
                }

            }else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
                general.log("BT","device search finished");
                general.log("BT","end list size: "+rowItemsDevices.size());
            }

        }
    };

    visibleDevices.setAdapter(devicesAdapter);

    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(bluetoothReceiver, filter);

    mBluetoothAdapter.startDiscovery();

}

日志显示设备被正确发现: enter image description here

为了排除问题的原因在我的自定义ListView适配器本身,我创建了一个测试函数,证明向我的ListView添加元素正常工作:

public void testListView(){
    general.log("BT","testListView");
    rowItemsDevices.clear();
    devicesAdapter =new DevicesListAdapter(tContext, rowItemsDevices);
    RowItemDevices items = new RowItemDevices("test 1","test sub 1");
    rowItemsDevices.add(items);
    RowItemDevices items2 = new RowItemDevices("test 2","test sub 2");
    rowItemsDevices.add(items2
    );
    visibleDevices.setAdapter(devicesAdapter);
}

这里有什么问题吗?看起来函数startDevicesDiscovery按预期工作,但也许我正在以正确的方式实现它?

修改

在我的适配器上添加devicesAdapter.notifyDataSetChanged()后,列表在首次运行时显示正确的值,但如果我再次运行该功能,则项目将重复,因此这与我在开始时遇到的问题相同。通过查看日志,我可以看到问题是由BroadcastReceiver引起的,由于某种原因,在第二次搜索时,addz先前发现了新搜索的设备。怎么解决? enter image description here

1 个答案:

答案 0 :(得分:2)

  1. 要让ListView显示已添加的项目,您必须告诉ListVIew其中的内容已更新:yourAdapter.notifyDataSetChanged()后添加rowItemsDevices.add(items);。没有它ListView会更新,知道他已经有一些项目可以画了。
  2. 现在这些项目是重复的,因为您注册了新的BroadcastReceiver,并且没有取消注册旧的项目。如果您尝试第三次运行您的方法,您将获得3个复制项目。所以你必须在扫描结束后取消注册接收器:

    else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
        general.log("BT","device search finished");
        general.log("BT","end list size: "+rowItemsDevices.size());
        unregisterReceiver(this); //add this line
    }