|
|
|
|
@@ -15,13 +15,17 @@ import android.graphics.drawable.AnimationDrawable;
|
|
|
|
|
import android.os.Build;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.os.Handler;
|
|
|
|
|
import android.os.ParcelUuid;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.widget.ImageView;
|
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
|
|
|
|
|
|
import com.ckkj.water.BuildConfig;
|
|
|
|
|
import com.ckkj.water.ConstantUtil;
|
|
|
|
|
import com.ckkj.water.R;
|
|
|
|
|
import com.ckkj.water.base.BaseActivity;
|
|
|
|
|
@@ -43,6 +47,7 @@ import org.jetbrains.annotations.NotNull;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
|
|
import no.nordicsemi.android.support.v18.scanner.BluetoothLeScannerCompat;
|
|
|
|
|
import no.nordicsemi.android.support.v18.scanner.ScanCallback;
|
|
|
|
|
@@ -63,6 +68,9 @@ public class AddDeviceActivity_copy extends BaseActivity<AddDeviceLayoutBinding,
|
|
|
|
|
private static final int REQUEST_BLUETOOTH_PERMISSION = 1002;
|
|
|
|
|
private List<BluetoothDevice> discoveredDevices = new ArrayList<>();
|
|
|
|
|
private AlertDialog deviceListDialog;
|
|
|
|
|
private String mUuid = "1775244d-6b43-439b-877c-060f2d9bed07";
|
|
|
|
|
private ScanCallback mScanCallback;
|
|
|
|
|
private boolean bluetoothReady = false;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public DevicePresenter createPresenter() {
|
|
|
|
|
@@ -73,6 +81,7 @@ public class AddDeviceActivity_copy extends BaseActivity<AddDeviceLayoutBinding,
|
|
|
|
|
@Override
|
|
|
|
|
public void initData(Bundle savedInstanceState) {
|
|
|
|
|
super.initData(savedInstanceState);
|
|
|
|
|
|
|
|
|
|
ActivityManager.addActivity(this,getClass().getSimpleName() + "_" + ConstantUtil.ADD_DEVICE_FLAG);
|
|
|
|
|
ImmersionBar.with(this)
|
|
|
|
|
.statusBarColor(R.color.mainBackgroundColor) // 设置状态栏颜色
|
|
|
|
|
@@ -83,34 +92,9 @@ public class AddDeviceActivity_copy extends BaseActivity<AddDeviceLayoutBinding,
|
|
|
|
|
if(checkBluetoothPermissions()){
|
|
|
|
|
// 初始化蓝牙适配器
|
|
|
|
|
initBluetooth();
|
|
|
|
|
BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
|
|
|
|
|
ScanSettings settings = new ScanSettings.Builder()
|
|
|
|
|
.setLegacy(false)
|
|
|
|
|
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
|
|
|
|
|
.setReportDelay(5000)
|
|
|
|
|
.setUseHardwareBatchingIfSupported(true)
|
|
|
|
|
.build();
|
|
|
|
|
List<ScanFilter> filters = new ArrayList<>();
|
|
|
|
|
scanner.startScan(null, settings, new ScanCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onScanResult(int callbackType, @NonNull ScanResult result) {
|
|
|
|
|
super.onScanResult(callbackType, result);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onBatchScanResults(@NonNull List<ScanResult> results) {
|
|
|
|
|
super.onBatchScanResults(results);
|
|
|
|
|
if(results != null){
|
|
|
|
|
int a = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onScanFailed(int errorCode) {
|
|
|
|
|
super.onScanFailed(errorCode);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (bluetoothReady) {
|
|
|
|
|
startBleScan();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -138,10 +122,57 @@ public class AddDeviceActivity_copy extends BaseActivity<AddDeviceLayoutBinding,
|
|
|
|
|
initListener();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 开始 BLE 扫描
|
|
|
|
|
*/
|
|
|
|
|
private void startBleScan() {
|
|
|
|
|
BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
|
|
|
|
|
UUID SERVICE_UUID = UUID.fromString(mUuid);
|
|
|
|
|
ParcelUuid parcelUuid = new ParcelUuid(SERVICE_UUID);
|
|
|
|
|
ScanSettings settings = new ScanSettings.Builder()
|
|
|
|
|
.setLegacy(false)
|
|
|
|
|
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
|
|
|
|
|
.setReportDelay(5000)
|
|
|
|
|
.build();
|
|
|
|
|
List<ScanFilter> filters = new ArrayList<>();
|
|
|
|
|
filters.add(new ScanFilter.Builder().setServiceUuid(parcelUuid).build());
|
|
|
|
|
mScanCallback = new ScanCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onScanResult(int callbackType, @NonNull ScanResult result) {
|
|
|
|
|
super.onScanResult(callbackType, result);
|
|
|
|
|
if (result.getScanRecord() != null) {
|
|
|
|
|
String deviceName = result.getScanRecord().getDeviceName();
|
|
|
|
|
Log.e("TEST", "onScanResult: " + deviceName);
|
|
|
|
|
ToastUtil.showShort(mContext, "扫描到设备: " + deviceName);
|
|
|
|
|
}else{
|
|
|
|
|
Log.e("TEST", "onScanResult: " + "设备名称为空");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onBatchScanResults(@NonNull List<ScanResult> results) {
|
|
|
|
|
super.onBatchScanResults(results);
|
|
|
|
|
Log.e("TEST", "onBatchScanResults: " + results.size() + " devices");
|
|
|
|
|
if (results != null && !results.isEmpty()) {
|
|
|
|
|
ToastUtil.showShort(mContext, "批量扫描到 " + results.size() + " 个设备");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onScanFailed(int errorCode) {
|
|
|
|
|
super.onScanFailed(errorCode);
|
|
|
|
|
Log.e("TEST", "onScanFailed: " + errorCode);
|
|
|
|
|
ToastUtil.showShort(mContext, "扫描失败,错误码: " + errorCode);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
scanner.startScan(filters, settings, mScanCallback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 初始化蓝牙适配器
|
|
|
|
|
*/
|
|
|
|
|
private void initBluetooth() {
|
|
|
|
|
Log.e("TEST", "initBluetooth: 开始初始化");
|
|
|
|
|
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
|
|
|
|
|
if (bluetoothManager != null) {
|
|
|
|
|
bluetoothAdapter = bluetoothManager.getAdapter();
|
|
|
|
|
@@ -154,8 +185,12 @@ public class AddDeviceActivity_copy extends BaseActivity<AddDeviceLayoutBinding,
|
|
|
|
|
|
|
|
|
|
// 检查蓝牙是否开启
|
|
|
|
|
if (!bluetoothAdapter.isEnabled()) {
|
|
|
|
|
Log.e("TEST", "initBluetooth: 蓝牙未开启,请求开启");
|
|
|
|
|
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
|
|
|
|
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
|
|
|
|
|
} else {
|
|
|
|
|
Log.e("TEST", "initBluetooth: 蓝牙已开启");
|
|
|
|
|
bluetoothReady = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -190,112 +225,7 @@ public class AddDeviceActivity_copy extends BaseActivity<AddDeviceLayoutBinding,
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 开始扫描蓝牙设备
|
|
|
|
|
*/
|
|
|
|
|
private void startBleScan() {
|
|
|
|
|
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
|
|
|
|
|
ToastUtil.showShort(mContext, "请先开启蓝牙");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!checkBluetoothPermissions()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
discoveredDevices.clear();
|
|
|
|
|
showLoadingDialog();
|
|
|
|
|
|
|
|
|
|
// 获取已配对设备
|
|
|
|
|
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
|
|
|
|
|
if (pairedDevices != null && !pairedDevices.isEmpty()) {
|
|
|
|
|
discoveredDevices.addAll(pairedDevices);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 开始扫描
|
|
|
|
|
new Handler().postDelayed(() -> {
|
|
|
|
|
stopBleScan();
|
|
|
|
|
showDeviceListDialog();
|
|
|
|
|
}, 10000); // 10秒后自动停止扫描
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 停止扫描
|
|
|
|
|
*/
|
|
|
|
|
private void stopBleScan() {
|
|
|
|
|
hideDialogLoading();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 显示设备列表对话框
|
|
|
|
|
*/
|
|
|
|
|
private void showDeviceListDialog() {
|
|
|
|
|
if (discoveredDevices.isEmpty()) {
|
|
|
|
|
ToastUtil.showShort(mContext, "未发现蓝牙设备");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String[] deviceNames = new String[discoveredDevices.size()];
|
|
|
|
|
for (int i = 0; i < discoveredDevices.size(); i++) {
|
|
|
|
|
BluetoothDevice device = discoveredDevices.get(i);
|
|
|
|
|
String name = device.getName();
|
|
|
|
|
if (name == null || name.isEmpty()) {
|
|
|
|
|
name = "未知设备";
|
|
|
|
|
}
|
|
|
|
|
deviceNames[i] = name + "\n" + device.getAddress();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
|
|
|
builder.setTitle("发现 " + discoveredDevices.size() + " 个设备");
|
|
|
|
|
builder.setItems(deviceNames, (dialog, which) -> {
|
|
|
|
|
BluetoothDevice selectedDevice = discoveredDevices.get(which);
|
|
|
|
|
connectToDevice(selectedDevice);
|
|
|
|
|
});
|
|
|
|
|
builder.setNegativeButton("取消", null);
|
|
|
|
|
deviceListDialog = builder.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 连接蓝牙设备
|
|
|
|
|
*/
|
|
|
|
|
private void connectToDevice(BluetoothDevice device) {
|
|
|
|
|
showLoadingDialog();
|
|
|
|
|
String deviceName = device.getName();
|
|
|
|
|
if (deviceName == null || deviceName.isEmpty()) {
|
|
|
|
|
deviceName = "未知设备";
|
|
|
|
|
}
|
|
|
|
|
ToastUtil.showShort(mContext, "正在连接: " + deviceName);
|
|
|
|
|
|
|
|
|
|
// TODO: 这里可以调用 DeviceBleManager 进行连接
|
|
|
|
|
// DeviceBleManager bleManager = new DeviceBleManager(this);
|
|
|
|
|
// bleManager.connect(device);
|
|
|
|
|
|
|
|
|
|
// 模拟连接成功
|
|
|
|
|
String finalDeviceName = deviceName;
|
|
|
|
|
new Handler().postDelayed(() -> {
|
|
|
|
|
hideDialogLoading();
|
|
|
|
|
ToastUtil.showShort(mContext, "连接成功: " + finalDeviceName);
|
|
|
|
|
// 获取设备信息
|
|
|
|
|
getDeviceInfo(device);
|
|
|
|
|
}, 2000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取蓝牙设备信息
|
|
|
|
|
*/
|
|
|
|
|
private void getDeviceInfo(BluetoothDevice device) {
|
|
|
|
|
String deviceName = device.getName();
|
|
|
|
|
String deviceAddress = device.getAddress();
|
|
|
|
|
|
|
|
|
|
if (deviceName == null || deviceName.isEmpty()) {
|
|
|
|
|
deviceName = "未知设备";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ToastUtil.showShort(mContext, "设备名称: " + deviceName + "\n设备地址: " + deviceAddress);
|
|
|
|
|
|
|
|
|
|
// TODO: 将设备信息传递给后续业务逻辑
|
|
|
|
|
// 例如跳转到下一个页面或保存设备信息
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void initListener() {
|
|
|
|
|
@@ -334,7 +264,6 @@ public class AddDeviceActivity_copy extends BaseActivity<AddDeviceLayoutBinding,
|
|
|
|
|
|
|
|
|
|
case R.id.btn_ble_scan:
|
|
|
|
|
// 扫描蓝牙设备
|
|
|
|
|
startBleScan();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -345,8 +274,12 @@ public class AddDeviceActivity_copy extends BaseActivity<AddDeviceLayoutBinding,
|
|
|
|
|
if (requestCode == REQUEST_ENABLE_BT) {
|
|
|
|
|
if (resultCode == RESULT_OK) {
|
|
|
|
|
ToastUtil.showShort(mContext, "蓝牙已开启");
|
|
|
|
|
Log.e("BluetoothScan", "onActivityResult: 蓝牙已开启,开始扫描");
|
|
|
|
|
bluetoothReady = true;
|
|
|
|
|
startBleScan();
|
|
|
|
|
} else {
|
|
|
|
|
ToastUtil.showShort(mContext, "请开启蓝牙以扫描设备");
|
|
|
|
|
Log.e("BluetoothScan", "onActivityResult: 用户拒绝开启蓝牙");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -367,7 +300,6 @@ public class AddDeviceActivity_copy extends BaseActivity<AddDeviceLayoutBinding,
|
|
|
|
|
if (allGranted) {
|
|
|
|
|
// 初始化蓝牙适配器
|
|
|
|
|
initBluetooth();
|
|
|
|
|
startBleScan();
|
|
|
|
|
} else {
|
|
|
|
|
ToastUtil.showShort(mContext, "需要蓝牙权限才能扫描设备");
|
|
|
|
|
}
|
|
|
|
|
@@ -468,22 +400,9 @@ public class AddDeviceActivity_copy extends BaseActivity<AddDeviceLayoutBinding,
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDestroy() {
|
|
|
|
|
super.onDestroy();
|
|
|
|
|
BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
|
|
|
|
|
scanner.stopScan(new ScanCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onScanResult(int callbackType, @NonNull ScanResult result) {
|
|
|
|
|
super.onScanResult(callbackType, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onBatchScanResults(@NonNull List<ScanResult> results) {
|
|
|
|
|
super.onBatchScanResults(results);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onScanFailed(int errorCode) {
|
|
|
|
|
super.onScanFailed(errorCode);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (mScanCallback != null) {
|
|
|
|
|
BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
|
|
|
|
|
scanner.stopScan(mScanCallback);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|