显示附近蓝牙设备
This commit is contained in:
@@ -41,6 +41,7 @@ import com.ckkj.water.index.entity.WaterDeviceEntity;
|
||||
import com.ckkj.water.network.BaseBean;
|
||||
import com.ckkj.water.utils.manager.ActivityManager;
|
||||
import com.ckkj.water.utils.manager.ThemeManager;
|
||||
import com.ckkj.water.utils.manager.WaterDeviceManager;
|
||||
import com.ckkj.water.utils.show.ToastUtil;
|
||||
import com.gyf.immersionbar.ImmersionBar;
|
||||
import com.maning.mlkitscanner.scan.MNScanManager;
|
||||
@@ -48,12 +49,14 @@ import com.maning.mlkitscanner.scan.callback.act.MNScanCallback;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import no.nordicsemi.android.ble.observer.ConnectionObserver;
|
||||
import no.nordicsemi.android.support.v18.scanner.BluetoothLeScannerCompat;
|
||||
import no.nordicsemi.android.support.v18.scanner.ScanCallback;
|
||||
import no.nordicsemi.android.support.v18.scanner.ScanFilter;
|
||||
@@ -80,6 +83,7 @@ public class AddDeviceActivity_copy extends BaseActivity<AddDeviceLayoutBinding,
|
||||
private BleScanDeviceAdapter bleAdapter;
|
||||
private List<WaterDeviceEntity> mDeviceList = new ArrayList<>();
|
||||
private Set<String> discoveredMacSet = new HashSet<>();
|
||||
private WaterDeviceManager waterDeviceManager;
|
||||
|
||||
@Override
|
||||
public DevicePresenter createPresenter() {
|
||||
@@ -189,8 +193,160 @@ public class AddDeviceActivity_copy extends BaseActivity<AddDeviceLayoutBinding,
|
||||
}
|
||||
};
|
||||
scanner.startScan(null, settings, mScanCallback);
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止 BLE 扫描
|
||||
*/
|
||||
private void stopBleScan() {
|
||||
if (mScanCallback != null) {
|
||||
BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
|
||||
scanner.stopScan(mScanCallback);
|
||||
Log.e("TEST", "停止扫描");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接蓝牙设备
|
||||
*/
|
||||
private void connectToDevice(String deviceAddress, String deviceName) {
|
||||
if (waterDeviceManager != null) {
|
||||
// 断开之前的连接
|
||||
waterDeviceManager.disconnect().enqueue();
|
||||
waterDeviceManager.close();
|
||||
}
|
||||
|
||||
// 根据地址获取 BluetoothDevice
|
||||
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress);
|
||||
if (device == null) {
|
||||
ToastUtil.showShort(mContext, "设备无效");
|
||||
return;
|
||||
}
|
||||
|
||||
waterDeviceManager = new WaterDeviceManager(mContext);
|
||||
|
||||
// 设置数据回调
|
||||
waterDeviceManager.setOnDataReceivedListener(bytes -> {
|
||||
Log.e("TEST", "设备响应: " + Arrays.toString(bytes));
|
||||
runOnUiThread(() -> {
|
||||
// TODO: 处理设备返回的数据,比如 WiFi 配置响应
|
||||
handleDeviceResponse(bytes);
|
||||
});
|
||||
});
|
||||
|
||||
// 连接设备
|
||||
waterDeviceManager.connect(device)
|
||||
.retry(3, 100) // 重试3次,间隔100ms
|
||||
.done(bluetoothDevice -> {
|
||||
Log.e("TEST", "连接成功: " + deviceName);
|
||||
runOnUiThread(() -> {
|
||||
ToastUtil.showShort(mContext, "连接成功: " + deviceName);
|
||||
// TODO: 连接成功后,发送 WiFi 配置
|
||||
// sendWifiConfig("SSID", "PASSWORD");
|
||||
});
|
||||
})
|
||||
.fail((bluetoothDevice, status) -> {
|
||||
Log.e("TEST", "连接失败: " + status);
|
||||
runOnUiThread(() -> {
|
||||
ToastUtil.showShort(mContext, "连接失败,请重试");
|
||||
});
|
||||
})
|
||||
.enqueue();
|
||||
|
||||
waterDeviceManager.setConnectionObserver(new ConnectionObserver() {
|
||||
|
||||
@Override
|
||||
public void onDeviceConnecting(@NonNull BluetoothDevice device) {
|
||||
Log.e("TEST", "onDeviceConnecting");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeviceConnected(@NonNull BluetoothDevice device) {
|
||||
Log.e("TEST", "onDeviceConnected");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeviceFailedToConnect(@NonNull BluetoothDevice device, int reason) {
|
||||
Log.e("TEST", "onDeviceFailedToConnect");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeviceReady(@NonNull BluetoothDevice device) {
|
||||
Log.e("TEST", "onDeviceReady");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeviceDisconnecting(@NonNull BluetoothDevice device) {
|
||||
Log.e("TEST", "onDeviceDisconnecting");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeviceDisconnected(
|
||||
@NonNull BluetoothDevice device,
|
||||
int reason) {
|
||||
|
||||
Log.e("TEST", "onDeviceDisconnected reason=" + reason);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送 WiFi 配置到设备
|
||||
*/
|
||||
public void sendWifiConfig(String ssid, String password) {
|
||||
if (waterDeviceManager == null || !waterDeviceManager.isBlConnected()) {
|
||||
Log.e("TEST", "设备未连接,无法发送WiFi配置");
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: 根据设备协议组装数据
|
||||
// 示例:假设协议是 [header(1) + ssid_len(1) + ssid(n) + pwd_len(1) + pwd(m)]
|
||||
byte[] ssidBytes = ssid.getBytes();
|
||||
byte[] pwdBytes = password.getBytes();
|
||||
|
||||
byte[] data = new byte[2 + ssidBytes.length + 2 + pwdBytes.length];
|
||||
int index = 0;
|
||||
data[index++] = 0x01; // header
|
||||
data[index++] = (byte) ssidBytes.length;
|
||||
System.arraycopy(ssidBytes, 0, data, index, ssidBytes.length);
|
||||
index += ssidBytes.length;
|
||||
data[index++] = (byte) pwdBytes.length;
|
||||
System.arraycopy(pwdBytes, 0, data, index, pwdBytes.length);
|
||||
|
||||
waterDeviceManager.send(data);
|
||||
Log.e("TEST", "发送WiFi配置: SSID=" + ssid + ", PWD=" + password);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理设备响应数据
|
||||
*/
|
||||
private void handleDeviceResponse(byte[] data) {
|
||||
if (data == null || data.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: 根据设备协议解析响应数据
|
||||
// 示例:假设响应格式是 [type(1) + status(1) + ...]
|
||||
Log.e("TEST", "处理设备响应: " + Arrays.toString(data));
|
||||
|
||||
if (data.length >= 2) {
|
||||
byte type = data[0];
|
||||
byte status = data[1];
|
||||
|
||||
switch (type) {
|
||||
case 0x01: // WiFi配置响应
|
||||
if (status == 0x00) {
|
||||
ToastUtil.showShort(mContext, "WiFi配置成功");
|
||||
// TODO: 下一步操作
|
||||
} else {
|
||||
ToastUtil.showShort(mContext, "WiFi配置失败,错误码: " + status);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Log.e("TEST", "未知响应类型: " + type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -260,9 +416,18 @@ public class AddDeviceActivity_copy extends BaseActivity<AddDeviceLayoutBinding,
|
||||
bleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
|
||||
ToastUtil.showShort(mContext, TextUtils.isEmpty(mDeviceList.get(position).getDeviceName()) ?
|
||||
"未知设备" : mDeviceList.get(position).getDeviceName());
|
||||
//TODO 准备连接设备,方案未定
|
||||
WaterDeviceEntity device = mDeviceList.get(position);
|
||||
String deviceName = TextUtils.isEmpty(device.getDeviceName()) ?
|
||||
"未知设备" : device.getDeviceName();
|
||||
String deviceAddress = device.getMacAddress();
|
||||
|
||||
ToastUtil.showShort(mContext, "正在连接: " + deviceName);
|
||||
|
||||
// 停止扫描
|
||||
stopBleScan();
|
||||
|
||||
// 连接设备
|
||||
connectToDevice(deviceAddress, deviceName);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -433,9 +598,13 @@ public class AddDeviceActivity_copy extends BaseActivity<AddDeviceLayoutBinding,
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
if (mScanCallback != null) {
|
||||
BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
|
||||
scanner.stopScan(mScanCallback);
|
||||
// 停止扫描
|
||||
stopBleScan();
|
||||
// 断开蓝牙连接
|
||||
if (waterDeviceManager != null) {
|
||||
waterDeviceManager.disconnect().enqueue();
|
||||
waterDeviceManager.close();
|
||||
waterDeviceManager = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user