Android/Java - ArrayList 中的蓝牙 RSSI 更新而无需访问它

Android/Java - Bluetooth RSSI updating in ArrayList without access to it

扫描蓝牙设备时,我试图通过 MAC 地址捕获 ArrayList 中的每个唯一设备。之后我想连接到列表中最强的信号 RSSI。由于某种原因,RSSI 在下面的代码中不断更新。只有当 MAC 不包含在另一个列表中时才会将数据放入 ArrayList。

基本上,我会根据列表中的内容检查我扫描的 mac 地址,如果它不在该列表中,我会将设备信息添加到不同的 ArrayList 中。

为什么 deviceArray ArrayList 中的 RSSI 还在更新??

// Device Discovery
private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
    @Override
    public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {

        if (device.getName() != null){
            if (DEVICE_NAME.equals(device.getName())){

                deviceInfo[0] = device.getName();
                deviceInfo[1] = device.getAddress();
                deviceInfo[2] = Integer.toString(rssi);

                // Add device info if MAC address is not located
                if (!MACAddresses.contains(deviceInfo[1])){
                    deviceArray.add(deviceInfo);
                    MACAddresses.add(deviceInfo[1]);
                    Log.e("MAC ADDRESS ADDING", "ADDING " + deviceInfo[1]);
                }

                // TODO remove this logging info
                for(int i = 0; i < deviceArray.size(); i++){
                    Log.e("Bluetooth Devices",
                            "Array Length = " + deviceArray.size() + ", "
                            + deviceArray.get(i)[0] + ", "
                            + deviceArray.get(i)[1] + ", "
                            + deviceArray.get(i)[2]);
                }
            }
        }

现在,请看下面的输出。 ArrayList 大小为 2,我们只有 2 MAC 个地址(我们应该这样做),但 RSSI 会不时发生变化。请注意,有 3 个 RSSI 值(-41、-47 和 -48),而应该只有 2 个值。最重要的是,这些 ArrayList 项目没有一个接一个地打印出来,这对我来说似乎很奇怪。如果一个 MAC 地址是 A,另一个是 B,它们应该打印为 AB,AB,AB,AB,但它们更像是 AB BA AB BA,并且在某一点上有一个 AAAA。

E/MAC ADDRESS ADDING: ADDING 00:A0:50:CC:4B:9B
E/Bluetooth Devices: Array Length = 1, MyDeviceName, 00:A0:50:CC:4B:9B, -41
E/MAC ADDRESS ADDING: ADDING 00:A0:50:12:14:96
E/Bluetooth Devices: Array Length = 2, MyDeviceName, 00:A0:50:12:14:96, -48
E/Bluetooth Devices: Array Length = 2, MyDeviceName, 00:A0:50:12:14:96, -48
E/Bluetooth Devices: Array Length = 2, MyDeviceName, 00:A0:50:CC:4B:9B, -41
E/Bluetooth Devices: Array Length = 2, MyDeviceName, 00:A0:50:CC:4B:9B, -41
E/Bluetooth Devices: Array Length = 2, MyDeviceName, 00:A0:50:12:14:96, -48
E/Bluetooth Devices: Array Length = 2, MyDeviceName, 00:A0:50:12:14:96, -48
E/Bluetooth Devices: Array Length = 2, MyDeviceName, 00:A0:50:CC:4B:9B, -41
E/Bluetooth Devices: Array Length = 2, MyDeviceName, 00:A0:50:CC:4B:9B, -41
E/Bluetooth Devices: Array Length = 2, MyDeviceName, 00:A0:50:12:14:96, -47
E/Bluetooth Devices: Array Length = 2, MyDeviceName, 00:A0:50:12:14:96, -48
E/Bluetooth Devices: Array Length = 2, MyDeviceName, 00:A0:50:CC:4B:9B, -41
E/Bluetooth Devices: Array Length = 2, MyDeviceName, 00:A0:50:CC:4B:9B, -41
E/Bluetooth Devices: Array Length = 2, MyDeviceName, 00:A0:50:CC:4B:9B, -41
E/Bluetooth Devices: Array Length = 2, MyDeviceName, 00:A0:50:CC:4B:9B, -41
E/Bluetooth Devices: Array Length = 2, MyDeviceName, 00:A0:50:12:14:96, -47

在我没有粘贴的输出的下方,我实际上看到了更多的 RSSI 值(-58 和 -53)。我只是不明白如果我不在每个 MAC 中添加一次项目,这些如何才能进入 ArrayList。有什么想法吗?

我想通了。我不擅长内存位置,所以我在代码中犯了一个错误。在 class 变量中,我创建了一个字符串数组:

String[] deviceInfo = new String[3];

但是,此方法使用相同的内存位置并且能够覆盖数据。此外,我的设备阵列实际上存储了 2 个相同的 MAC 地址,这也没有帮助。这就是我后来发现问题的方式。

无论如何,我必须在每个循环的回调方法中声明一个新的 String 数组才能获得新的内存位置。