【问题标题】:My android app is detecting the same bluetooth device multiple times我的 android 应用程序多次检测到同一个蓝牙设备
【发布时间】:2012-07-31 15:00:40
【问题描述】:

我正在使用来自 Android 开发者网站的代码来检测范围内的蓝牙设备,并将它们添加到 ArrayAdapter。问题是,每个设备都被添加到 ArrayAdapter 5-6 次。现在,我只是使用这里的代码:http://developer.android.com/guide/topics/connectivity/bluetooth.html#DiscoveringDevices

这是我所拥有的:

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();      
mBluetoothAdapter.startDiscovery();

final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

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

            // Add the name and address to an array adapter to show in a ListView
            mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
        }
    }
};

知道是什么原因造成的吗?我该怎么做才能让一个设备只添加到 ArrayAdapter 一次,而不是 5 次?

【问题讨论】:

    标签: java android bluetooth broadcastreceiver


    【解决方案1】:

    我不确定这是错误还是什么,但我在某些设备上也遇到过这种情况。为了解决这个问题,只需在List 中添加一次找到的设备并进行一些检查。见下文:

    private List<BluetoothDevice> tmpBtChecker = new ArrayList<BluetoothDevice>();
    
        final BroadcastReceiver mReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
    
                // When discovery starts    
                if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
                    //clearing any existing list data
                    tmpBtChecker.clear();
                }
    
                // 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);
    
                    // Add the name and address to an array adapter
                    if(!tmpBtChecker.contains(device)){
                       tmpBtChecker.add(device);
                       mArrayAdapter.add(device.getName()+"\n"+device.getAddress());
                    }
                }
            }
        };
    

    【讨论】:

    • 我之前确实有过这个。但有人告诉我,这并不完全是一个“优雅”的解决方案。您认为这可能只是设备特定的错误吗?我希望我有另一个设备来测试它......
    • 我通常在三星设备上遇到这个问题。我认为这是一个优雅的解决方案,否则你打算如何解决这个问题?
    • 我会建议检查它是否已经在列表中,但这似乎更好。
    • 您可以通过使用 HashSet 来提高效率。 List.Contains 是大 O 表示法 (O)n,而 HashSet 是 (O)1。即 HashSet 更有效,因为不需要您遍历整个列表。 private Set&lt;BluetoothDevice&gt; foundDevices = new HashSet&lt;&gt;(); .... if(foundDevices.contains(device)){ return; } foundDevices.add(device);
    • 这并不能解决问题。它只是一种解决方法。这个答案没有帮助。
    猜你喜欢
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2013-07-08
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多