2023-08-08 09:42:35 +08:00
import ' package:flutter/material.dart ' ;
import ' package:flutter_reactive_ble/flutter_reactive_ble.dart ' ;
import ' package:star_lock/blue/sender_manage.dart ' ;
import ' ../app_settings/app_settings.dart ' ;
import ' io_tool/io_tool.dart ' ;
import ' io_tool/manager_event_bus.dart ' ;
2023-08-09 10:07:27 +08:00
import ' reciver_data.dart ' ;
2023-08-08 09:42:35 +08:00
typedef ScanResultCallBack = void Function ( List < DiscoveredDevice > devices ) ;
class BlueManage {
FlutterReactiveBle ? _flutterReactiveBle ;
List < DiscoveredDevice > _scanDevices = [ ] ;
QualifiedCharacteristic ? qualifiedCharacteristic ;
DiscoveredCharacteristic ? getDiscoveredCharacteristic ;
Uuid serviceId = Uuid . parse ( ' 0000FFF0-0000-1000-8000-00805F9B34FB ' ) ;
final int _limitLen = 20 ;
static BlueManage ? _manager ;
BlueManage . _init ( ) ;
static BlueManage ? shareManager ( ) {
_manager ? ? = BlueManage . _init ( ) ;
_manager ! . _initBlue ( ) ;
return _manager ;
}
factory BlueManage ( ) = > shareManager ( ) ! ;
BlueManage ? get manager = > shareManager ( ) ;
void _initBlue ( ) {
_flutterReactiveBle = FlutterReactiveBle ( ) ;
}
/// 开始扫描蓝牙设备
void startScan ( ScanResultCallBack scanResultCallBack ) {
_scanDevices . clear ( ) ;
_flutterReactiveBle ! . scanForDevices ( withServices: [ ] ) . listen ( ( device ) {
// 判断名字为空的直接剔除
if ( device . name . isEmpty ) {
return ;
}
2023-08-10 18:56:29 +08:00
// print("startScanDevice:${device}");
2023-08-08 09:42:35 +08:00
if ( ( ( device . serviceUuids . isNotEmpty ? device . serviceUuids [ 0 ] : " " ) . toString ( ) . contains ( " 758824 " ) ) & & ( device . rssi > = - 100 ) ) {
// print("11111111111111111:${device}");
final knownDeviceIndex = _scanDevices . indexWhere ( ( d ) = > d . id = = device . id ) ;
if ( knownDeviceIndex > = 0 ) {
_scanDevices [ knownDeviceIndex ] = device ;
} else {
_scanDevices . add ( device ) ;
}
// EventBusManager().eventBusFir(_scanDevices);
scanResultCallBack ( _scanDevices ) ;
}
// _pushState();
} , onError: ( Object e ) {
print ( ' Device scan fails with error: $ e ' ) ;
} ) ;
}
/// 连接监听状态
Future < void > connect ( String deviceMAC ) async {
print ( " connect: $ deviceMAC " ) ;
_flutterReactiveBle ! . connectToDevice ( id: deviceMAC ) . listen ( ( connectionStateUpdate ) {
print ( ' ConnectionState for device $ deviceMAC : ${ connectionStateUpdate . connectionState } ' ) ;
// EventBusManager().eventBusFir(connectionStateUpdate);
if ( connectionStateUpdate . connectionState = = DeviceConnectionState . connected ) {
// getPublicKey(update.deviceId);
// 如果状态是连接的开始发现服务
discoverServices ( deviceMAC ) ;
}
} , onError: ( Object e ) {
print ( ' Connecting to device $ deviceMAC resulted in error $ e ' ) ;
}
) ;
}
Future < void > disconnect ( String deviceMAC ) async {
try {
print ( ' disconnecting to device: $ deviceMAC ' ) ;
} on Exception catch ( e , _ ) {
print ( " Error disconnecting from a device: $ e " ) ;
} finally {
EventBusManager ( ) . eventBusFir ( ConnectionStateUpdate (
deviceId: deviceMAC ,
connectionState: DeviceConnectionState . disconnected ,
failure: null ,
) ) ;
}
}
// 扫描服务,并过滤服务
Future < List < DiscoveredService > > discoverServices ( String deviceId ) async {
try {
2023-08-09 15:42:26 +08:00
// print('Start discovering services for: $deviceId');
2023-08-08 09:42:35 +08:00
List < DiscoveredService > result = await _flutterReactiveBle ! . discoverServices ( deviceId ) ;
2023-08-09 15:42:26 +08:00
// print("mmmmmmmmm$result");
2023-08-08 09:42:35 +08:00
if ( result . isNotEmpty ) {
for ( var i = 0 ; i < result . length ; i + + ) {
DiscoveredService discoveredService = result [ i ] ;
2023-08-09 15:42:26 +08:00
// print("objectdiscoveredService.serviceId.toString() ${discoveredService.serviceId.toString()}");
2023-08-08 09:42:35 +08:00
if ( discoveredService . serviceId . toString ( ) = = " fff0 " ) {
// getDiscoveredService = discoveredService;
for ( var j = 0 ; j < discoveredService . characteristics . length ; j + + ) {
DiscoveredCharacteristic discoveredCharacteristic = discoveredService . characteristics [ j ] ;
// print("hhhhhhhhhh${result[i].characteristicIds[j].toString()}");
if ( discoveredCharacteristic . characteristicId . toString ( ) = = " fff1 " ) {
// 订阅用
getDiscoveredCharacteristic = discoveredCharacteristic ;
// print("1111111111111111characteristicId:${result[i].characteristicIds[j].toString()} serviceId:${result[i].serviceId} deviceId:$deviceId");
}
if ( discoveredCharacteristic . characteristicId . toString ( ) = = " fff2 " ) {
2023-08-09 15:42:26 +08:00
// print("1111111111111111characteristicId:${discoveredCharacteristic.characteristicId} serviceId:${serviceId} deviceId:$deviceId");
2023-08-08 09:42:35 +08:00
qualifiedCharacteristic = QualifiedCharacteristic ( characteristicId: discoveredCharacteristic . characteristicId , serviceId: serviceId , deviceId: deviceId ) ;
}
}
}
}
}
subScribeToCharacteristic ( QualifiedCharacteristic ( characteristicId: getDiscoveredCharacteristic ! . characteristicId , serviceId: Uuid . parse ( " fff0 " ) , deviceId: deviceId ) ) ;
print ( ' Discovering services finished ' ) ;
EventBusManager ( ) . eventBusFir ( qualifiedCharacteristic ) ;
return result ;
} on Exception catch ( e ) {
print ( ' Error occurred when discovering services: $ e ' ) ;
rethrow ;
}
}
// 听上报来的数据,参数来自前面扫描到的结果
subScribeToCharacteristic ( QualifiedCharacteristic characteristic ) {
_flutterReactiveBle ! . subscribeToCharacteristic ( characteristic ) . listen ( ( data ) {
// code to handle incoming data
print ( " subscribeToCharacteristic: deviceId = ${ characteristic . deviceId } characteristicId = ${ characteristic . characteristicId } ---上报来的数据data = $ data " ) ;
2023-08-09 10:07:27 +08:00
CommandReciverManager . appDataReceive ( data , " " ) ;
2023-08-08 09:42:35 +08:00
} , onError: ( dynamic error ) {
print ( " subscribeToCharacteristic error: $ error " ) ;
} ) ;
}
// 写入
Future < void > writeCharacteristicWithResponse ( QualifiedCharacteristic characteristic , List < int > value ) async {
2023-08-11 14:50:42 +08:00
print ( ' Write with characteristicId: ${ characteristic . characteristicId } serviceId: ${ characteristic . serviceId } deviceId: ${ characteristic . deviceId } value : $ value \n hexStr: ${ radixString ( value ) } ' ) ;
2023-08-08 09:42:35 +08:00
2023-08-09 10:07:27 +08:00
try {
List < int > valueList = value ;
List subData = splitList ( valueList , _limitLen ) ;
print ( ' 得到的分割数据: $ subData ' ) ;
for ( int i = 0 ; i < subData . length ; i + + ) {
2023-08-09 15:42:26 +08:00
await _flutterReactiveBle ! . writeCharacteristicWithResponse ( characteristic , value: subData [ i ] ) . then ( ( value ) async {
2023-08-09 10:07:27 +08:00
await Future . delayed ( const Duration ( milliseconds: 1 ) )
. then ( ( value ) async {
2023-08-10 09:52:05 +08:00
print ( ' 分包发送成功了 ' ) ;
2023-08-09 10:07:27 +08:00
} ) ;
2023-08-08 09:42:35 +08:00
} ) ;
2023-08-09 10:07:27 +08:00
}
2023-08-08 09:42:35 +08:00
} on Exception catch ( e , s ) {
2023-08-09 15:42:26 +08:00
print ( ' Error occurred when writing: $ e ' ) ;
2023-08-08 09:42:35 +08:00
// ignore: avoid_print
print ( s ) ;
rethrow ;
}
}
2023-08-09 10:07:27 +08:00
2023-08-08 09:42:35 +08:00
// 读取
Future < List < int > > readCharacteristic ( QualifiedCharacteristic characteristic ) async {
try {
final result = await _flutterReactiveBle ! . readCharacteristic ( characteristic ) ;
print ( " readListresult $ result " ) ;
return result ;
} on Exception catch ( e , s ) {
print ( ' Error occurred when reading ${ characteristic . characteristicId } : $ e ' , ) ;
rethrow ;
}
}
Future < void > writeCharacteristicWithoutResponse (
QualifiedCharacteristic characteristic , List < int > value ) async {
try {
await _flutterReactiveBle ! . writeCharacteristicWithoutResponse ( characteristic ,
value: value ) ;
} on Exception catch ( e , s ) {
// ignore: avoid_print
print ( s ) ;
rethrow ;
}
}
}