diff --git a/app/build.gradle b/app/build.gradle index e7be4f2..d861378 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -150,6 +150,9 @@ dependencies { implementation libs.com.google.mlkit.face.detection2 //二维码扫描 implementation libs.barcode.scanning + //FastBle 一个简化BLE开发的框架,专注于低功耗蓝牙设备的连接和数据传输 + //https://github.com/Jasonchenlijian/FastBle?tab=readme-ov-file + implementation libs.fastble configurations { all*.exclude group: 'com.google.code.gson' diff --git a/app/src/main/java/com/rehome/dywoa/bleUtil/BlueToothDeviceActivity.java b/app/src/main/java/com/rehome/dywoa/bleUtil/BlueToothDeviceActivity.java index 3c65938..edc656e 100644 --- a/app/src/main/java/com/rehome/dywoa/bleUtil/BlueToothDeviceActivity.java +++ b/app/src/main/java/com/rehome/dywoa/bleUtil/BlueToothDeviceActivity.java @@ -24,11 +24,16 @@ import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; +import com.clj.fastble.BleManager; +import com.clj.fastble.callback.BleScanCallback; +import com.clj.fastble.data.BleDevice; +import com.clj.fastble.scan.BleScanRuleConfig; import com.google.gson.Gson; import com.rehome.dywoa.R; import com.rehome.dywoa.base.BaseActivity; import java.util.ArrayList; +import java.util.List; /** * 蓝牙扫描 @@ -62,9 +67,13 @@ public class BlueToothDeviceActivity extends BaseActivity { showLog("蓝牙数据管理"); mLeDeviceListAdapter.clear(); mLeDeviceListAdapter.notifyDataSetChanged(); - initBlueTooth(); + //initBlueTooth(); + if(BleManager.getInstance().isSupportBle()){ + initBlueToothFastBle(); + } } }); + BleManager.getInstance().init(getApplication()); lv = findViewById(R.id.lv); mLeDeviceListAdapter = new LeDeviceListAdapter(BlueToothDeviceActivity.this); lv.setAdapter(mLeDeviceListAdapter); @@ -143,6 +152,42 @@ public class BlueToothDeviceActivity extends BaseActivity { } } + public void initBlueToothFastBle() { + BleManager.getInstance() + .enableLog(true) + .setReConnectCount(1, 5000) + .setSplitWriteNum(20) + .setConnectOverTime(10000) + .setOperateTimeout(5000); +// +// BleScanRuleConfig scanRuleConfig = new BleScanRuleConfig.Builder() +// .setAutoConnect(true) +// .setScanTimeOut(10000) +// .build(); +// BleManager.getInstance().initScanRule(scanRuleConfig); + + BleManager.getInstance().scan(new BleScanCallback() { + @Override + public void onScanStarted(boolean success) { + showToast("开始蓝牙扫描"); + } + + @Override + public void onScanning(BleDevice bleDevice) { + showToast("发现设备"); + mLeDeviceListAdapter.addDevice(bleDevice.getDevice()); + mLeDeviceListAdapter.notifyDataSetChanged(); + } + + @Override + public void onScanFinished(List scanResultList) { + showToast("蓝牙扫描结束"); + showLog(String.valueOf(scanResultList.size())); + showLog(new Gson().toJson(scanResultList)); + } + }); + } + private void scanLeDevice() { if (!scanning) { // Stops scanning after a predefined scan period. diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 624852d..391f810 100755 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -9,6 +9,7 @@ circleimageview = "3.1.0" commonsCodecCommonsCodec2 = "1.15" crashreport = "4.1.9.3" eventbus = "3.3.1" +fastble = "2.4.0" glide = "4.11.0" hutoolAllVersion = "5.8.5" kotlin = "1.9.24" @@ -62,6 +63,7 @@ compiler = { module = "com.github.bumptech.glide:compiler", version.ref = "glide compress = { module = "io.github.lucksiege:compress", version.ref = "pictureselector" } crashreport = { module = "com.tencent.bugly:crashreport", version.ref = "crashreport" } eventbus = { module = "org.greenrobot:eventbus", version.ref = "eventbus" } +fastble = { module = "com.github.Jasonchenlijian:FastBle", version.ref = "fastble" } glide = { module = "com.github.bumptech.glide:glide", version.ref = "glide" } junit = { group = "junit", name = "junit", version.ref = "junit" } androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }