Commit 446eab12 authored by hywang's avatar hywang

1.使用新的ble库

parent 7ade97c9
...@@ -10,6 +10,8 @@ A few resources to get you started if this is your first Flutter project: ...@@ -10,6 +10,8 @@ A few resources to get you started if this is your first Flutter project:
- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab) - [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook) - [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook)
flutter pub run build_runner build
For help getting started with Flutter development, view the For help getting started with Flutter development, view the
[online documentation](https://docs.flutter.dev/), which offers tutorials, [online documentation](https://docs.flutter.dev/), which offers tutorials,
......
...@@ -2,17 +2,14 @@ import 'dart:async'; ...@@ -2,17 +2,14 @@ import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:anchor_collect_flutter/utils/dialog_utils.dart'; import 'package:anchor_collect_flutter/utils/dialog_utils.dart';
import 'package:anchor_collect_flutter/utils/empty_utils.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart'; import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:isar/isar.dart';
import 'package:permission_handler/permission_handler.dart'; import 'package:permission_handler/permission_handler.dart';
import 'package:uuid/uuid.dart';
import '../models/cattle_resume_entity.dart'; import '../rfid/rfid_plugin.dart';
import '../models/dict_entity.dart';
import '../models/user_model.dart';
typedef void DiscoveredCallback(args); typedef void DiscoveredCallback(args);
typedef void ConnectCallback(args); typedef void ConnectCallback(args);
...@@ -23,36 +20,37 @@ class BleService extends GetxService { ...@@ -23,36 +20,37 @@ class BleService extends GetxService {
late DiscoveredCallback? discoveredCallback; late DiscoveredCallback? discoveredCallback;
late ConnectCallback? connectCallback; late ConnectCallback? connectCallback;
late BluetoothService _service; late BluetoothService selectService;
late BluetoothCharacteristic _writeCharacteristic; late BluetoothCharacteristic _writeCharacteristic;
late BluetoothCharacteristic _notifyCharacteristic; late BluetoothCharacteristic _notifyCharacteristic;
late
// 读取数据的 Stream
List<BluetoothDevice> _systemDevices = [];
// //
List<ScanResult> _scanResults = []; RxList<ScanResult> scanResults = <ScanResult>[].obs;
RxBool isScanning = false.obs; RxBool isScanning = false.obs;
bool isConnect = false;
late StreamSubscription<List<ScanResult>> _scanResultsSubscription; late StreamSubscription<List<ScanResult>> _scanResultsSubscription;
late StreamSubscription<bool> _isScanningSubscription; late StreamSubscription<bool> _isScanningSubscription;
late StreamSubscription<BluetoothConnectionState> late StreamSubscription<BluetoothConnectionState> _connectionStateSubscription;
_connectionStateSubscription;
late StreamSubscription<List<int>> _lastValueSubscription; late StreamSubscription<List<int>> _lastValueSubscription;
late StreamSubscription<List<int>> _valueReceivedSubscription; late StreamSubscription<List<int>> _valueReceivedSubscription;
late BluetoothDevice selectedDevice;
Future<BleService> init() async { Future<BleService> init() async {
_isScanningSubscription = FlutterBluePlus.isScanning.listen((state) { _isScanningSubscription = FlutterBluePlus.isScanning.listen((state) {
isScanning.value = state; isScanning.value = state;
}); });
_scanResultsSubscription = FlutterBluePlus.scanResults.listen((results) { _scanResultsSubscription = FlutterBluePlus.scanResults.listen((results) {
_scanResults = results; List<ScanResult> tempList = List.from(results);
for (var element in tempList) {
if (!EmptyUtils.isStrNotEmpty(element.device.platformName)) {
tempList.remove(element);
}
}
scanResults.value = tempList;
}, onError: (e) { }, onError: (e) {
DialogUtils.showToast('扫描错误'); DialogUtils.showToast('扫描错误');
}); });
...@@ -60,11 +58,10 @@ class BleService extends GetxService { ...@@ -60,11 +58,10 @@ class BleService extends GetxService {
} }
// 切换扫描状态 // 切换扫描状态
Future<void> toggleScan(DiscoveredCallback callback) async { Future<void> toggleScan() async {
discoveredCallback = callback; // discoveredCallback = callback;
if (isScanning.value) { if (isScanning.value) {
FlutterBluePlus.stopScan(); stopScan();
isScanning.value = false;
} else { } else {
await scanDevices(); await scanDevices();
} }
...@@ -86,60 +83,70 @@ class BleService extends GetxService { ...@@ -86,60 +83,70 @@ class BleService extends GetxService {
// so instead we only ask for 1/8 of them // so instead we only ask for 1/8 of them
int divisor = Platform.isAndroid ? 8 : 1; int divisor = Platform.isAndroid ? 8 : 1;
await FlutterBluePlus.startScan( await FlutterBluePlus.startScan(
timeout: const Duration(seconds: 15), timeout: const Duration(seconds: 15), continuousUpdates: true, continuousDivisor: divisor);
continuousUpdates: true,
continuousDivisor: divisor);
} catch (e) { } catch (e) {
DialogUtils.showToast('扫描错误'); DialogUtils.showToast('扫描错误');
} }
///扫描10秒 ///扫描10秒
// Future.delayed(const Duration(seconds: 15), () async { Future.delayed(const Duration(seconds: 15), () async {
// FlutterBluePlus.stopScan(); FlutterBluePlus.stopScan();
// isScanning.value = false; isScanning.value = false;
// DialogUtils.showToast('自动停止扫描'); DialogUtils.dismissDialog();
// }); });
}
stopScan() {
FlutterBluePlus.stopScan();
isScanning.value = false;
} }
// 连接设备 // 连接设备
Future<void> connectToDevice(BluetoothDevice device, BuildContext context, ConnectCallback callback) async { Future<void> connectToDevice(int index, ConnectCallback callback) async {
connectCallback = callback; connectCallback = callback;
selectedDevice = device; // 设置当前选中的设备 var selectedDevice = scanResults[index].device; // 设置当前选中的设备
// 在连接设备之前,停止扫描 // 在连接设备之前,停止扫描
await FlutterBluePlus.stopScan(); await FlutterBluePlus.stopScan();
isScanning.value = false; // 设置 isScanning 为 false isScanning.value = false; // 设置 isScanning 为 false
DialogUtils.showLoadingDialog('正在连接到 ${device.name}'); DialogUtils.showLoadingDialog('正在连接到 ${selectedDevice.advName}');
// listen for disconnection _connectionStateSubscription = selectedDevice.connectionState.listen((BluetoothConnectionState state) async {
_connectionStateSubscription = device.connectionState.listen((BluetoothConnectionState state) async {
if (state == BluetoothConnectionState.disconnected) { if (state == BluetoothConnectionState.disconnected) {
// 1. typically, start a periodic timer that tries to isConnect = false;
// reconnect, or just call connect() again right now if (kDebugMode) {
// 2. you must always re-discover services after disconnection! print("${selectedDevice.disconnectReason!.code} ${selectedDevice.disconnectReason!.description}");
DialogUtils.showToast('断开连接'); }
print(
"${device.disconnectReason!.code} ${device.disconnectReason!.description}");
} else if (state == BluetoothConnectionState.connected) { } else if (state == BluetoothConnectionState.connected) {
// Note: You must call discoverServices after every re-connection! if (Platform.isAndroid) {
List<BluetoothService> services = await device.discoverServices(); await selectedDevice.requestMtu(512);
}
List<BluetoothService> services = await selectedDevice.discoverServices();
services.forEach((service) { services.forEach((service) {
//打印所有服务的 uuid //打印所有服务的 uuid
if (kDebugMode) {
print('service uuid: ${service.uuid}'); print('service uuid: ${service.uuid}');
}
if (!service.serviceUuid.toString().contains('000180')) { if (!service.serviceUuid.toString().contains('000180')) {
_service = service; selectService = service;
//打印 service uuid //打印 service uuid
print('service uuid: ${service.uuid}'); if (kDebugMode) {
print('service 000180 uuid: ${service.uuid}');
}
var characteristics = service.characteristics; var characteristics = service.characteristics;
for (BluetoothCharacteristic c in characteristics) { for (BluetoothCharacteristic c in characteristics) {
if (c.properties.write) { if (c.properties.write) {
//打印写的特征值 uuid //打印写的特征值 uuid
if (kDebugMode) {
print('write characteristic uuid: ${c.uuid}'); print('write characteristic uuid: ${c.uuid}');
}
_writeCharacteristic = c; _writeCharacteristic = c;
} }
if (c.properties.notify) { if (c.properties.notify) {
//打印通知的特征值 uuid //打印通知的特征值 uuid
if (kDebugMode) {
print('notify characteristic uuid: ${c.uuid}'); print('notify characteristic uuid: ${c.uuid}');
}
_notifyCharacteristic = c; _notifyCharacteristic = c;
c.setNotifyValue(true); c.setNotifyValue(true);
...@@ -147,36 +154,38 @@ class BleService extends GetxService { ...@@ -147,36 +154,38 @@ class BleService extends GetxService {
// print(value); // print(value);
// }); // });
_valueReceivedSubscription = c.onValueReceived.listen((value) { _valueReceivedSubscription = c.onValueReceived.listen((value) {
print(value); RfidPlugin.pushRemoteRFIDData(value);
}); });
} }
} }
} }
}); });
if (connectCallback != null) {
connectCallback!('');
}
DialogUtils.dismissDialog();
} else if (state == BluetoothConnectionState.connecting) { } else if (state == BluetoothConnectionState.connecting) {
isConnect = false;
DialogUtils.showToast('正在连接'); DialogUtils.showToast('正在连接');
} }
}); });
// 连接设备 // 连接设备
await device.connect().catchError((onError) { await selectedDevice.connect().catchError((onError) {
DialogUtils.dismissDialog();
DialogUtils.showToast('连接设备失败$onError'); DialogUtils.showToast('连接设备失败$onError');
}); });
} }
// 写入数据 // 写入数据
Future<void> writeData(List<int> data) async { Future<void> writeData(List<int> data) async {
if (selectedDevice.value == null) { // if (selectedDevice == null) {
throw Exception('No device connected'); // throw Exception('No device connected');
} // }
final characteristic = QualifiedCharacteristic(
deviceId: selectedDevice.value!.id,
serviceId: _serviceId,
characteristicId: _writeCharacteristicId,
);
await ble.writeCharacteristicWithResponse(characteristic, value: data); await _writeCharacteristic.write(data, allowLongWrite: true);
} }
// 检查权限 // 检查权限
......
import 'package:anchor_collect_flutter/congifs.dart'; import 'package:anchor_collect_flutter/congifs.dart';
import 'package:anchor_collect_flutter/pages/login/login_view.dart'; import 'package:anchor_collect_flutter/pages/login/login_view.dart';
import 'package:anchor_collect_flutter/rfid/rfid_plugin.dart';
import 'package:anchor_collect_flutter/routes/pages.dart'; import 'package:anchor_collect_flutter/routes/pages.dart';
import 'package:anchor_collect_flutter/routes/routes.dart'; import 'package:anchor_collect_flutter/routes/routes.dart';
import 'package:anchor_collect_flutter/utils/sp_helper.dart'; import 'package:anchor_collect_flutter/utils/sp_helper.dart';
...@@ -44,6 +45,8 @@ Future<void> initServices() async { ...@@ -44,6 +45,8 @@ Future<void> initServices() async {
await Get.putAsync(() => GlobalService().init()); await Get.putAsync(() => GlobalService().init());
await Get.putAsync(() => BleService().init()); await Get.putAsync(() => BleService().init());
RfidPlugin();
print('All services started...'); print('All services started...');
} }
......
import 'package:isar/isar.dart';
import '../generated/json/base/json_field.dart';
@JsonSerializable()
@collection
class Performance{
Id id = Isar.autoIncrement;
/** unid主键 */
@Index(unique: true, replace: true)
String? unid;
/**
* 基本信息ID
*/
String? cattleresumeId;
/**
* 年龄
*/
String? age;
/**
* 测量日期
*/
String? measureDate;
/**
* 体高(cm)
*/
double? height;
/**
* 体长(cm)
*/
double? length;
/**
* 胸围(cm)
*/
double? bust;
/**
* 体重
*/
double? weight;
/**
* 胸宽(cm)
*/
double? chestWidth;
/**
* 胸深(cm)
*/
double? chestDeep;
/**
* 腰角宽(cm)
*/
double? waistWidth;
/**
* 十字部宽(cm)
*/
double? crossWidth;
/**
* 管维(cm)
*/
double? guanWei;
/**
* 眼肌面积
*/
double? eyeMuscleArea;
/**
* 等级
*/
String? grade;
/**
* 羊毛长度(cm)
*/
double? hairLength;
/**
* 羊毛细度(微米)
*/
double? hairFineness;
/**
* 剪毛量(kg)
*/
double? shearing;
/**
* 背腰宽
*/
double? backWidth;
/**
* 繁殖成绩
*/
String? breedRes;
/**
* 健康状况
*/
String? health;
/**
* 免疫情况
*/
String? immune;
/** 是否上传 0未上传,1已上传 */
String? uploadStatus;
/**
* 部门id
*/
String? deptId;
/** 搜索值 */
String? searchValue;
/** 创建者 */
String? createBy;
/** 创建时间 */
String? createTime;
/** 更新者 */
String? updateBy;
/** 更新时间 */
String? updateTime;
/** 备注 */
String? remark;
}
\ No newline at end of file
...@@ -7,6 +7,7 @@ import 'package:anchor_collect_flutter/pages/sync/sync_view.dart'; ...@@ -7,6 +7,7 @@ import 'package:anchor_collect_flutter/pages/sync/sync_view.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import '../../congifs.dart';
import '../message/message_logic.dart'; import '../message/message_logic.dart';
import 'MyBottomNavigationBarItem.dart'; import 'MyBottomNavigationBarItem.dart';
import 'main_logic.dart'; import 'main_logic.dart';
...@@ -32,13 +33,12 @@ class MainPage extends StatelessWidget { ...@@ -32,13 +33,12 @@ class MainPage extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
body: PageView( body: PageView(
controller: _pageController, controller: _pageController,
onPageChanged: (value) { onPageChanged: (value) {
state.currentIndex.value = value; state.currentIndex.value = value;
if(value==1){ if (value == 1) {
logic.setMessageStatus(false); logic.setMessageStatus(false);
} }
}, },
...@@ -53,22 +53,24 @@ class MainPage extends StatelessWidget { ...@@ -53,22 +53,24 @@ class MainPage extends StatelessWidget {
// 选中颜色 // 选中颜色
selectedItemColor: Colors.green, selectedItemColor: Colors.green,
items: [ items: [
MyBottomNavigationBarItem('首页', state.currentIndex.value, 0, MyBottomNavigationBarItem(
'images/home_selected.png', 'images/home.png', false), '首页', state.currentIndex.value, 0, 'images/home_selected.png', 'images/home.png', false),
MyBottomNavigationBarItem('消息', state.currentIndex.value, 1, MyBottomNavigationBarItem('消息', state.currentIndex.value, 1, 'images/message_selected.png',
'images/message_selected.png', 'images/message.png', state.isNewMessage.value), 'images/message.png', state.isNewMessage.value),
MyBottomNavigationBarItem('同步', state.currentIndex.value, 2, MyBottomNavigationBarItem(
'images/sync_selected.png', 'images/sync.png', false), '同步', state.currentIndex.value, 2, 'images/sync_selected.png', 'images/sync.png', false),
MyBottomNavigationBarItem('我的', state.currentIndex.value, 3, MyBottomNavigationBarItem(
'images/mine_selected.png', 'images/mine.png', false), '我的', state.currentIndex.value, 3, 'images/mine_selected.png', 'images/mine.png', false),
], ],
onTap: (int position) { onTap: (int position) {
switch(position){ switch (position) {
case 0: case 0:
homeLogic.getBreedingCount(); homeLogic.getBreedingCount();
break; break;
case 1: case 1:
if (Config.isOnLine) {
messageLogic.getMessageCount(); messageLogic.getMessageCount();
}
break; break;
case 2: case 2:
syncLogic.queryCount(); syncLogic.queryCount();
......
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:package_info/package_info.dart';
import 'package:r_upgrade/r_upgrade.dart';
import '../../api/HttpUtils.dart';
import '../../api/apis.dart';
import '../../utils/dialog_utils.dart';
import 'mine_state.dart'; import 'mine_state.dart';
class MineLogic extends GetxController { class MineLogic extends GetxController {
final MineState state = MineState(); final MineState state = MineState();
checkUpdate() async {
///拉取版本信息
PackageInfo packageInfo = await PackageInfo.fromPlatform();
String buildNumber = packageInfo.buildNumber;
///检查更新
HttpUtils<int>()
..get()
..setUrl(APIS.get_version)
..onResponse((response) {
if (buildNumber != response.toString()) {
// DialogUtils.showToast('最新版本$response');
DialogUtils.showLoadingDialog('正在下载新版本');
upgrade();
} else {
DialogUtils.showToast('已经是最新版本');
}
// var _appName = packageInfo.appName;
// var _packageName = packageInfo.packageName;
})
..onError((msg, code) {
// DialogUtils.dismissDialog();
})
..build();
}
///下载app
void upgrade() async {
int appId = await RUpgrade.upgrade(
'${APIS.baseUrl}/api/download?type=Flutter',
fileName: 'anchor_collect_flutter.apk',
installType: RUpgradeInstallType.normal,
) ??
0;
bool isSuccess = await RUpgrade.install(appId) ?? false;
}
} }
import 'package:anchor_collect_flutter/routes/routes.dart';
import 'package:anchor_collect_flutter/utils/dialog_utils.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import '../../congifs.dart';
import '../../utils/sp_helper.dart';
import 'mine_logic.dart'; import 'mine_logic.dart';
class MinePage extends StatelessWidget { class MinePage extends StatelessWidget {
...@@ -25,13 +29,13 @@ class MinePage extends StatelessWidget { ...@@ -25,13 +29,13 @@ class MinePage extends StatelessWidget {
children: [ children: [
Container( Container(
width: double.infinity, width: double.infinity,
height: 300, height: 200,
color: Colors.blue, color: Colors.blue,
), ),
Expanded( Expanded(
child: Container( child: Container(
width: double.infinity, width: double.infinity,
color: Colors.grey, color: const Color(0xf5f6f8),
), ),
), ),
], ],
...@@ -39,54 +43,116 @@ class MinePage extends StatelessWidget { ...@@ -39,54 +43,116 @@ class MinePage extends StatelessWidget {
///上层 ///上层
Column( Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max, mainAxisSize: MainAxisSize.max,
children: [ children: [
Row( Padding(
padding: const EdgeInsets.fromLTRB(20.0, 40.0, 20.0, 20.0),
child: Row(
children: [ children: [
CircleAvatar( CircleAvatar(
radius: 50, // 头像的半径大小 radius: 35, // 头像的半径大小
backgroundImage: AssetImage('assets/images/ic_launcher.png'), // 头像的图片路径 backgroundImage: AssetImage('images/ic_launcher.png'), // 头像的图片路径
child: Container(
// 边框容器
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.grey, // 边框的颜色
width: 2, // 边框的宽度
),
),
),
),
SizedBox(
width: 10,
),
Text(
'用户名',
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
), ),
Text('用户名'),
Expanded(child: SizedBox()), Expanded(child: SizedBox()),
Text('个人信息>'), Text(
'个人信息>',
style: TextStyle(
fontSize: 16,
color: Colors.white,
),
),
], ],
), ),
Container( ),
Padding(
padding: const EdgeInsets.all(20.0),
child: Container(
decoration: BoxDecoration( decoration: BoxDecoration(
color: Colors.white, color: Colors.white,
borderRadius: BorderRadius.circular(8), borderRadius: BorderRadius.circular(8),
), ),
child: const Column( child: Column(
children: [ children: [
ListTile( ListTile(
title: Text('编辑资料'), title: const Text('编辑资料'),
leading: Icon(Icons.account_box), leading: const Icon(Icons.account_box),
trailing: Icon(Icons.add), trailing: const Icon(Icons.add),
onTap: () => {},
),
SizedBox(
height: 1,
child: Container(
height: 1,
color: Colors.black12,
),
), ),
ListTile( ListTile(
title: Text('关于我们'), title: const Text('关于我们'),
leading: Icon(Icons.accessibility), leading: const Icon(Icons.accessibility),
trailing: Icon(Icons.add), trailing: const Icon(Icons.add),
onTap: () => {},
),
SizedBox(
height: 1,
child: Container(
height: 1,
color: Colors.black12,
),
), ),
ListTile( ListTile(
title: Text('修改密码'), title: const Text('修改密码'),
leading: Icon(Icons.account_balance_wallet_outlined), leading: const Icon(Icons.account_balance_wallet_outlined),
trailing: Icon(Icons.add), trailing: const Icon(Icons.add),
onTap: () => {},
),
SizedBox(
height: 1,
child: Container(
height: 1,
color: Colors.black12,
),
), ),
ListTile( ListTile(
title: Text('检查更新'), title: const Text('检查更新'),
leading: Icon(Icons.ad_units_outlined), leading: const Icon(Icons.ad_units_outlined),
trailing: Icon(Icons.add), trailing: const Icon(Icons.add),
onTap: () => {
if(Config.isOnLine){
logic.checkUpdate(),
}
},
), ),
], ],
), ),
), ),
),
const SizedBox( const SizedBox(
height: 100, height: 30,
), ),
Row(children: [ Row(children: [
const SizedBox(
width: 10,
),
Expanded( Expanded(
child: TextButton( child: TextButton(
style: ButtonStyle( style: ButtonStyle(
...@@ -94,10 +160,20 @@ class MinePage extends StatelessWidget { ...@@ -94,10 +160,20 @@ class MinePage extends StatelessWidget {
foregroundColor: MaterialStateProperty.all(Colors.white), foregroundColor: MaterialStateProperty.all(Colors.white),
padding: MaterialStateProperty.all(const EdgeInsets.all(8)), padding: MaterialStateProperty.all(const EdgeInsets.all(8)),
), ),
onPressed: () => (), onPressed: () => {
SpHelper.putStorage(Config.SP_STR_USERNAME, ''),
SpHelper.putStorage(Config.SP_STR_PASSWORD, ''),
SpHelper.putStorage(Config.SP_STR_TOKEN, ''),
SpHelper.putStorage(Config.SP_DEPT_ID, ''),
Config.isOnLine = false,
Get.offNamed(AppRoute.LOGIN),
},
child: const Text("退出登录"), child: const Text("退出登录"),
), ),
), ),
const SizedBox(
width: 10,
),
]), ]),
], ],
), ),
......
...@@ -20,29 +20,18 @@ class SettingLogic extends GetxController { ...@@ -20,29 +20,18 @@ class SettingLogic extends GetxController {
if (BleService.to.isScanning.value) { if (BleService.to.isScanning.value) {
DialogUtils.showToast('停止扫描'); DialogUtils.showToast('停止扫描');
} else { } else {
state.deviceList.clear(); BleService.to.scanResults.clear();
DialogUtils.showToast('开始扫描'); DialogUtils.showToast('开始扫描');
} }
BleService.to.toggleScan((device) => { BleService.to.toggleScan();
// 如果列表中没有该设备,则添加到列表中
if (!state.deviceList.any((element) => element.id == device.id))
{
state.deviceList.add(device),
state.deviceList.refresh(), // 手动触发 Obx 的更新
}
});
} }
connectBle(BuildContext context, int index) { connectBle(BuildContext context, int index) {
if (BleService.to.deviceStatus == 'Connected') { if (BleService.to.isConnect) {
if (state.deviceList[index].id != BleService.to.selectedDevice.value!.id) { BleService.to.stopScan();
BleService.to.streamSubscriptionConnection.cancel();
} else {
DialogUtils.showToast('请勿重复链接蓝牙设备');
return;
}
} }
BleService.to.connectToDevice(state.deviceList[index], context, (args) { BleService.to.connectToDevice(index, (args) {
BleService.to.isConnect = true;
DialogUtils.showToast('蓝牙设备连接成功'); DialogUtils.showToast('蓝牙设备连接成功');
// DialogUtils.dismissDialog(); // DialogUtils.dismissDialog();
Get.back(); Get.back();
......
...@@ -4,7 +4,6 @@ import 'package:get/get.dart'; ...@@ -4,7 +4,6 @@ import 'package:get/get.dart';
class SettingState { class SettingState {
final deviceList = <DiscoveredDevice>[].obs;
bool isBleOn = false; bool isBleOn = false;
......
import 'dart:ffi';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
...@@ -27,11 +29,11 @@ class SettingPage extends StatelessWidget { ...@@ -27,11 +29,11 @@ class SettingPage extends StatelessWidget {
Obx(() { Obx(() {
return Expanded( return Expanded(
child: ListView.separated( child: ListView.separated(
itemCount: state.deviceList.length, itemCount: BleService.to.scanResults.length,
itemBuilder: (BuildContext context, int index) { itemBuilder: (BuildContext context, int index) {
return ListTile( return ListTile(
title: Text(state.deviceList[index].name), title: Text(BleService.to.scanResults[index].device.advName),
subtitle: Text(state.deviceList[index].id), subtitle: Text(BleService.to.scanResults[index].device.platformName),
onTap: ()=>{ onTap: ()=>{
logic.connectBle(context,index), logic.connectBle(context,index),
}, },
...@@ -80,7 +82,7 @@ class SettingPage extends StatelessWidget { ...@@ -80,7 +82,7 @@ class SettingPage extends StatelessWidget {
// ), // ),
TextButton( TextButton(
onPressed: () => { onPressed: () => {
state.deviceList.clear(), BleService.to.scanResults.clear(),
}, },
child: const Text( child: const Text(
'清空', '清空',
......
...@@ -17,7 +17,7 @@ class RfidPlugin { ...@@ -17,7 +17,7 @@ class RfidPlugin {
late StreamSubscription<dynamic> _streamSubscription; late StreamSubscription<dynamic> _streamSubscription;
String rfidTagTemp = ''; static String rfidTagTemp = '';
RfidPlugin() { RfidPlugin() {
_streamSubscription = _eventChannel.receiveBroadcastStream().listen((event) async { _streamSubscription = _eventChannel.receiveBroadcastStream().listen((event) async {
...@@ -33,27 +33,27 @@ class RfidPlugin { ...@@ -33,27 +33,27 @@ class RfidPlugin {
}, onError: errorEventListen, onDone: doneEventListen); }, onError: errorEventListen, onDone: doneEventListen);
} }
Future<void> initRfid() async { static Future<void> initRfid() async {
await _methodChannel.invokeMethod('init_rfid'); await _methodChannel.invokeMethod('init_rfid');
} }
Future<String> getVersion() async { static Future<String> getVersion() async {
String version = await _methodChannel.invokeMethod('get_version'); String version = await _methodChannel.invokeMethod('get_version');
return version; return version;
} }
Future<void> startInventory(EventCallback callback) async { static Future<void> startInventory(EventCallback callback) async {
_callback = callback; _callback = callback;
rfidTagTemp = ''; rfidTagTemp = '';
await _methodChannel.invokeMethod('start_inventory'); await _methodChannel.invokeMethod('start_inventory');
} }
Future<void> stopInventory() async { static Future<void> stopInventory() async {
await _methodChannel.invokeMethod('stop_inventory'); await _methodChannel.invokeMethod('stop_inventory');
} }
Future<void> pushRemoteRFIDData() async { static Future<void> pushRemoteRFIDData(List<int> readData) async {
List<int> readData = await BleService.to.readData(); // List<int> readData = await BleService.to.readData();
Map params = { Map params = {
"value":readData, "value":readData,
}; };
......
...@@ -6,6 +6,7 @@ import FlutterMacOS ...@@ -6,6 +6,7 @@ import FlutterMacOS
import Foundation import Foundation
import device_info_plus import device_info_plus
import flutter_blue_plus
import flutter_image_compress_macos import flutter_image_compress_macos
import isar_flutter_libs import isar_flutter_libs
import package_info import package_info
...@@ -15,6 +16,7 @@ import sqflite ...@@ -15,6 +16,7 @@ import sqflite
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin")) DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
FlutterBluePlusPlugin.register(with: registry.registrar(forPlugin: "FlutterBluePlusPlugin"))
FlutterImageCompressMacosPlugin.register(with: registry.registrar(forPlugin: "FlutterImageCompressMacosPlugin")) FlutterImageCompressMacosPlugin.register(with: registry.registrar(forPlugin: "FlutterImageCompressMacosPlugin"))
IsarFlutterLibsPlugin.register(with: registry.registrar(forPlugin: "IsarFlutterLibsPlugin")) IsarFlutterLibsPlugin.register(with: registry.registrar(forPlugin: "IsarFlutterLibsPlugin"))
FLTPackageInfoPlugin.register(with: registry.registrar(forPlugin: "FLTPackageInfoPlugin")) FLTPackageInfoPlugin.register(with: registry.registrar(forPlugin: "FLTPackageInfoPlugin"))
......
...@@ -342,14 +342,14 @@ packages: ...@@ -342,14 +342,14 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
flutter_blue: flutter_blue_plus:
dependency: "direct main" dependency: "direct main"
description: description:
name: flutter_blue name: flutter_blue_plus
sha256: f7f76b9b80455b0375693ec96c276fadb01e94d8441fa1740a64980cd1aeda5c sha256: c4b25ca6bf232bda4919e656179221aabeceb8271476b641ac59ae8aad085329
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "0.8.0" version: "1.31.2"
flutter_cache_manager: flutter_cache_manager:
dependency: transitive dependency: transitive
description: description:
......
...@@ -57,7 +57,9 @@ dependencies: ...@@ -57,7 +57,9 @@ dependencies:
package_info: ^2.0.2 #获取包名 package_info: ^2.0.2 #获取包名
device_info_plus: ^8.2.2 #获取设备信息(版本号等) device_info_plus: ^8.2.2 #获取设备信息(版本号等)
r_upgrade: ^0.4.2 #版本更新 r_upgrade: ^0.4.2 #版本更新
flutter_blue: ^0.8.0 # flutter_blue: ^0.8.0
flutter_blue_plus: 1.31.2
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment