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