feat: 5.0.0 (#35)
* draft: 临时提交 * feat: 实现扫描功能 * fix: 优化广播逻辑 * feat: 添加协程方法 * fix: 修改宏定义 * draft: 临时提交 * feat: 调整接口 * fix: 修改版本号 * feat: 4.1.1 * draft: 临时提交 * feat: 5.0.0-dev.2 * fix: 修复版本号错误 * draft: 临时提交 * fix: 修复连接断开异常 * fix: 修复问题 * fix: 优化代码 * fix: 优化 short UUID 格式化逻辑 * fix: 尝试实现 read_rssi 接口,当前此接口不可用,会报异常 * feat: 删除 getMaximumWriteLength 方法 * fix: 更新 CHANGELOG.md * feat: 5.0.0-dev.1 * fix: 更新依赖项 * feat: linux-5.0.0-dev.1 * fix: 更新 CHANGELOG.md * fix: 开始搜索设备时清空设备列表 * fix: 开始扫描时清空设备列表 * feat: 5.0.0-dev.2 * fix: 优化 MyGattService 和 MyGattCharacteristic * feat: 更新 interface 版本 -> 5.0.0-dev.4 * feat: 更新 interface 版本 -> 5.0.0-dev.4 * feat: 实现 flutter 部分 5.0.0 * fix: 移除 maximumWriteLength * fix: 移除 rssi * feat: 5.0.0-dev.1 * feat: 5.0.0-dev.2 * fix: 更新依赖项 * fix: 5.0.0-dev.4 * fix: 更新依赖项 * draft: 临时提交 * feat: 5.0.0-dev.5 * draft: 删除 MyCentralManager 和 MyPeripheralManager * fix: 更新依赖项 * fix: 更新依赖项 * feat: 适配新接口 * feat: 5.0.0-dev.6 * draft: 临时提交 * feat: 5.0.0-dev.7 * fix: 修改版本号 * feat: 5.0.0-dev.8 * feat: 5.0.0-dev.9 * fix: 修复 trimGATT 错误 * feat: 5.0.0-dev.6 * feat: 5.0.0-dev.3 * feat: 5.0.0-dev.4 * fix: 更新 pubspec.lock * feat: 5.0.0-dev.7 * feat: 5.0.0-dev.3 * fix: balabala * fix: balabala * draft: 5.0.0-dev.1 * fix: trim GATT when call the `writeCharacteristic` method. * fix: make difference of `trim` and `fragment`. * feat: 5.0.0-dev.1 * feat: 5.0.0-dev.1 * feat: 优化示例程序 * fix: 更新 README.md * fix: 修复插件引用 * draft: XXXX * feat: 增加调试信息 * fix: 更新 pubspec.lock * feat: 5.0.0-dev.4 * feat: 5.0.0-dev.3 * feat: 5.0.0 * feat: 5.0.0 * feat: 5.0.0 * feat: 5.0.0 * feat: 5.0.0 * feat: 5.0.0
This commit is contained in:
@ -8,18 +8,17 @@ import 'package:convert/convert.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
CentralManager get centralManager => CentralManager.instance;
|
||||
PeripheralManager get peripheralManager => PeripheralManager.instance;
|
||||
|
||||
void main() {
|
||||
runZonedGuarded(onStartUp, onCrashed);
|
||||
}
|
||||
|
||||
void onStartUp() async {
|
||||
Logger.root.onRecord.listen(onLogRecord);
|
||||
// hierarchicalLoggingEnabled = true;
|
||||
// CentralManager.instance.logLevel = Level.WARNING;
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await centralManager.setUp();
|
||||
await peripheralManager.setUp();
|
||||
await CentralManager.instance.setUp();
|
||||
await PeripheralManager.instance.setUp();
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
@ -58,6 +57,8 @@ class _MyAppState extends State<MyApp> {
|
||||
return MaterialApp(
|
||||
theme: ThemeData.light(
|
||||
useMaterial3: true,
|
||||
).copyWith(
|
||||
materialTapTargetSize: MaterialTapTargetSize.padded,
|
||||
),
|
||||
home: const HomeView(),
|
||||
routes: {
|
||||
@ -168,15 +169,15 @@ class _ScannerViewState extends State<ScannerView> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
state = ValueNotifier(centralManager.state);
|
||||
state = ValueNotifier(BluetoothLowEnergyState.unknown);
|
||||
discovering = ValueNotifier(false);
|
||||
discoveredEventArgs = ValueNotifier([]);
|
||||
stateChangedSubscription = centralManager.stateChanged.listen(
|
||||
stateChangedSubscription = CentralManager.instance.stateChanged.listen(
|
||||
(eventArgs) {
|
||||
state.value = eventArgs.state;
|
||||
},
|
||||
);
|
||||
discoveredSubscription = centralManager.discovered.listen(
|
||||
discoveredSubscription = CentralManager.instance.discovered.listen(
|
||||
(eventArgs) {
|
||||
final items = discoveredEventArgs.value;
|
||||
final i = items.indexWhere(
|
||||
@ -190,6 +191,11 @@ class _ScannerViewState extends State<ScannerView> {
|
||||
}
|
||||
},
|
||||
);
|
||||
_initialize();
|
||||
}
|
||||
|
||||
void _initialize() async {
|
||||
state.value = await CentralManager.instance.getState();
|
||||
}
|
||||
|
||||
@override
|
||||
@ -233,12 +239,13 @@ class _ScannerViewState extends State<ScannerView> {
|
||||
}
|
||||
|
||||
Future<void> startDiscovery() async {
|
||||
await centralManager.startDiscovery();
|
||||
discoveredEventArgs.value = [];
|
||||
await CentralManager.instance.startDiscovery();
|
||||
discovering.value = true;
|
||||
}
|
||||
|
||||
Future<void> stopDiscovery() async {
|
||||
await centralManager.stopDiscovery();
|
||||
await CentralManager.instance.stopDiscovery();
|
||||
discovering.value = false;
|
||||
}
|
||||
|
||||
@ -340,7 +347,13 @@ class _ScannerViewState extends State<ScannerView> {
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
trailing: RssiWidget(rssi),
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
RssiWidget(rssi),
|
||||
Text('$rssi'),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
separatorBuilder: (context, i) {
|
||||
@ -378,44 +391,39 @@ class PeripheralView extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _PeripheralViewState extends State<PeripheralView> {
|
||||
late final ValueNotifier<bool> state;
|
||||
late final ValueNotifier<bool> connectionState;
|
||||
late final DiscoveredEventArgs eventArgs;
|
||||
late final ValueNotifier<List<GattService>> services;
|
||||
late final ValueNotifier<List<GattCharacteristic>> characteristics;
|
||||
late final ValueNotifier<GattService?> service;
|
||||
late final ValueNotifier<GattCharacteristic?> characteristic;
|
||||
late final ValueNotifier<GattCharacteristicWriteType> writeType;
|
||||
late final ValueNotifier<int> maximumWriteLength;
|
||||
late final ValueNotifier<int> rssi;
|
||||
late final ValueNotifier<List<Log>> logs;
|
||||
late final TextEditingController writeController;
|
||||
late final StreamSubscription stateChangedSubscription;
|
||||
late final StreamSubscription valueChangedSubscription;
|
||||
late final StreamSubscription rssiChangedSubscription;
|
||||
late final Timer rssiTimer;
|
||||
late final StreamSubscription connectionStateChangedSubscription;
|
||||
late final StreamSubscription characteristicNotifiedSubscription;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
eventArgs = widget.eventArgs;
|
||||
state = ValueNotifier(false);
|
||||
connectionState = ValueNotifier(false);
|
||||
services = ValueNotifier([]);
|
||||
characteristics = ValueNotifier([]);
|
||||
service = ValueNotifier(null);
|
||||
characteristic = ValueNotifier(null);
|
||||
writeType = ValueNotifier(GattCharacteristicWriteType.withResponse);
|
||||
maximumWriteLength = ValueNotifier(0);
|
||||
rssi = ValueNotifier(-100);
|
||||
logs = ValueNotifier([]);
|
||||
writeController = TextEditingController();
|
||||
stateChangedSubscription = centralManager.peripheralStateChanged.listen(
|
||||
connectionStateChangedSubscription =
|
||||
CentralManager.instance.connectionStateChanged.listen(
|
||||
(eventArgs) {
|
||||
if (eventArgs.peripheral != this.eventArgs.peripheral) {
|
||||
return;
|
||||
}
|
||||
final state = eventArgs.state;
|
||||
this.state.value = state;
|
||||
if (!state) {
|
||||
final connectionState = eventArgs.connectionState;
|
||||
this.connectionState.value = connectionState;
|
||||
if (!connectionState) {
|
||||
services.value = [];
|
||||
characteristics.value = [];
|
||||
service.value = null;
|
||||
@ -424,12 +432,13 @@ class _PeripheralViewState extends State<PeripheralView> {
|
||||
}
|
||||
},
|
||||
);
|
||||
valueChangedSubscription = centralManager.characteristicValueChanged.listen(
|
||||
characteristicNotifiedSubscription =
|
||||
CentralManager.instance.characteristicNotified.listen(
|
||||
(eventArgs) {
|
||||
final characteristic = this.characteristic.value;
|
||||
if (eventArgs.characteristic != characteristic) {
|
||||
return;
|
||||
}
|
||||
// final characteristic = this.characteristic.value;
|
||||
// if (eventArgs.characteristic != characteristic) {
|
||||
// return;
|
||||
// }
|
||||
const type = LogType.notify;
|
||||
final log = Log(type, eventArgs.value);
|
||||
logs.value = [
|
||||
@ -438,28 +447,16 @@ class _PeripheralViewState extends State<PeripheralView> {
|
||||
];
|
||||
},
|
||||
);
|
||||
rssiTimer = Timer.periodic(
|
||||
const Duration(seconds: 5),
|
||||
(timer) async {
|
||||
final state = this.state.value;
|
||||
if (state) {
|
||||
rssi.value = await centralManager.readRSSI(eventArgs.peripheral);
|
||||
} else {
|
||||
rssi.value = -100;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return WillPopScope(
|
||||
onWillPop: () async {
|
||||
if (state.value) {
|
||||
return PopScope(
|
||||
onPopInvoked: (didPop) async {
|
||||
if (connectionState.value) {
|
||||
final peripheral = eventArgs.peripheral;
|
||||
await centralManager.disconnect(peripheral);
|
||||
await CentralManager.instance.disconnect(peripheral);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
child: Scaffold(
|
||||
appBar: buildAppBar(context),
|
||||
@ -474,25 +471,17 @@ class _PeripheralViewState extends State<PeripheralView> {
|
||||
title: Text(title),
|
||||
actions: [
|
||||
ValueListenableBuilder(
|
||||
valueListenable: state,
|
||||
valueListenable: connectionState,
|
||||
builder: (context, state, child) {
|
||||
return TextButton(
|
||||
onPressed: () async {
|
||||
final peripheral = eventArgs.peripheral;
|
||||
if (state) {
|
||||
await centralManager.disconnect(peripheral);
|
||||
maximumWriteLength.value = 0;
|
||||
rssi.value = 0;
|
||||
await CentralManager.instance.disconnect(peripheral);
|
||||
} else {
|
||||
await centralManager.connect(peripheral);
|
||||
await CentralManager.instance.connect(peripheral);
|
||||
services.value =
|
||||
await centralManager.discoverGATT(peripheral);
|
||||
maximumWriteLength.value =
|
||||
await centralManager.getMaximumWriteLength(
|
||||
peripheral,
|
||||
type: writeType.value,
|
||||
);
|
||||
rssi.value = await centralManager.readRSSI(peripheral);
|
||||
await CentralManager.instance.discoverGATT(peripheral);
|
||||
}
|
||||
},
|
||||
child: Text(state ? 'DISCONNECT' : 'CONNECT'),
|
||||
@ -566,7 +555,32 @@ class _PeripheralViewState extends State<PeripheralView> {
|
||||
hint: const Text('CHOOSE A CHARACTERISTIC'),
|
||||
value: characteristic,
|
||||
onChanged: (characteristic) {
|
||||
if (characteristic == null) {
|
||||
return;
|
||||
}
|
||||
this.characteristic.value = characteristic;
|
||||
final writeType = this.writeType.value;
|
||||
final canWrite = characteristic.properties.contains(
|
||||
GattCharacteristicProperty.write,
|
||||
);
|
||||
final canWriteWithoutResponse =
|
||||
characteristic.properties.contains(
|
||||
GattCharacteristicProperty.writeWithoutResponse,
|
||||
);
|
||||
if (writeType ==
|
||||
GattCharacteristicWriteType.withResponse &&
|
||||
!canWrite &&
|
||||
canWriteWithoutResponse) {
|
||||
this.writeType.value =
|
||||
GattCharacteristicWriteType.withoutResponse;
|
||||
}
|
||||
if (writeType ==
|
||||
GattCharacteristicWriteType.withoutResponse &&
|
||||
!canWriteWithoutResponse &&
|
||||
canWrite) {
|
||||
this.writeType.value =
|
||||
GattCharacteristicWriteType.withResponse;
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
@ -624,129 +638,45 @@ class _PeripheralViewState extends State<PeripheralView> {
|
||||
},
|
||||
),
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
ValueListenableBuilder(
|
||||
valueListenable: writeType,
|
||||
builder: (context, writeType, child) {
|
||||
return ToggleButtons(
|
||||
onPressed: (i) async {
|
||||
final type = GattCharacteristicWriteType.values[i];
|
||||
this.writeType.value = type;
|
||||
maximumWriteLength.value =
|
||||
await centralManager.getMaximumWriteLength(
|
||||
eventArgs.peripheral,
|
||||
type: type,
|
||||
);
|
||||
},
|
||||
constraints: const BoxConstraints(
|
||||
minWidth: 0.0,
|
||||
minHeight: 0.0,
|
||||
),
|
||||
borderRadius: BorderRadius.circular(4.0),
|
||||
isSelected: GattCharacteristicWriteType.values
|
||||
.map((type) => type == writeType)
|
||||
.toList(),
|
||||
children: GattCharacteristicWriteType.values.map((type) {
|
||||
return Container(
|
||||
margin: const EdgeInsets.symmetric(
|
||||
horizontal: 8.0,
|
||||
vertical: 4.0,
|
||||
),
|
||||
child: Text(type.name),
|
||||
);
|
||||
}).toList(),
|
||||
);
|
||||
// final segments =
|
||||
// GattCharacteristicWriteType.values.map((type) {
|
||||
// return ButtonSegment(
|
||||
// value: type,
|
||||
// label: Text(type.name),
|
||||
// );
|
||||
// }).toList();
|
||||
// return SegmentedButton(
|
||||
// segments: segments,
|
||||
// selected: {writeType},
|
||||
// showSelectedIcon: false,
|
||||
// style: OutlinedButton.styleFrom(
|
||||
// tapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
||||
// padding: EdgeInsets.zero,
|
||||
// visualDensity: VisualDensity.compact,
|
||||
// shape: RoundedRectangleBorder(
|
||||
// borderRadius: BorderRadius.circular(8.0),
|
||||
// ),
|
||||
// ),
|
||||
// );
|
||||
},
|
||||
),
|
||||
const SizedBox(width: 8.0),
|
||||
ValueListenableBuilder(
|
||||
valueListenable: state,
|
||||
builder: (context, state, child) {
|
||||
return ValueListenableBuilder(
|
||||
valueListenable: maximumWriteLength,
|
||||
builder: (context, maximumWriteLength, child) {
|
||||
return Text('$maximumWriteLength');
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
const Spacer(),
|
||||
ValueListenableBuilder(
|
||||
valueListenable: rssi,
|
||||
builder: (context, rssi, child) {
|
||||
return RssiWidget(rssi);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
Container(
|
||||
margin: const EdgeInsets.only(bottom: 16.0),
|
||||
height: 160.0,
|
||||
child: ValueListenableBuilder(
|
||||
valueListenable: characteristic,
|
||||
builder: (context, characteristic, child) {
|
||||
final bool canNotify, canRead, canWrite;
|
||||
if (characteristic == null) {
|
||||
canNotify = canRead = canWrite = false;
|
||||
} else {
|
||||
final properties = characteristic.properties;
|
||||
canNotify = properties.contains(
|
||||
GattCharacteristicProperty.notify,
|
||||
);
|
||||
canRead = properties.contains(
|
||||
GattCharacteristicProperty.read,
|
||||
);
|
||||
canWrite = properties.contains(
|
||||
GattCharacteristicProperty.write,
|
||||
);
|
||||
}
|
||||
return Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: writeController,
|
||||
enabled: canWrite,
|
||||
expands: true,
|
||||
maxLines: null,
|
||||
textAlignVertical: TextAlignVertical.top,
|
||||
decoration: const InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
contentPadding: EdgeInsets.symmetric(
|
||||
horizontal: 12.0,
|
||||
vertical: 8.0,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
ValueListenableBuilder(
|
||||
valueListenable: characteristic,
|
||||
builder: (context, characteristic, chld) {
|
||||
final bool canNotify, canRead, canWrite, canWriteWithoutResponse;
|
||||
if (characteristic == null) {
|
||||
canNotify =
|
||||
canRead = canWrite = canWriteWithoutResponse = false;
|
||||
} else {
|
||||
final properties = characteristic.properties;
|
||||
canNotify = properties.contains(
|
||||
GattCharacteristicProperty.notify,
|
||||
) ||
|
||||
properties.contains(
|
||||
GattCharacteristicProperty.indicate,
|
||||
);
|
||||
canRead = properties.contains(
|
||||
GattCharacteristicProperty.read,
|
||||
);
|
||||
canWrite = properties.contains(
|
||||
GattCharacteristicProperty.write,
|
||||
);
|
||||
canWriteWithoutResponse = properties.contains(
|
||||
GattCharacteristicProperty.writeWithoutResponse,
|
||||
);
|
||||
}
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Container(
|
||||
margin: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton(
|
||||
ElevatedButton(
|
||||
onPressed: characteristic != null && canNotify
|
||||
? () async {
|
||||
await centralManager.notifyCharacteristic(
|
||||
await CentralManager.instance
|
||||
.setCharacteristicNotifyState(
|
||||
characteristic,
|
||||
state: true,
|
||||
);
|
||||
@ -754,10 +684,11 @@ class _PeripheralViewState extends State<PeripheralView> {
|
||||
: null,
|
||||
child: const Text('NOTIFY'),
|
||||
),
|
||||
TextButton(
|
||||
const SizedBox(width: 8.0),
|
||||
ElevatedButton(
|
||||
onPressed: characteristic != null && canRead
|
||||
? () async {
|
||||
final value = await centralManager
|
||||
final value = await CentralManager.instance
|
||||
.readCharacteristic(characteristic);
|
||||
const type = LogType.read;
|
||||
final log = Log(type, value);
|
||||
@ -765,31 +696,124 @@ class _PeripheralViewState extends State<PeripheralView> {
|
||||
}
|
||||
: null,
|
||||
child: const Text('READ'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: characteristic != null && canWrite
|
||||
? () async {
|
||||
final text = writeController.text;
|
||||
final elements = utf8.encode(text);
|
||||
final value = Uint8List.fromList(elements);
|
||||
final type = writeType.value;
|
||||
await centralManager.writeCharacteristic(
|
||||
characteristic,
|
||||
value: value,
|
||||
type: type,
|
||||
);
|
||||
final log = Log(LogType.write, value);
|
||||
logs.value = [...logs.value, log];
|
||||
}
|
||||
: null,
|
||||
child: const Text('WRITE'),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 160.0,
|
||||
child: TextField(
|
||||
controller: writeController,
|
||||
enabled: canWrite || canWriteWithoutResponse,
|
||||
expands: true,
|
||||
maxLines: null,
|
||||
textAlignVertical: TextAlignVertical.top,
|
||||
decoration: const InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
contentPadding: EdgeInsets.symmetric(
|
||||
horizontal: 12.0,
|
||||
vertical: 8.0,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
ValueListenableBuilder(
|
||||
valueListenable: writeType,
|
||||
builder: (context, writeType, child) {
|
||||
return ToggleButtons(
|
||||
onPressed: canWrite || canWriteWithoutResponse
|
||||
? (i) {
|
||||
if (!canWrite || !canWriteWithoutResponse) {
|
||||
return;
|
||||
}
|
||||
final type =
|
||||
GattCharacteristicWriteType.values[i];
|
||||
this.writeType.value = type;
|
||||
}
|
||||
: null,
|
||||
constraints: const BoxConstraints(
|
||||
minWidth: 0.0,
|
||||
minHeight: 0.0,
|
||||
),
|
||||
borderRadius: BorderRadius.circular(4.0),
|
||||
isSelected: GattCharacteristicWriteType.values
|
||||
.map((type) => type == writeType)
|
||||
.toList(),
|
||||
children: GattCharacteristicWriteType.values.map(
|
||||
(type) {
|
||||
return Container(
|
||||
margin: const EdgeInsets.symmetric(
|
||||
horizontal: 8.0,
|
||||
vertical: 4.0,
|
||||
),
|
||||
child: Text(type.name),
|
||||
);
|
||||
},
|
||||
).toList(),
|
||||
);
|
||||
// final segments =
|
||||
// GattCharacteristicWriteType.values.map((type) {
|
||||
// return ButtonSegment(
|
||||
// value: type,
|
||||
// label: Text(type.name),
|
||||
// );
|
||||
// }).toList();
|
||||
// return SegmentedButton(
|
||||
// segments: segments,
|
||||
// selected: {writeType},
|
||||
// showSelectedIcon: false,
|
||||
// style: OutlinedButton.styleFrom(
|
||||
// tapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
||||
// padding: EdgeInsets.zero,
|
||||
// visualDensity: VisualDensity.compact,
|
||||
// shape: RoundedRectangleBorder(
|
||||
// borderRadius: BorderRadius.circular(8.0),
|
||||
// ),
|
||||
// ),
|
||||
// );
|
||||
},
|
||||
),
|
||||
const Spacer(),
|
||||
ElevatedButton(
|
||||
onPressed: characteristic != null && canWrite
|
||||
? () async {
|
||||
final text = writeController.text;
|
||||
final elements = utf8.encode(text);
|
||||
final value = Uint8List.fromList(elements);
|
||||
final type = writeType.value;
|
||||
// Fragments the value by 512 bytes.
|
||||
const fragmentSize = 512;
|
||||
var start = 0;
|
||||
while (start < value.length) {
|
||||
final end = start + fragmentSize;
|
||||
final fragmentedValue = end < value.length
|
||||
? value.sublist(start, end)
|
||||
: value.sublist(start);
|
||||
await CentralManager.instance
|
||||
.writeCharacteristic(
|
||||
characteristic,
|
||||
value: fragmentedValue,
|
||||
type: type,
|
||||
);
|
||||
final log = Log(
|
||||
LogType.write,
|
||||
fragmentedValue,
|
||||
);
|
||||
logs.value = [...logs.value, log];
|
||||
start = end;
|
||||
}
|
||||
}
|
||||
: null,
|
||||
child: const Text('WRITE'),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16.0),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
@ -799,17 +823,14 @@ class _PeripheralViewState extends State<PeripheralView> {
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
rssiTimer.cancel();
|
||||
stateChangedSubscription.cancel();
|
||||
valueChangedSubscription.cancel();
|
||||
state.dispose();
|
||||
connectionStateChangedSubscription.cancel();
|
||||
characteristicNotifiedSubscription.cancel();
|
||||
connectionState.dispose();
|
||||
services.dispose();
|
||||
characteristics.dispose();
|
||||
service.dispose();
|
||||
characteristic.dispose();
|
||||
writeType.dispose();
|
||||
maximumWriteLength.dispose();
|
||||
rssi.dispose();
|
||||
logs.dispose();
|
||||
writeController.dispose();
|
||||
}
|
||||
@ -828,87 +849,63 @@ class _AdvertiserViewState extends State<AdvertiserView>
|
||||
late final ValueNotifier<bool> advertising;
|
||||
late final ValueNotifier<List<Log>> logs;
|
||||
late final StreamSubscription stateChangedSubscription;
|
||||
late final StreamSubscription readCharacteristicCommandReceivedSubscription;
|
||||
late final StreamSubscription writeCharacteristicCommandReceivedSubscription;
|
||||
late final StreamSubscription notifyCharacteristicCommandReceivedSubscription;
|
||||
late final StreamSubscription characteristicReadSubscription;
|
||||
late final StreamSubscription characteristicWrittenSubscription;
|
||||
late final StreamSubscription characteristicNotifyStateChangedSubscription;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
state = ValueNotifier(peripheralManager.state);
|
||||
state = ValueNotifier(BluetoothLowEnergyState.unknown);
|
||||
advertising = ValueNotifier(false);
|
||||
logs = ValueNotifier([]);
|
||||
stateChangedSubscription = peripheralManager.stateChanged.listen(
|
||||
stateChangedSubscription = PeripheralManager.instance.stateChanged.listen(
|
||||
(eventArgs) {
|
||||
state.value = eventArgs.state;
|
||||
},
|
||||
);
|
||||
readCharacteristicCommandReceivedSubscription =
|
||||
peripheralManager.readCharacteristicCommandReceived.listen(
|
||||
characteristicReadSubscription =
|
||||
PeripheralManager.instance.characteristicRead.listen(
|
||||
(eventArgs) async {
|
||||
final central = eventArgs.central;
|
||||
final characteristic = eventArgs.characteristic;
|
||||
final id = eventArgs.id;
|
||||
final offset = eventArgs.offset;
|
||||
final value = eventArgs.value;
|
||||
final log = Log(
|
||||
LogType.read,
|
||||
Uint8List.fromList([]),
|
||||
'central: ${central.uuid}; characteristic: ${characteristic.uuid}; id: $id; offset: $offset',
|
||||
value,
|
||||
'central: ${central.uuid}; characteristic: ${characteristic.uuid}',
|
||||
);
|
||||
logs.value = [
|
||||
...logs.value,
|
||||
log,
|
||||
];
|
||||
// final maximumWriteLength = peripheralManager.getMaximumWriteLength(
|
||||
// central,
|
||||
// );
|
||||
const status = true;
|
||||
final value = Uint8List.fromList([0x01, 0x02, 0x03]);
|
||||
await peripheralManager.sendReadCharacteristicReply(
|
||||
central,
|
||||
characteristic: characteristic,
|
||||
id: id,
|
||||
offset: offset,
|
||||
status: status,
|
||||
value: value,
|
||||
);
|
||||
},
|
||||
);
|
||||
writeCharacteristicCommandReceivedSubscription =
|
||||
peripheralManager.writeCharacteristicCommandReceived.listen(
|
||||
characteristicWrittenSubscription =
|
||||
PeripheralManager.instance.characteristicWritten.listen(
|
||||
(eventArgs) async {
|
||||
final central = eventArgs.central;
|
||||
final characteristic = eventArgs.characteristic;
|
||||
final id = eventArgs.id;
|
||||
final offset = eventArgs.offset;
|
||||
final value = eventArgs.value;
|
||||
final log = Log(
|
||||
LogType.write,
|
||||
value,
|
||||
'central: ${central.uuid}; characteristic: ${characteristic.uuid}; id: $id; offset: $offset',
|
||||
'central: ${central.uuid}; characteristic: ${characteristic.uuid}',
|
||||
);
|
||||
logs.value = [
|
||||
...logs.value,
|
||||
log,
|
||||
];
|
||||
const status = true;
|
||||
await peripheralManager.sendWriteCharacteristicReply(
|
||||
central,
|
||||
characteristic: characteristic,
|
||||
id: id,
|
||||
offset: offset,
|
||||
status: status,
|
||||
);
|
||||
},
|
||||
);
|
||||
notifyCharacteristicCommandReceivedSubscription =
|
||||
peripheralManager.notifyCharacteristicCommandReceived.listen(
|
||||
characteristicNotifyStateChangedSubscription =
|
||||
PeripheralManager.instance.characteristicNotifyStateChanged.listen(
|
||||
(eventArgs) async {
|
||||
final central = eventArgs.central;
|
||||
final characteristic = eventArgs.characteristic;
|
||||
final state = eventArgs.state;
|
||||
final log = Log(
|
||||
LogType.write,
|
||||
LogType.notify,
|
||||
Uint8List.fromList([]),
|
||||
'central: ${central.uuid}; characteristic: ${characteristic.uuid}; state: $state',
|
||||
);
|
||||
@ -918,15 +915,21 @@ class _AdvertiserViewState extends State<AdvertiserView>
|
||||
];
|
||||
// Write someting to the central when notify started.
|
||||
if (state) {
|
||||
final value = Uint8List.fromList([0x03, 0x02, 0x01]);
|
||||
await peripheralManager.notifyCharacteristicValueChanged(
|
||||
central,
|
||||
characteristic: characteristic,
|
||||
final elements = List.generate(2000, (i) => i % 256);
|
||||
final value = Uint8List.fromList(elements);
|
||||
await PeripheralManager.instance.writeCharacteristic(
|
||||
characteristic,
|
||||
value: value,
|
||||
central: central,
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
_initialize();
|
||||
}
|
||||
|
||||
void _initialize() async {
|
||||
state.value = await PeripheralManager.instance.getState();
|
||||
}
|
||||
|
||||
@override
|
||||
@ -970,7 +973,9 @@ class _AdvertiserViewState extends State<AdvertiserView>
|
||||
}
|
||||
|
||||
Future<void> startAdvertising() async {
|
||||
await peripheralManager.clearServices();
|
||||
await PeripheralManager.instance.clearServices();
|
||||
final elements = List.generate(1000, (i) => i % 256);
|
||||
final value = Uint8List.fromList(elements);
|
||||
final service = GattService(
|
||||
uuid: UUID.short(100),
|
||||
characteristics: [
|
||||
@ -979,15 +984,16 @@ class _AdvertiserViewState extends State<AdvertiserView>
|
||||
properties: [
|
||||
GattCharacteristicProperty.read,
|
||||
],
|
||||
value: value,
|
||||
descriptors: [],
|
||||
),
|
||||
GattCharacteristic(
|
||||
uuid: UUID.short(201),
|
||||
properties: [
|
||||
GattCharacteristicProperty.read,
|
||||
GattCharacteristicProperty.write,
|
||||
GattCharacteristicProperty.writeWithoutResponse,
|
||||
],
|
||||
value: Uint8List.fromList([]),
|
||||
descriptors: [],
|
||||
),
|
||||
GattCharacteristic(
|
||||
@ -996,24 +1002,41 @@ class _AdvertiserViewState extends State<AdvertiserView>
|
||||
GattCharacteristicProperty.notify,
|
||||
GattCharacteristicProperty.indicate,
|
||||
],
|
||||
value: Uint8List.fromList([]),
|
||||
descriptors: [],
|
||||
),
|
||||
GattCharacteristic(
|
||||
uuid: UUID.short(203),
|
||||
properties: [
|
||||
GattCharacteristicProperty.notify,
|
||||
],
|
||||
value: Uint8List.fromList([]),
|
||||
descriptors: [],
|
||||
),
|
||||
GattCharacteristic(
|
||||
uuid: UUID.short(204),
|
||||
properties: [
|
||||
GattCharacteristicProperty.indicate,
|
||||
],
|
||||
value: Uint8List.fromList([]),
|
||||
descriptors: [],
|
||||
),
|
||||
],
|
||||
);
|
||||
await peripheralManager.addService(service);
|
||||
await PeripheralManager.instance.addService(service);
|
||||
final advertisement = Advertisement(
|
||||
name: 'flutter',
|
||||
name: 'le12138',
|
||||
manufacturerSpecificData: ManufacturerSpecificData(
|
||||
id: 0x2e19,
|
||||
data: Uint8List.fromList([0x01, 0x02, 0x03]),
|
||||
),
|
||||
);
|
||||
await peripheralManager.startAdvertising(advertisement);
|
||||
await PeripheralManager.instance.startAdvertising(advertisement);
|
||||
advertising.value = true;
|
||||
}
|
||||
|
||||
Future<void> stopAdvertising() async {
|
||||
await peripheralManager.stopAdvertising();
|
||||
await PeripheralManager.instance.stopAdvertising();
|
||||
advertising.value = false;
|
||||
}
|
||||
|
||||
@ -1074,9 +1097,9 @@ class _AdvertiserViewState extends State<AdvertiserView>
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
stateChangedSubscription.cancel();
|
||||
readCharacteristicCommandReceivedSubscription.cancel();
|
||||
writeCharacteristicCommandReceivedSubscription.cancel();
|
||||
notifyCharacteristicCommandReceivedSubscription.cancel();
|
||||
characteristicReadSubscription.cancel();
|
||||
characteristicWrittenSubscription.cancel();
|
||||
characteristicNotifyStateChangedSubscription.cancel();
|
||||
state.dispose();
|
||||
advertising.dispose();
|
||||
logs.dispose();
|
||||
|
@ -15,15 +15,15 @@ packages:
|
||||
path: ".."
|
||||
relative: true
|
||||
source: path
|
||||
version: "4.0.0"
|
||||
version: "5.0.0"
|
||||
bluetooth_low_energy_platform_interface:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: bluetooth_low_energy_platform_interface
|
||||
sha256: a01819f4ef89d033edaa979465ec8c3af13b2618dc718d90fe681be91b6c4356
|
||||
sha256: "54f92ab2d7746fb6f2b4a40a48cd7eb8e3bf772f3ee89e1979d4d7b741fb2a05"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "4.0.0"
|
||||
version: "5.0.0"
|
||||
boolean_selector:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -52,10 +52,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: collection
|
||||
sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687
|
||||
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.17.2"
|
||||
version: "1.18.0"
|
||||
convert:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
@ -102,10 +102,10 @@ packages:
|
||||
dependency: "direct dev"
|
||||
description:
|
||||
name: flutter_lints
|
||||
sha256: ad76540d21c066228ee3f9d1dad64a9f7e46530e8bb7c85011a88bc1fd874bc5
|
||||
sha256: e2a421b7e59244faef694ba7b30562e489c2b489866e505074eb005cd7060db7
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.0.0"
|
||||
version: "3.0.1"
|
||||
flutter_test:
|
||||
dependency: "direct dev"
|
||||
description: flutter
|
||||
@ -125,10 +125,10 @@ packages:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: intl
|
||||
sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d"
|
||||
sha256: d6f56758b7d3014a48af9701c085700aac781a92a87a62b1333b46d8879661cf
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.18.1"
|
||||
version: "0.19.0"
|
||||
lints:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -173,10 +173,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: meta
|
||||
sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3"
|
||||
sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.9.1"
|
||||
version: "1.10.0"
|
||||
path:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -189,18 +189,18 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: platform
|
||||
sha256: "4a451831508d7d6ca779f7ac6e212b4023dd5a7d08a27a63da33756410e32b76"
|
||||
sha256: ae68c7bfcd7383af3629daafb32fb4e8681c7154428da4febcff06200585f102
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.1.0"
|
||||
version: "3.1.2"
|
||||
plugin_platform_interface:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: plugin_platform_interface
|
||||
sha256: da3fdfeccc4d4ff2da8f8c556704c08f912542c5fb3cf2233ed75372384a034d
|
||||
sha256: f4f88d4a900933e7267e2b353594774fc0d07fb072b47eedcd5b54e1ea3269f8
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.1.6"
|
||||
version: "2.1.7"
|
||||
process:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -226,18 +226,18 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: stack_trace
|
||||
sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5
|
||||
sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.11.0"
|
||||
version: "1.11.1"
|
||||
stream_channel:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: stream_channel
|
||||
sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8"
|
||||
sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.1.1"
|
||||
version: "2.1.2"
|
||||
string_scanner:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -266,10 +266,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: test_api
|
||||
sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8"
|
||||
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.6.0"
|
||||
version: "0.6.1"
|
||||
typed_data:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -290,18 +290,18 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: vm_service
|
||||
sha256: c620a6f783fa22436da68e42db7ebbf18b8c44b9a46ab911f666ff09ffd9153f
|
||||
sha256: c538be99af830f478718b51630ec1b6bee5e74e52c8a802d328d9e71d35d2583
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "11.7.1"
|
||||
version: "11.10.0"
|
||||
web:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: web
|
||||
sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10
|
||||
sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.1.4-beta"
|
||||
version: "0.3.0"
|
||||
webdriver:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -311,5 +311,5 @@ packages:
|
||||
source: hosted
|
||||
version: "3.0.2"
|
||||
sdks:
|
||||
dart: ">=3.1.0-185.0.dev <4.0.0"
|
||||
dart: ">=3.2.0-194.0.dev <4.0.0"
|
||||
flutter: ">=3.3.0"
|
||||
|
@ -17,7 +17,7 @@ dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
|
||||
bluetooth_low_energy_platform_interface: ^4.0.0
|
||||
bluetooth_low_energy_platform_interface: ^5.0.0
|
||||
bluetooth_low_energy_android:
|
||||
# When depending on this package from a real application you should use:
|
||||
# bluetooth_low_energy: ^x.y.z
|
||||
@ -30,7 +30,7 @@ dependencies:
|
||||
# Use with the CupertinoIcons class for iOS style icons.
|
||||
cupertino_icons: ^1.0.2
|
||||
convert: ^3.1.1
|
||||
intl: ^0.18.1
|
||||
intl: ^0.19.0
|
||||
|
||||
dev_dependencies:
|
||||
integration_test:
|
||||
|
Reference in New Issue
Block a user