Browse Source

蓝牙设备自动链接

master
白凤吉 4 months ago
parent
commit
3452b05120
  1. 1
      app/src/main/java/com/iflytop/profilometer/MainActivity.java
  2. 41
      app/src/main/java/com/iflytop/profilometer/core/bluetooth/BleManager.java

1
app/src/main/java/com/iflytop/profilometer/MainActivity.java

@ -41,6 +41,7 @@ public class MainActivity extends AppCompatActivity {
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
// webView.loadUrl("http://127.0.0.1:8080/");
webView.loadUrl("http://192.168.1.112:3000/");

41
app/src/main/java/com/iflytop/profilometer/core/bluetooth/BleManager.java

@ -1,5 +1,7 @@
package com.iflytop.profilometer.core.bluetooth;
import static android.content.Context.MODE_PRIVATE;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
@ -16,6 +18,7 @@ import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
@ -293,6 +296,7 @@ public class BleManager {
/**
* 开始扫描 BLE 设备扫描到的每个设备均依次连接进行服务查询
* 如果扫描到的设备其 MAC 地址与存储的地址一致则自动调用 connectToDevice() 进行连接
*/
@SuppressLint("MissingPermission")
public void startScan() {
@ -318,12 +322,23 @@ public class BleManager {
@Override
public void onScanResult(int callbackType, ScanResult result) {
BluetoothDevice device = result.getDevice();
// 仅处理设备名非空的设备
if (device != null && device.getName() != null) {
if (result.getScanRecord() != null) {
deviceLastSeenMap.put(device, System.currentTimeMillis());
}
String address = device.getAddress();
// 如果该设备已经处理过则跳过连接
// 先检查存储的 MAC 地址若匹配则自动连接
SharedPreferences prefs = context.getSharedPreferences("profilometer", MODE_PRIVATE);
String storedMac = prefs.getString("mac_address", null);
if (storedMac != null && storedMac.equals(address)) {
if (!isConnected()) {
connectToDevice(address);
}
processedDevices.add(address);
return;
}
// 避免重复处理
if (processedDevices.contains(address)) {
return;
}
@ -331,18 +346,27 @@ public class BleManager {
// 对扫描到的设备进行连接查询服务
device.connectGatt(context, false, new ServiceDiscoveryGattCallback(device));
}
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
for (ScanResult result : results) {
BluetoothDevice device = result.getDevice();
if(device != null && device.getName() != null){
if (device != null && device.getName() != null) {
if (result.getScanRecord() != null) {
deviceLastSeenMap.put(device, System.currentTimeMillis());
}
String address = device.getAddress();
// 检查存储的 MAC 地址
SharedPreferences prefs = context.getSharedPreferences("profilometer", MODE_PRIVATE);
String storedMac = prefs.getString("mac_address", null);
if (storedMac != null && storedMac.equals(address)) {
if (!isConnected()) {
connectToDevice(address);
}
processedDevices.add(address);
continue;
}
if (processedDevices.contains(address)) {
continue;
}
@ -427,6 +451,11 @@ public class BleManager {
channel.setDeviceOnReportListener(new BleDeviceListener());
DeviceState deviceState = SystemService.getInstance().getDeviceState();
deviceState.setConnected(true);
SharedPreferences prefs = context.getSharedPreferences("profilometer", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("mac_address", macAddress);
editor.apply();
} catch (IllegalArgumentException e) {
Log.e(TAG, "无效的 MAC 地址: " + macAddress, e);
}
@ -469,6 +498,12 @@ public class BleManager {
bluetoothGatt = null;
DeviceState deviceState = SystemService.getInstance().getDeviceState();
deviceState.setConnected(false);
SharedPreferences prefs = context.getSharedPreferences("profilometer", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.remove("mac_address");
editor.apply();
Log.d(TAG, "当前蓝牙设备已断开连接");
}
}

Loading…
Cancel
Save