Skip to content

Commit b105156

Browse files
committed
修复重复连接的问题
1 parent aa5ecaa commit b105156

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

BleLibrary/src/main/java/com/renyu/blelibrary/utils/BLEFramework.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,16 @@ public synchronized void startScanAndConn(final IScanAndConnRule iScanAndConnRul
356356
public void onScanResult(int callbackType, ScanResult result) {
357357
super.onScanResult(callbackType, result);
358358
if (result !=null && result.getDevice()!=null && iScanAndConnRule.rule21(result)) {
359-
BLEDevice device1=new BLEDevice();
360-
device1.setRssi(result.getRssi());
361-
device1.setDevice(result.getDevice());
362-
device1.setScanRecord(result.getScanRecord().getBytes());
363-
bleConnectListener.getAllScanDevice(device1);
364-
stopScan(false);
365-
startConn(result.getDevice());
359+
// 已经发生了连接
360+
if (connectionState <= STATE_SCANNED) {
361+
BLEDevice device1=new BLEDevice();
362+
device1.setRssi(result.getRssi());
363+
device1.setDevice(result.getDevice());
364+
device1.setScanRecord(result.getScanRecord().getBytes());
365+
bleConnectListener.getAllScanDevice(device1);
366+
stopScan(false);
367+
startConn(result.getDevice());
368+
}
366369
}
367370
}
368371
};
@@ -381,13 +384,16 @@ public void run() {
381384
@Override
382385
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
383386
if (device!=null && !TextUtils.isEmpty(device.getName()) && iScanAndConnRule.rule(device)) {
384-
BLEDevice device1=new BLEDevice();
385-
device1.setRssi(rssi);
386-
device1.setDevice(device);
387-
device1.setScanRecord(scanRecord);
388-
bleConnectListener.getAllScanDevice(device1);
389-
stopScan(false);
390-
startConn(device);
387+
// 已经发生了连接
388+
if (connectionState <= STATE_SCANNED) {
389+
BLEDevice device1=new BLEDevice();
390+
device1.setRssi(rssi);
391+
device1.setDevice(device);
392+
device1.setScanRecord(scanRecord);
393+
bleConnectListener.getAllScanDevice(device1);
394+
stopScan(false);
395+
startConn(device);
396+
}
391397
}
392398
}
393399
};

0 commit comments

Comments
 (0)