英文:
How to get a list after each discovered device?
问题
以下是翻译好的内容:
我正在使用这个函数扫描BLE设备并将它们保存到一个列表中。
public BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
act.runOnUiThread(new Runnable() {
public void run() {
if (aDevices.size() > 0) {
boolean isNewItem = true;
for (int i = 0; i < aDevices.size(); i++) {
if (aDevices.get(i).getMacAddress().equals(device.getAddress())) {
isNewItem = false;
}
}
if (isNewItem) {
aDevices.add(new BluetoothLE(device.getName(), device.getAddress(), rssi, device));
}
} else {
aDevices.add(new BluetoothLE(device.getName(), device.getAddress(), rssi, device));
}
}
});
}
};
并且使用这个函数来获取列表:
public ArrayList<BluetoothLE> getListDevices(){
return aDevices;
}
但是,我想在线程内的每次aDevices.add()
之后获取列表。我应该怎么做?
英文:
I'm using this function to scan BLE devices and save then into a list.
public BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
act.runOnUiThread(new Runnable() {
public void run() {
if(aDevices.size() > 0) {
boolean isNewItem = true;
for (int i = 0; i < aDevices.size(); i++) {
if (aDevices.get(i).getMacAddress().equals(device.getAddress())) {
isNewItem = false;
}
}
if(isNewItem) {
aDevices.add(new BluetoothLE(device.getName(), device.getAddress(), rssi, device));
}
}else{
aDevices.add(new BluetoothLE(device.getName(), device.getAddress(), rssi, device));
}
}
});
}
};
And using this function to get a a list:
public ArrayList<BluetoothLE> getListDevices(){
return aDevices;
}
But, I want to get the list after each aDevices.add()
inside the thread. What should I do?
答案1
得分: 1
你可以使用 ObservableArrayList 的实例,并为其提供一个 OnListChangedCallback 的实现,该实现会在每次更改时得到通知,而不是使用你当前正在使用的 ArrayList。
public class MyCallback extends ObservableList.OnListChangedCallback<ObservableList> {
public void onChanged(ObservableList sender) {
Log.i(tag, "list changed");
}
public void onItemRangeChanged(ObservableList sender, int positionStart, int itemCount) {
Log.i(tag, "item range changed");
}
public void onItemRangeInserted(ObservableList sender, int positionStart, int itemCount) {
Log.i(tag, "item range inserted");
}
public void onItemRangeMoved(ObservableList sender, int fromPosition, int toPosition, int itemCount) {
Log.i(tag, "item range moved");
}
public void onItemRangeRemoved(ObservableList sender, int positionStart, int itemCount) {
Log.i(tag, "item range removed");
}
}
ObservableArrayList list = new ObservableArrayList();
list.addOnListChangedCallback(new MyCallback());
...
list.add(element);
英文:
You could use an instance of ObservableArrayList and provide it with an implementation of OnListChangedCallback that is notified on each change instead of the ArrayList you are currently using.
public class MyCallback extends ObservableList.OnListChangedCallback<ObservableList> {
public void onChanged(ObservableList sender) {
Log.i(tag, "list changed");
}
public void onItemRangeChanged(ObservableList sender, int positionStart, int itemCount) {
Log.i(tag, "item range changed");
}
public void onItemRangeInserted(ObservableList sender, int positionStart, int itemCount) {
Log.i(tag, "item range inserted");
}
public void onItemRangeMoved(ObservableList sender, int fromPosition, int toPosition, int itemCount) {
Log.i(tag, "item range moved");
}
public void onItemRangeRemoved(ObservableList sender, int positionStart, int itemCount) {
Log.i(tag, "item range removed");
}
}
ObservableArrayList list = new ObservableArrayList();
list.addOnListChangedCallback(new MyCallback());
...
list.add(element);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论