扫描蓝牙设备时,我尝试通过MAC地址捕获ArrayList中的每个唯一设备。之后我想连接到列表中最强的信号RSSI。出于某种原因,RSSI在下面的代码中不断更新。数据放入ArrayList的唯一时间是MAC未包含在另一个列表中的时间。Android/Java - 在无法访问ArrayList的情况下更新ArrayList中的Bluetooth RSSI
基本上,我检查我扫描的MAC地址与列表中的内容,如果它不在列表中,我将设备信息添加到不同的ArrayList中。
为什么RSSI仍然在deviceArray ArrayList中更新?
// 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中。有什么想法吗?