新增 蓝牙扫描

master
wenfei 2 months ago
parent 49dc0c35a2
commit f30327c6d2

@ -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'

@ -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<BleDevice> scanResultList) {
showToast("蓝牙扫描结束");
showLog(String.valueOf(scanResultList.size()));
showLog(new Gson().toJson(scanResultList));
}
});
}
private void scanLeDevice() {
if (!scanning) {
// Stops scanning after a predefined scan period.

@ -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" }

Loading…
Cancel
Save