排序传入的蓝牙设备

时间:2018-10-31 16:57:18

标签: java android bluetooth-lowenergy

每次我扫描设备时,列表都会显示相同的〜10个设备,它们看起来似乎是随机的。我没有做任何确保它们一旦出现就进行的尝试。有什么想法吗?

 private final no.nordicsemi.android.support.v18.scanner.ScanCallback mScanCallback = new no.nordicsemi.android.support.v18.scanner.ScanCallback() {
    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        super.onScanResult(callbackType, result);

        Log.i("onScanResult", "device detected");

            device = result.getDevice();
            String deviceName = device.getName();
            String deviceAddress = device.getAddress();

            Log.i(DEVICE, "Scanned device: " + device.toString());
            Log.i(DEVICE, "Scanned device name: " + deviceName);
            Log.i(DEVICE, "Scanned device address: " + deviceAddress);


        deviceShowFormat deviceObj;

//            if(foundDevices.size() == 0){
//                foundDevices.add(new deviceShowFormat(device, deviceName, deviceAddress));
//                BTadapter.notifyDataSetChanged();
//            }
        boolean isThere = false;
        for (int i=0; i<30; i++)
        {
            try {
                deviceObj = foundDevices.get(i);

                Log.i("Sorting1", "" + i);
                if (deviceObj.get_device_address() == deviceAddress){
                    isThere = true;
                }
            } catch(Exception e){}

        }

        if(!isThere){
            foundDevices.add(new deviceShowFormat(device, deviceName, deviceAddress));
            BTadapter.notifyDataSetChanged();

            isThere = false;
        }


    }
};

1 个答案:

答案 0 :(得分:0)

现在,我知道我不是Java或android java sdk的专家,但这很简单,不是吗? 您可以创建无序集合/字典并添加每个名称。如果名称在列表中,请不要再添加。使用集合对于时间复杂度更好。查看清单将是O(n)。