2023-08-08 16:30:53 +08:00
import ' dart:async ' ;
2023-08-08 09:42:35 +08:00
2023-08-08 16:30:53 +08:00
import ' package:flutter/foundation.dart ' ;
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 ' ;
typedef ScanResultCallBack = void Function ( List < DiscoveredDevice > devices ) ;
2023-08-08 16:30:53 +08:00
class BlueManage {
2023-08-08 09:42:35 +08:00
FlutterReactiveBle ? _flutterReactiveBle ;
List < DiscoveredDevice > _scanDevices = [ ] ;
QualifiedCharacteristic ? qualifiedCharacteristic ;
DiscoveredCharacteristic ? getDiscoveredCharacteristic ;
Uuid serviceId = Uuid . parse ( ' 0000FFF0-0000-1000-8000-00805F9B34FB ' ) ;
final int _limitLen = 20 ;
final int _sleepTimes = AppPlatform . isAndroid ? 6 : 0 ;
static BlueManage ? _manager ;
BlueManage . _init ( ) ;
2023-08-08 16:30:53 +08:00
static BlueManage ? shareManager ( ) {
_manager ? ? = BlueManage . _init ( ) ;
2023-08-08 09:42:35 +08:00
_manager ! . _initBlue ( ) ;
return _manager ;
}
factory BlueManage ( ) = > shareManager ( ) ! ;
BlueManage ? get manager = > shareManager ( ) ;
2023-08-08 16:30:53 +08:00
void _initBlue ( ) {
2023-08-08 09:42:35 +08:00
_flutterReactiveBle = FlutterReactiveBle ( ) ;
}
/// 开始扫描蓝牙设备
void startScan ( ScanResultCallBack scanResultCallBack ) {
_scanDevices . clear ( ) ;
_flutterReactiveBle ! . scanForDevices ( withServices: [ ] ) . listen ( ( device ) {
// 判断名字为空的直接剔除
2023-08-08 16:30:53 +08:00
if ( device . name . isEmpty ) {
2023-08-08 09:42:35 +08:00
return ;
}
2023-08-08 16:30:53 +08:00
if ( ( ( device . serviceUuids . isNotEmpty ? device . serviceUuids [ 0 ] : " " )
. toString ( )
. contains ( " 758824 " ) ) & &
( device . rssi > = - 100 ) ) {
2023-08-08 09:42:35 +08:00
// print("11111111111111111:${device}");
2023-08-08 16:30:53 +08:00
final knownDeviceIndex =
_scanDevices . indexWhere ( ( d ) = > d . id = = device . id ) ;
2023-08-08 09:42:35 +08:00
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 " ) ;
2023-08-08 16:30:53 +08:00
_flutterReactiveBle ! . connectToDevice ( id: deviceMAC ) . listen (
( connectionStateUpdate ) {
print (
' ConnectionState for device $ deviceMAC : ${ connectionStateUpdate . connectionState } ' ) ;
2023-08-08 09:42:35 +08:00
// EventBusManager().eventBusFir(connectionStateUpdate);
2023-08-08 16:30:53 +08:00
if ( connectionStateUpdate . connectionState = =
DeviceConnectionState . connected ) {
2023-08-08 09:42:35 +08:00
// getPublicKey(update.deviceId);
// 如果状态是连接的开始发现服务
discoverServices ( deviceMAC ) ;
}
2023-08-08 16:30:53 +08:00
} , onError: ( Object e ) {
print ( ' Connecting to device $ deviceMAC resulted in error $ e ' ) ;
} ) ;
2023-08-08 09:42:35 +08:00
}
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 {
print ( ' Start discovering services for: $ deviceId ' ) ;
2023-08-08 16:30:53 +08:00
List < DiscoveredService > result =
await _flutterReactiveBle ! . discoverServices ( deviceId ) ;
2023-08-08 09:42:35 +08:00
print ( " mmmmmmmmm $ result " ) ;
2023-08-08 16:30:53 +08:00
if ( result . isNotEmpty ) {
2023-08-08 09:42:35 +08:00
for ( var i = 0 ; i < result . length ; i + + ) {
DiscoveredService discoveredService = result [ i ] ;
2023-08-08 16:30:53 +08:00
print (
" objectdiscoveredService.serviceId.toString() ${ discoveredService . serviceId . toString ( ) } " ) ;
if ( discoveredService . serviceId . toString ( ) = = " fff0 " ) {
2023-08-08 09:42:35 +08:00
// getDiscoveredService = discoveredService;
for ( var j = 0 ; j < discoveredService . characteristics . length ; j + + ) {
2023-08-08 16:30:53 +08:00
DiscoveredCharacteristic discoveredCharacteristic =
discoveredService . characteristics [ j ] ;
2023-08-08 09:42:35 +08:00
// print("hhhhhhhhhh${result[i].characteristicIds[j].toString()}");
2023-08-08 16:30:53 +08:00
if ( discoveredCharacteristic . characteristicId . toString ( ) = =
" fff1 " ) {
2023-08-08 09:42:35 +08:00
// 订阅用
getDiscoveredCharacteristic = discoveredCharacteristic ;
// print("1111111111111111characteristicId:${result[i].characteristicIds[j].toString()} serviceId:${result[i].serviceId} deviceId:$deviceId");
}
2023-08-08 16:30:53 +08:00
if ( discoveredCharacteristic . characteristicId . toString ( ) = =
" fff2 " ) {
print (
" 1111111111111111characteristicId: ${ discoveredCharacteristic . characteristicId } serviceId: ${ serviceId } deviceId: $ deviceId " ) ;
qualifiedCharacteristic = QualifiedCharacteristic (
characteristicId: discoveredCharacteristic . characteristicId ,
serviceId: serviceId ,
deviceId: deviceId ) ;
2023-08-08 09:42:35 +08:00
}
}
}
}
}
2023-08-08 16:30:53 +08:00
subScribeToCharacteristic ( QualifiedCharacteristic (
characteristicId: getDiscoveredCharacteristic ! . characteristicId ,
serviceId: Uuid . parse ( " fff0 " ) ,
deviceId: deviceId ) ) ;
2023-08-08 09:42:35 +08:00
print ( ' Discovering services finished ' ) ;
EventBusManager ( ) . eventBusFir ( qualifiedCharacteristic ) ;
return result ;
} on Exception catch ( e ) {
print ( ' Error occurred when discovering services: $ e ' ) ;
rethrow ;
}
}
// 听上报来的数据,参数来自前面扫描到的结果
subScribeToCharacteristic ( QualifiedCharacteristic characteristic ) {
2023-08-08 16:30:53 +08:00
print (
' Subscribing to characteristicId: ${ characteristic . characteristicId } serviceId: ${ characteristic . serviceId } deviceId: ${ characteristic . deviceId } ' ) ;
_flutterReactiveBle ! . subscribeToCharacteristic ( characteristic ) . listen (
( data ) {
2023-08-08 09:42:35 +08:00
// code to handle incoming data
2023-08-08 16:30:53 +08:00
print (
" subscribeToCharacteristic: deviceId = ${ characteristic . deviceId } characteristicId = ${ characteristic . characteristicId } ---上报来的数据data = $ data " ) ;
2023-08-08 09:42:35 +08:00
} , onError: ( dynamic error ) {
print ( " subscribeToCharacteristic error: $ error " ) ;
} ) ;
// return _ble.subscribeToCharacteristic(characteristic);
}
// 写入
2023-08-08 16:30:53 +08:00
Future < void > writeCharacteristicWithResponse (
QualifiedCharacteristic characteristic , List < int > value ) async {
print (
' Write with characteristicId: ${ characteristic . characteristicId } serviceId: ${ characteristic . serviceId } deviceId: ${ characteristic . deviceId } value : $ value ' ) ;
2023-08-08 09:42:35 +08:00
2023-08-08 16:30:53 +08:00
try {
List < int > valueList = value ;
List subData = splitList ( valueList , _limitLen ) ;
print ( ' 得到的分割数据: $ subData ' ) ;
for ( int i = 0 ; i < subData . length ; i + + ) {
await _flutterReactiveBle !
. writeCharacteristicWithResponse ( characteristic , value: subData [ i ] )
. then ( ( value ) async {
await Future . delayed ( const Duration ( milliseconds: 1 ) )
. then ( ( value ) async {
print ( ' 成功了么 ' ) ;
} ) ;
2023-08-08 09:42:35 +08:00
} ) ;
2023-08-08 16:30:53 +08:00
}
2023-08-08 09:42:35 +08:00
} on Exception catch ( e , s ) {
2023-08-08 16:30:53 +08:00
print (
' Error occurred when writing: $ e ' ,
) ;
2023-08-08 09:42:35 +08:00
// ignore: avoid_print
print ( s ) ;
rethrow ;
}
}
// 读取
2023-08-08 16:30:53 +08:00
Future < List < int > > readCharacteristic (
QualifiedCharacteristic characteristic ) async {
2023-08-08 09:42:35 +08:00
try {
2023-08-08 16:30:53 +08:00
final result =
await _flutterReactiveBle ! . readCharacteristic ( characteristic ) ;
2023-08-08 09:42:35 +08:00
print ( " readListresult $ result " ) ;
return result ;
} on Exception catch ( e , s ) {
2023-08-08 16:30:53 +08:00
print (
' Error occurred when reading ${ characteristic . characteristicId } : $ e ' ,
) ;
2023-08-08 09:42:35 +08:00
rethrow ;
}
}
Future < void > writeCharacteristicWithoutResponse (
QualifiedCharacteristic characteristic , List < int > value ) async {
try {
2023-08-08 16:30:53 +08:00
await _flutterReactiveBle !
. writeCharacteristicWithoutResponse ( characteristic , value: value ) ;
2023-08-08 09:42:35 +08:00
} on Exception catch ( e , s ) {
// ignore: avoid_print
print ( s ) ;
rethrow ;
}
}
2023-08-08 16:30:53 +08:00
}