如何在每个发现的设备之后获得一个列表?

huangapple go评论76阅读模式
英文:

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() &gt; 0) {

                    boolean isNewItem = true;

                    for (int i = 0; i &lt; 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&lt;BluetoothLE&gt; 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&lt;ObservableList&gt; {

public void onChanged(ObservableList sender) {
    Log.i(tag, &quot;list changed&quot;);
}

public void onItemRangeChanged(ObservableList sender, int positionStart, int itemCount) {
    Log.i(tag, &quot;item range changed&quot;);
}

public void onItemRangeInserted(ObservableList sender, int positionStart, int itemCount) {
    Log.i(tag, &quot;item range inserted&quot;);
}

public void onItemRangeMoved(ObservableList sender, int fromPosition, int toPosition, int itemCount)  {
    Log.i(tag, &quot;item range moved&quot;);
}

public void onItemRangeRemoved(ObservableList sender, int positionStart, int itemCount)  {
    Log.i(tag, &quot;item range removed&quot;);
}
}


ObservableArrayList list = new ObservableArrayList();
list.addOnListChangedCallback(new MyCallback());
...
list.add(element);

huangapple
  • 本文由 发表于 2020年10月15日 01:48:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64358860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定