Skip to content

Commit

Permalink
2020.10.23: 优化ble 连接稳定性
Browse files Browse the repository at this point in the history
  • Loading branch information
konewu committed Oct 26, 2020
1 parent 21763fe commit 98fc02a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion sinovoble/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ android {

sourceSets {
main{
jniLibs.srcDirs = ['jniLibs']
jniLibs.srcDirs = ['jnilibs']
}
}

Expand Down
32 changes: 18 additions & 14 deletions sinovoble/src/main/java/com/sinovotec/sinovoble/SinovoBle.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,13 @@ public void init(Context context, IScanCallBack iScanCallBack, IConnectCallback
context.registerReceiver(receiver,makeFilter());

//加载 so库,初始化
LoadLibJni.LoadLib();
myJniLib = new LoadLibJni();
if (LoadLibJni.LoadLib()){
Log.d(TAG, "加载 so库,初始化 成功");
myJniLib = new LoadLibJni();
}else {
Log.e(TAG, "加载 so库,初始化 失败");
}

}
}
private IntentFilter makeFilter() {
Expand All @@ -291,7 +296,7 @@ private IntentFilter makeFilter() {
return filter;
}

public int startBleScan(){
private void startBleScan(){
Log.d(TAG, "调用startBleScan 来扫描");
getBondBleMacList().clear(); //clean the bondBleMacList before starting scan
setScanAgain(true);
Expand All @@ -305,7 +310,8 @@ public void run() {
}
}, 60*1000);
}
return bleScan(getmBleScanCallBack());

bleScan(getmBleScanCallBack());
}

//绑定超时检测
Expand Down Expand Up @@ -383,7 +389,7 @@ public int checkEnvir(){

/**
* 创建用户,默认创建的是普通用户
* @param userName
* @param userName string
*/
public int addUser(String userName){
if (userName.isEmpty() || userName.length()>10){
Expand Down Expand Up @@ -907,7 +913,6 @@ public void run() {
}
}, 100);
}

}


Expand All @@ -932,15 +937,16 @@ private boolean connectBle(final BluetoothDevice bluetoothDevice) {
return false;
}

//防止连接出现133错误, 不能发现Services
if (BleConnCallBack.getInstance().getmBluetoothGatt() != null ) {
Log.w(TAG, "connectDevice: closeGatt");
BleConnCallBack.getInstance().releaseBle();
return false;
}

SinovoBle.getInstance().setConnectting(true); //标记 已经在开始进行连接
BleConnCallBack.getInstance().setConnectingMAC(bluetoothDevice.getAddress()); //标记 当前准备连接的地址,以便后面断开进行重连

//防止连接出现133错误, 不能发现Services
// if (BleConnCallBack.getInstance().getmBluetoothGatt() != null ) {
// Log.w(TAG, "connectDevice: closeGatt");
// BleConnCallBack.getInstance().releaseBle();
// }

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
BleConnCallBack.getInstance().setmBluetoothGatt(bluetoothDevice.connectGatt(getContext(), false, BleConnCallBack.getInstance(), BluetoothDevice.TRANSPORT_LE));
} else {
Expand All @@ -956,6 +962,4 @@ public void disconnBle(){
BleConnCallBack.getInstance().disConectBle();
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,6 @@ public void disConectBle() {
BleData.getInstance().getCommandList().clear();

sendDataHandler.removeCallbacksAndMessages(null); //取消发送数据定时检测的任务
//是否资源 20201023
releaseBle();

//非绑定模式下,连接断开才通知回调
Expand Down

0 comments on commit 98fc02a

Please sign in to comment.