merge:合并代码 解决冲突
This commit is contained in:
commit
34af4fb9d7
@ -20,15 +20,19 @@ class BlueManage {
|
|||||||
|
|
||||||
// 用来写入的服务id
|
// 用来写入的服务id
|
||||||
final Guid _serviceIdConnect = Guid("fff0");
|
final Guid _serviceIdConnect = Guid("fff0");
|
||||||
|
|
||||||
// 用来写入的服务id
|
// 用来写入的服务id
|
||||||
final Guid _serviceIdWrite = Guid('0000FFF0-0000-1000-8000-00805F9B34FB');
|
final Guid _serviceIdWrite = Guid('0000FFF0-0000-1000-8000-00805F9B34FB');
|
||||||
|
|
||||||
// 用来订阅的特征id
|
// 用来订阅的特征id
|
||||||
final Guid _characteristicIdSubscription = Guid("fff1");
|
final Guid _characteristicIdSubscription = Guid("fff1");
|
||||||
|
|
||||||
// 用来写入的特征id
|
// 用来写入的特征id
|
||||||
final Guid _characteristicIdWrite = Guid("fff2");
|
final Guid _characteristicIdWrite = Guid("fff2");
|
||||||
|
|
||||||
// 监听发送事件
|
// 监听发送事件
|
||||||
StreamSubscription<EventSendModel>? _sendStreamSubscription;
|
StreamSubscription<EventSendModel>? _sendStreamSubscription;
|
||||||
|
|
||||||
// 监听蓝牙扫描的事件
|
// 监听蓝牙扫描的事件
|
||||||
// StreamSubscription? _scanSubscription;
|
// StreamSubscription? _scanSubscription;
|
||||||
// 监听蓝牙连接的事件
|
// 监听蓝牙连接的事件
|
||||||
@ -42,12 +46,16 @@ class BlueManage {
|
|||||||
|
|
||||||
// 当前连接设备的名字
|
// 当前连接设备的名字
|
||||||
String connectDeviceName = "";
|
String connectDeviceName = "";
|
||||||
|
|
||||||
// 当前连接设备的mac地址
|
// 当前连接设备的mac地址
|
||||||
String connectDeviceMacAddress = "";
|
String connectDeviceMacAddress = "";
|
||||||
|
|
||||||
// 当前连接的设备
|
// 当前连接的设备
|
||||||
BluetoothDevice? bluetoothConnectDevice;
|
BluetoothDevice? bluetoothConnectDevice;
|
||||||
|
|
||||||
// 当前扫描到结果要连接设备
|
// 当前扫描到结果要连接设备
|
||||||
ScanResult? scanResult;
|
ScanResult? scanResult;
|
||||||
|
|
||||||
// 监听蓝牙连接状态
|
// 监听蓝牙连接状态
|
||||||
BluetoothConnectionState? bluetoothConnectionState = BluetoothConnectionState.disconnected;
|
BluetoothConnectionState? bluetoothConnectionState = BluetoothConnectionState.disconnected;
|
||||||
|
|
||||||
@ -55,6 +63,7 @@ class BlueManage {
|
|||||||
StreamSubscription<BluetoothAdapterState>? _adapterStateStateSubscription;
|
StreamSubscription<BluetoothAdapterState>? _adapterStateStateSubscription;
|
||||||
|
|
||||||
static BlueManage? _manager;
|
static BlueManage? _manager;
|
||||||
|
|
||||||
BlueManage._init() {
|
BlueManage._init() {
|
||||||
_initBlue();
|
_initBlue();
|
||||||
}
|
}
|
||||||
@ -66,6 +75,7 @@ class BlueManage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
factory BlueManage() => shareManager()!;
|
factory BlueManage() => shareManager()!;
|
||||||
|
|
||||||
BlueManage? get manager => shareManager();
|
BlueManage? get manager => shareManager();
|
||||||
|
|
||||||
void _initBlue() {
|
void _initBlue() {
|
||||||
@ -161,6 +171,64 @@ class BlueManage {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 开始指定设备名称的扫描蓝牙设备
|
||||||
|
Future<void> startScanSingle(String deviceName, int timeout, ScanDevicesCallBack scanDevicesCallBack) async {
|
||||||
|
FlutterBluePlus.isSupported.then((isAvailable) async {
|
||||||
|
if (isAvailable) {
|
||||||
|
if (_adapterState == BluetoothAdapterState.on) {
|
||||||
|
try {
|
||||||
|
FlutterBluePlus.startScan(timeout: Duration(seconds: timeout));
|
||||||
|
Completer<dynamic> completer = Completer<dynamic>();
|
||||||
|
var subscription = FlutterBluePlus.scanResults.listen((results) {
|
||||||
|
bool isExit = results.any((element) => element.device.platformName == deviceName);
|
||||||
|
if (isExit) {
|
||||||
|
for (var scanResult in results) {
|
||||||
|
if (((scanResult.advertisementData.serviceUuids.isNotEmpty
|
||||||
|
? scanResult.advertisementData.serviceUuids[0]
|
||||||
|
: "")
|
||||||
|
.toString()
|
||||||
|
.contains("758824")) &&
|
||||||
|
(scanResult.rssi >= -100)) {
|
||||||
|
// 查询id相同的元素
|
||||||
|
final knownDeviceIndex = scanDevices
|
||||||
|
.indexWhere((d) => d.advertisementData.advName == scanResult.advertisementData.advName);
|
||||||
|
// 不存在的时候返回-1
|
||||||
|
if (knownDeviceIndex >= 0) {
|
||||||
|
scanDevices[knownDeviceIndex] = scanResult;
|
||||||
|
} else {
|
||||||
|
scanDevices.add(scanResult);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
completer.complete();
|
||||||
|
}
|
||||||
|
}, onError: (e) {
|
||||||
|
Get.log(
|
||||||
|
"Scan Error:$e",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
FlutterBluePlus.cancelWhenScanComplete(subscription);
|
||||||
|
await completer.future;
|
||||||
|
scanDevicesCallBack(scanDevices);
|
||||||
|
subscription.cancel();
|
||||||
|
} catch (e) {
|
||||||
|
Get.log("扫描失败");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
if (Platform.isAndroid) {
|
||||||
|
await FlutterBluePlus.turnOn();
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
Get.log("Error Turning On:");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Get.log("开始扫描 蓝牙不可用,不能进行蓝牙操作");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/// 开始扫描蓝牙设备
|
/// 开始扫描蓝牙设备
|
||||||
Future<void> startScan(int timeout, ScanDevicesCallBack scanDevicesCallBack, {List<Guid>? idList}) async {
|
Future<void> startScan(int timeout, ScanDevicesCallBack scanDevicesCallBack, {List<Guid>? idList}) async {
|
||||||
FlutterBluePlus.isSupported.then((isAvailable) async {
|
FlutterBluePlus.isSupported.then((isAvailable) async {
|
||||||
@ -179,9 +247,15 @@ class BlueManage {
|
|||||||
// Get.log("scanResult.device.advName:${scanResult.device.advName}"
|
// Get.log("scanResult.device.advName:${scanResult.device.advName}"
|
||||||
// " scanResult.advertisementData.serviceUuids:${scanResult.advertisementData.serviceUuids}"
|
// " scanResult.advertisementData.serviceUuids:${scanResult.advertisementData.serviceUuids}"
|
||||||
// " rssi:${scanResult.rssi}");
|
// " rssi:${scanResult.rssi}");
|
||||||
if (((scanResult.advertisementData.serviceUuids.isNotEmpty ? scanResult.advertisementData.serviceUuids[0] : "").toString().contains("758824")) && (scanResult.rssi >= -100)) {
|
if (((scanResult.advertisementData.serviceUuids.isNotEmpty
|
||||||
|
? scanResult.advertisementData.serviceUuids[0]
|
||||||
|
: "")
|
||||||
|
.toString()
|
||||||
|
.contains("758824")) &&
|
||||||
|
(scanResult.rssi >= -100)) {
|
||||||
// 查询id相同的元素
|
// 查询id相同的元素
|
||||||
final knownDeviceIndex = scanDevices.indexWhere((d) => d.advertisementData.advName == scanResult.advertisementData.advName);
|
final knownDeviceIndex = scanDevices
|
||||||
|
.indexWhere((d) => d.advertisementData.advName == scanResult.advertisementData.advName);
|
||||||
// 不存在的时候返回-1
|
// 不存在的时候返回-1
|
||||||
if (knownDeviceIndex >= 0) {
|
if (knownDeviceIndex >= 0) {
|
||||||
scanDevices[knownDeviceIndex] = scanResult;
|
scanDevices[knownDeviceIndex] = scanResult;
|
||||||
@ -194,7 +268,9 @@ class BlueManage {
|
|||||||
// EventBusManager().eventBusFir(scanDevices);
|
// EventBusManager().eventBusFir(scanDevices);
|
||||||
// FlutterBluePlus.stopScan();
|
// FlutterBluePlus.stopScan();
|
||||||
}, onError: (e) {
|
}, onError: (e) {
|
||||||
Get.log("Scan Error:$e", );
|
Get.log(
|
||||||
|
"Scan Error:$e",
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
FlutterBluePlus.cancelWhenScanComplete(subscription);
|
FlutterBluePlus.cancelWhenScanComplete(subscription);
|
||||||
@ -203,11 +279,14 @@ class BlueManage {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
openBlue();
|
if (Platform.isAndroid) {
|
||||||
|
await FlutterBluePlus.turnOn();
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
Get.log("Error Turning On:");
|
Get.log("Error Turning On:");
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
;
|
||||||
} else {
|
} else {
|
||||||
Get.log("开始扫描 蓝牙不可用,不能进行蓝牙操作");
|
Get.log("开始扫描 蓝牙不可用,不能进行蓝牙操作");
|
||||||
}
|
}
|
||||||
@ -215,7 +294,8 @@ class BlueManage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 调用发送数据
|
/// 调用发送数据
|
||||||
Future<void> bludSendData(String deviceName, ConnectStateCallBack stateCallBack, {bool isAddEquipment = false}) async {
|
Future<void> bludSendData(String deviceName, ConnectStateCallBack stateCallBack,
|
||||||
|
{bool isAddEquipment = false}) async {
|
||||||
FlutterBluePlus.isSupported.then((isAvailable) async {
|
FlutterBluePlus.isSupported.then((isAvailable) async {
|
||||||
if (isAvailable) {
|
if (isAvailable) {
|
||||||
if (_adapterState == BluetoothAdapterState.on) {
|
if (_adapterState == BluetoothAdapterState.on) {
|
||||||
@ -247,11 +327,14 @@ class BlueManage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 连接
|
/// 连接
|
||||||
Future<void> _connect(String deviceName, ConnectStateCallBack connectStateCallBack, {bool isAddEquipment = false}) async {
|
Future<void> _connect(String deviceName, ConnectStateCallBack connectStateCallBack,
|
||||||
|
{bool isAddEquipment = false}) async {
|
||||||
connectDeviceName = deviceName;
|
connectDeviceName = deviceName;
|
||||||
List<ScanResult> devicesList = scanDevices;
|
List<ScanResult> devicesList = scanDevices;
|
||||||
if(isAddEquipment == false){
|
//判断列表里面有设备则不开启扫描
|
||||||
startScan(10, (List<ScanResult> scanDevices){
|
bool isExistDevice = scanDevices.any((element) => element.device.platformName == connectDeviceName);
|
||||||
|
if (isAddEquipment == false && isExistDevice == false) {
|
||||||
|
startScanSingle(deviceName, 10, (List<ScanResult> scanDevices) {
|
||||||
Get.log("扫描到的设备:$scanDevices");
|
Get.log("扫描到的设备:$scanDevices");
|
||||||
devicesList = scanDevices;
|
devicesList = scanDevices;
|
||||||
_connectDevice(devicesList, deviceName, connectStateCallBack, isAddEquipment: isAddEquipment);
|
_connectDevice(devicesList, deviceName, connectStateCallBack, isAddEquipment: isAddEquipment);
|
||||||
@ -261,7 +344,9 @@ class BlueManage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _connectDevice( List<ScanResult> devicesList, String deviceName, ConnectStateCallBack connectStateCallBack, {bool isAddEquipment = false}) async {
|
Future<void> _connectDevice(
|
||||||
|
List<ScanResult> devicesList, String deviceName, ConnectStateCallBack connectStateCallBack,
|
||||||
|
{bool isAddEquipment = false}) async {
|
||||||
// 判断数组列表里面是否有这个设备
|
// 判断数组列表里面是否有这个设备
|
||||||
Get.log("devicesList:$devicesList");
|
Get.log("devicesList:$devicesList");
|
||||||
final knownDeviceIndex = devicesList.indexWhere((d) => d.advertisementData.advName == deviceName);
|
final knownDeviceIndex = devicesList.indexWhere((d) => d.advertisementData.advName == deviceName);
|
||||||
@ -287,7 +372,7 @@ class BlueManage {
|
|||||||
await stopScan();
|
await stopScan();
|
||||||
|
|
||||||
if ((scanResult!.advertisementData.serviceUuids[0].toString()[31] == "0") && isAddEquipment == false) {
|
if ((scanResult!.advertisementData.serviceUuids[0].toString()[31] == "0") && isAddEquipment == false) {
|
||||||
connectStateCallBack(BluetoothConnectionState.disconnected!);
|
connectStateCallBack(BluetoothConnectionState.disconnected);
|
||||||
EasyLoading.showToast("该锁已被重置", duration: 2000.milliseconds);
|
EasyLoading.showToast("该锁已被重置", duration: 2000.milliseconds);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -310,7 +395,7 @@ class BlueManage {
|
|||||||
|
|
||||||
if (attempt >= maxAttempts) {
|
if (attempt >= maxAttempts) {
|
||||||
Get.log('Failed to connect after $maxAttempts attempts.');
|
Get.log('Failed to connect after $maxAttempts attempts.');
|
||||||
connectStateCallBack(BluetoothConnectionState.disconnected!);
|
connectStateCallBack(BluetoothConnectionState.disconnected);
|
||||||
}
|
}
|
||||||
|
|
||||||
// await bluetoothConnectDevice!.connect();
|
// await bluetoothConnectDevice!.connect();
|
||||||
@ -351,9 +436,11 @@ class BlueManage {
|
|||||||
|
|
||||||
// 听上报来的数据,参数来自前面扫描到的结果
|
// 听上报来的数据,参数来自前面扫描到的结果
|
||||||
var allData = <int>[];
|
var allData = <int>[];
|
||||||
|
|
||||||
// 保存上一次的数据,用来判断是否收到重复的数据
|
// 保存上一次的数据,用来判断是否收到重复的数据
|
||||||
var lastTimeData = <int>[];
|
var lastTimeData = <int>[];
|
||||||
int? dataLen;
|
int? dataLen;
|
||||||
|
|
||||||
_subScribeToCharacteristic(BluetoothCharacteristic characteristic) async {
|
_subScribeToCharacteristic(BluetoothCharacteristic characteristic) async {
|
||||||
final subscription = characteristic.onValueReceived.listen((data) {
|
final subscription = characteristic.onValueReceived.listen((data) {
|
||||||
Get.log("启动对特性的通知。当特性的值发生变化时,设备会发送一个通知");
|
Get.log("启动对特性的通知。当特性的值发生变化时,设备会发送一个通知");
|
||||||
@ -456,8 +543,7 @@ class BlueManage {
|
|||||||
// " service.characteristics:${service.characteristics}\n\n"
|
// " service.characteristics:${service.characteristics}\n\n"
|
||||||
// " service.includedServices:${service.includedServices}");
|
// " service.includedServices:${service.includedServices}");
|
||||||
if (service.uuid == _serviceIdConnect) {
|
if (service.uuid == _serviceIdConnect) {
|
||||||
for (BluetoothCharacteristic characteristic in service
|
for (BluetoothCharacteristic characteristic in service.characteristics) {
|
||||||
.characteristics) {
|
|
||||||
// print("44444 characteristic.remoteId:${characteristic.remoteId}"
|
// print("44444 characteristic.remoteId:${characteristic.remoteId}"
|
||||||
// " characteristic.uuid:${characteristic.uuid}\n\n"
|
// " characteristic.uuid:${characteristic.uuid}\n\n"
|
||||||
// " characteristic.secondaryServiceUuid:${characteristic
|
// " characteristic.secondaryServiceUuid:${characteristic
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user