mirror of
https://github.com/shuto-cn/RFID.git
synced 2024-10-05 23:32:15 +08:00
处理蓝牙列表数据重复问题
This commit is contained in:
parent
3837dd05d8
commit
5bda10620b
@ -22,7 +22,8 @@
|
||||
- success 成功回调
|
||||
- error 失败回调
|
||||
- 返回值
|
||||
- success => { message: "", data:[ 蓝牙设备list ] }
|
||||
- success => { message: "", data:[
|
||||
{"name":"DYD0CZJ895NEL93","address":"3C:F8:62:13:29:B8"} ] }
|
||||
- error => errorMsg
|
||||
- 调用示例
|
||||
```
|
||||
|
@ -54,7 +54,9 @@ public class RfidPlugin extends CordovaPlugin {
|
||||
private static final String WRITE = "write";
|
||||
|
||||
//蓝牙设备列表
|
||||
private List<BluetoothDevice> mBlueToothList;
|
||||
private JSONArray mBlueToothArray;
|
||||
//搜索出的蓝牙设备列表,其中有重复的,去重
|
||||
private List<String> mBlueToothAddressList;
|
||||
|
||||
//判断设备是否已连接蓝牙
|
||||
private Boolean isConnect = false;
|
||||
@ -245,20 +247,20 @@ public class RfidPlugin extends CordovaPlugin {
|
||||
* 搜索蓝牙设备列表
|
||||
*/
|
||||
private void doDiscovery() {
|
||||
mBlueToothList = new ArrayList<>();
|
||||
// 注册蓝牙开始搜索广播
|
||||
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
|
||||
this.cordova.getActivity().registerReceiver(mReceiver, filter);
|
||||
if (!mBluetoothAdapter.isDiscovering()) {
|
||||
// 注册蓝牙开始搜索广播
|
||||
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
|
||||
this.cordova.getActivity().registerReceiver(mReceiver, filter);
|
||||
|
||||
// 注册蓝牙搜索完毕广播
|
||||
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
|
||||
this.cordova.getActivity().registerReceiver(mReceiver, filter);
|
||||
//如果正在搜索,关闭
|
||||
if (mBluetoothAdapter.isDiscovering()) {
|
||||
mBluetoothAdapter.cancelDiscovery();
|
||||
// 注册蓝牙搜索完毕广播
|
||||
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
|
||||
this.cordova.getActivity().registerReceiver(mReceiver, filter);
|
||||
mBlueToothArray = new JSONArray();
|
||||
mBlueToothAddressList = new ArrayList<>();
|
||||
//开始搜索
|
||||
mBluetoothAdapter.startDiscovery();
|
||||
}
|
||||
//开始搜索
|
||||
mBluetoothAdapter.startDiscovery();
|
||||
|
||||
}
|
||||
|
||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@ -268,12 +270,24 @@ public class RfidPlugin extends CordovaPlugin {
|
||||
|
||||
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
|
||||
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
||||
mBlueToothList.add(device);
|
||||
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
|
||||
if (mBlueToothAddressList.indexOf(device.getAddress()) == -1) {
|
||||
mBlueToothAddressList.add(device.getAddress());
|
||||
JSONObject object = new JSONObject();
|
||||
try {
|
||||
object.put("name", device.getName() == null ? "" : device.getName());
|
||||
object.put("address", device.getAddress());
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
mBlueToothArray.put(object);
|
||||
}
|
||||
}
|
||||
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
|
||||
JSONObject obj = new JSONObject();
|
||||
try {
|
||||
obj.put("message", "蓝牙搜索完毕");
|
||||
obj.put("data", mBlueToothList);
|
||||
obj.put("data", mBlueToothArray);
|
||||
} catch (JSONException e) {
|
||||
mCallbackContext.error(e.getMessage());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user