diff --git a/README.md b/README.md index fc435cd..1b13da7 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,8 @@ - success 成功回调 - error 失败回调 - 返回值 - - success => { message: "", data:[ 蓝牙设备list ] } + - success => { message: "", data:[ + {"name":"DYD0CZJ895NEL93","address":"3C:F8:62:13:29:B8"} ] } - error => errorMsg - 调用示例 ``` diff --git a/src/android/cn/shuto/rfid/RfidPlugin.java b/src/android/cn/shuto/rfid/RfidPlugin.java index 8af758e..86e3f3d 100644 --- a/src/android/cn/shuto/rfid/RfidPlugin.java +++ b/src/android/cn/shuto/rfid/RfidPlugin.java @@ -54,7 +54,9 @@ public class RfidPlugin extends CordovaPlugin { private static final String WRITE = "write"; //蓝牙设备列表 - private List mBlueToothList; + private JSONArray mBlueToothArray; + //搜索出的蓝牙设备列表,其中有重复的,去重 + private List 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()); }