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:
@ -1,3 +1,86 @@
|
||||
## 5.0.0
|
||||
|
||||
* Now `CentralManager#writeCharacteristic` and `PeripheralManager#writeCharacteristic` will fragment the value automatically, the maximum write length is 512 bytes.
|
||||
* Add `UUID#fromAddress` constructor.
|
||||
* Add `GattCharacteristicReadEventArgs` and `GattCharacteristicWrittenEventArgs`.
|
||||
* Add `PeripheralManager#characteristicRead` and `PeripheralManager#characteristicWritten`.
|
||||
* Add `PeripheralManager#readCharacteristic`.
|
||||
* Remove `CentralManager#getMaximumWriteLength` method.
|
||||
* Remove `PeripheralManager#getMaximumWriteLength` method.
|
||||
* Remove `ReadGattCharacteristicCommandEventArgs` and `WriteGattCharacteristicCommandEventArgs`.
|
||||
* Remove `PeripheralManager#readCharacteristicCommandReceived` and `PeripheralManager#writeCharacteristicCommandReceived`.
|
||||
* Remove `PeripheralManager#sendReadCharacteristicReply` and `PeripheralManager#sendWriteCharacteristicReply`.
|
||||
* Move `CentralManager#state` to `CentralManager#getState()`.
|
||||
* Move `PeripheralStateChangedEventArgs` to `ConnectionStateChangedEventArgs`.
|
||||
* Move `CentralManager#peripheralStateChanged` to `CentralManager#connectionStateChanged`.
|
||||
* Move `GattCharacteristicValueChangedEventArgs` to `GattCharacteristicNotifiedEventArgs`.
|
||||
* Move `CentralManager#characteristicValueChanged` to `CentralManager#characteristicNotified`.
|
||||
* Move `CentralManager#notifyCharacteristic` to `CentralManager#setCharacteristicNotifyState`.
|
||||
* Move `PeripheralManager#notifyCharacteristicValueChanged` to `PeripheralManager#writeCharacteristic`.
|
||||
* Move `NotifyGattCharacteristicCommandEventArgs` to `GattCharacteristicNotifyStateChangedEventArgs`.
|
||||
* Move `PeripheralManager#notifyCharacteristicCommandReceived` to `PeripheralManager#characteristicNotifyStateChanged`.
|
||||
|
||||
|
||||
## 5.0.0-dev.10
|
||||
|
||||
* Fix `Uint8List#trimGATT` throws when the value is not exceeded 512 bytes.
|
||||
|
||||
## 5.0.0-dev.9
|
||||
|
||||
* Add `PeripheralManager#characteristicRead`.
|
||||
|
||||
## 5.0.0-dev.8
|
||||
|
||||
* Add `PeripheralManager#readCharacteristic`.
|
||||
* Move `PeripheralManager#notifyCharacteristic` to `PeripheralManager#writeCharacteristic`.
|
||||
|
||||
## 5.0.0-dev.7
|
||||
|
||||
* Remove `GattCharacteristicReadEventArgs`.
|
||||
* Remove `PeripheralManager#characteristicRead`.
|
||||
|
||||
## 5.0.0-dev.6
|
||||
|
||||
* Remove the final modifier form `MyGattCharacteristic#value` and `MyGattDescriptor#value` and trim by 512 bytes.
|
||||
|
||||
## 5.0.0-dev.5
|
||||
|
||||
* Move `CentralManager#state` to `CentralManager#getState()`.
|
||||
* Move `PeripheralStateChangedEventArgs` to `ConnectionStateChangedEventArgs`.
|
||||
* Move `GattCharacteristicValueChangedEventArgs` to `GattCharacteristicNotifiedEventArgs`.
|
||||
* Move `CentralManager#peripheralStateChanged` to `CentralManager#connectionStateChanged`.
|
||||
* Move `CentralManager#characteristicValueChanged` to `CentralManager#characteristicNotified`.
|
||||
* Move `CentralManager#notifyCharacteristic` to `CentralManager#setCharacteristicNotifyState`.
|
||||
* Remove `ReadGattCharacteristicCommandEventArgs` and `WriteGattCharacteristicCommandEventArgs`.
|
||||
* Move `NotifyGattCharacteristicCommandEventArgs` to `GattCharacteristicNotifyStateChangedEventArgs`.
|
||||
* Remove `PeripheralManager#readCharacteristicCommandReceived` and `PeripheralManager#writeCharacteristicCommandReceived`.
|
||||
* Add `PeripheralManager#characteristicRead` and `PeripheralManager#characteristicWritten`.
|
||||
* Move `PeripheralManager#notifyCharacteristicCommandReceived` to `PeripheralManager#characteristicNotifyStateChanged`.
|
||||
* Remove `PeripheralManager#sendReadCharacteristicReply` and `PeripheralManager#sendWriteCharacteristicReply`.
|
||||
* Add `GattCharacteristicReadEventArgs` and `GattCharacteristicWrittenEventArgs`.
|
||||
* Move `PeripheralManager#notifyCharacteristicValueChanged` to `PeripheralManager#notifyCharacteristic`.
|
||||
* Remove `MyCentralManager` and `MyPeripheralManager`.
|
||||
|
||||
## 5.0.0-dev.4
|
||||
|
||||
* Optimize `MyGattService` and `MyGattCharacteristic`.
|
||||
|
||||
## 5.0.0-dev.3
|
||||
|
||||
* Remove `CentralManager#getMaximumWriteLength` method.
|
||||
* Remove `PeripheralManager#getMaximumWriteLength` method.
|
||||
|
||||
## 5.0.0-dev.2
|
||||
|
||||
* Add `UUID#fromAddress` constructor.
|
||||
* Override `hashCode` and `==` of `MyCentral` and `MyPeripheral`.
|
||||
|
||||
## 5.0.0-dev.1
|
||||
|
||||
* Add `MyBluetoothLowEnergyPeer` and `MyGattAttribute`.
|
||||
* Remove `MyObject` base class.
|
||||
* Use `LoggerProvider` instead of custom logger.
|
||||
|
||||
## 4.0.0
|
||||
|
||||
* Remove `BluetoothLowEnergy` class.
|
||||
|
@ -9,12 +9,12 @@ same interface.
|
||||
# Usage
|
||||
|
||||
To implement a new platform-specific implementation of `bluetooth_low_energy`,
|
||||
extend [`MyCentralManager`][2] with an implementation that performs the
|
||||
extend [`CentralManager`][2] with an implementation that performs the
|
||||
platform-specific behavior, and when you register your plugin, set the default
|
||||
`MyCentralManager` by calling `MyCentralManager.instance = MyCentralManagerImpl()`,
|
||||
extend [`MyPeripheralManager`][3] with an implementation that performs the
|
||||
`CentralManager` by calling `CentralManager.instance = MyCentralManager()`,
|
||||
extend [`PeripheralManager`][3] with an implementation that performs the
|
||||
platform-specific behavior, and when you register your plugin, set the default
|
||||
`MyPeripheralManager` by calling `MyPeripheralManager.instance = MyPeripheralManagerImpl()`.
|
||||
`PeripheralManager` by calling `PeripheralManager.instance = MyPeripheralManager()`.
|
||||
|
||||
# Note on breaking changes
|
||||
|
||||
|
@ -14,10 +14,10 @@ export 'src/bluetooth_low_energy_state.dart';
|
||||
export 'src/bluetooth_low_energy_event_args.dart';
|
||||
export 'src/bluetooth_low_energy_manager.dart';
|
||||
export 'src/bluetooth_low_energy_peer.dart';
|
||||
export 'src/central_manager_event_args.dart';
|
||||
export 'src/central_event_args.dart';
|
||||
export 'src/central_manager.dart';
|
||||
export 'src/central.dart';
|
||||
export 'src/peripheral_manager_event_args.dart';
|
||||
export 'src/peripheral_event_args.dart';
|
||||
export 'src/peripheral_manager.dart';
|
||||
export 'src/peripheral.dart';
|
||||
export 'src/uuid.dart';
|
||||
@ -28,11 +28,10 @@ export 'src/gatt_characteristic.dart';
|
||||
export 'src/gatt_characteristic_property.dart';
|
||||
export 'src/gatt_characteristic_write_type.dart';
|
||||
export 'src/gatt_descriptor.dart';
|
||||
export 'src/my_central_manager.dart';
|
||||
export 'src/my_peripheral_manager.dart';
|
||||
export 'src/my_object.dart';
|
||||
export 'src/my_bluetooth_low_energy_peer.dart';
|
||||
export 'src/my_central.dart';
|
||||
export 'src/my_peripheral.dart';
|
||||
export 'src/my_gatt_attribute.dart';
|
||||
export 'src/my_gatt_service.dart';
|
||||
export 'src/my_gatt_characteristic.dart';
|
||||
export 'src/my_gatt_descriptor.dart';
|
||||
|
@ -5,12 +5,12 @@ import 'bluetooth_low_energy_state.dart';
|
||||
|
||||
/// The abstract base class that manages central and peripheral objects.
|
||||
abstract class BluetoothLowEnergyManager implements LogController {
|
||||
/// The current state of the manager.
|
||||
BluetoothLowEnergyState get state;
|
||||
|
||||
/// Tells the manager’s state updated.
|
||||
/// Tells the manager's state updated.
|
||||
Stream<BluetoothLowEnergyStateChangedEventArgs> get stateChanged;
|
||||
|
||||
/// Sets up the manager.
|
||||
Future<void> setUp();
|
||||
|
||||
/// Gets the manager's state.
|
||||
Future<BluetoothLowEnergyState> getState();
|
||||
}
|
||||
|
@ -0,0 +1,51 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'advertisement.dart';
|
||||
import 'event_args.dart';
|
||||
import 'gatt_characteristic.dart';
|
||||
import 'peripheral.dart';
|
||||
|
||||
/// The discovered event arguments.
|
||||
class DiscoveredEventArgs extends EventArgs {
|
||||
/// The disvered peripheral.
|
||||
final Peripheral peripheral;
|
||||
|
||||
/// The rssi of the peripheral.
|
||||
final int rssi;
|
||||
|
||||
/// The advertisement of the peripheral.
|
||||
final Advertisement advertisement;
|
||||
|
||||
/// Constructs a [DiscoveredEventArgs].
|
||||
DiscoveredEventArgs(this.peripheral, this.rssi, this.advertisement);
|
||||
}
|
||||
|
||||
/// The connection state cahnged event arguments.
|
||||
class ConnectionStateChangedEventArgs extends EventArgs {
|
||||
/// The peripheral which connection state changed.
|
||||
final Peripheral peripheral;
|
||||
|
||||
/// The connection state.
|
||||
final bool connectionState;
|
||||
|
||||
/// Constructs a [ConnectionStateChangedEventArgs].
|
||||
ConnectionStateChangedEventArgs(
|
||||
this.peripheral,
|
||||
this.connectionState,
|
||||
);
|
||||
}
|
||||
|
||||
/// The GATT characteristic notified event arguments.
|
||||
class GattCharacteristicNotifiedEventArgs extends EventArgs {
|
||||
/// The GATT characteristic which notified.
|
||||
final GattCharacteristic characteristic;
|
||||
|
||||
/// The notified value.
|
||||
final Uint8List value;
|
||||
|
||||
/// Constructs a [GattCharacteristicNotifiedEventArgs].
|
||||
GattCharacteristicNotifiedEventArgs(
|
||||
this.characteristic,
|
||||
this.value,
|
||||
);
|
||||
}
|
@ -1,28 +1,55 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:log_service/log_service.dart';
|
||||
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
|
||||
|
||||
import 'bluetooth_low_energy_manager.dart';
|
||||
import 'central_manager_event_args.dart';
|
||||
import 'central_event_args.dart';
|
||||
import 'gatt_characteristic.dart';
|
||||
import 'gatt_characteristic_write_type.dart';
|
||||
import 'gatt_descriptor.dart';
|
||||
import 'gatt_service.dart';
|
||||
import 'my_central_manager.dart';
|
||||
import 'peripheral.dart';
|
||||
|
||||
/// An object that scans for, discovers, connects to, and manages peripherals.
|
||||
abstract class CentralManager extends BluetoothLowEnergyManager {
|
||||
/// The instance of [CentralManager] to use.
|
||||
static CentralManager get instance => MyCentralManager.instance;
|
||||
///
|
||||
/// Platform-specific implementations should implement this class to support [CentralManager].
|
||||
abstract class CentralManager extends PlatformInterface
|
||||
with LoggerProvider, LoggerController
|
||||
implements BluetoothLowEnergyManager {
|
||||
static final Object _token = Object();
|
||||
|
||||
static CentralManager? _instance;
|
||||
|
||||
/// The default instance of [CentralManager] to use.
|
||||
static CentralManager get instance {
|
||||
final instance = _instance;
|
||||
if (instance == null) {
|
||||
throw UnimplementedError(
|
||||
'CentralManager is not implemented on this platform.');
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/// Platform-specific implementations should set this with their own
|
||||
/// platform-specific class that extends [CentralManager] when
|
||||
/// they register themselves.
|
||||
static set instance(CentralManager instance) {
|
||||
PlatformInterface.verifyToken(instance, _token);
|
||||
_instance = instance;
|
||||
}
|
||||
|
||||
/// Constructs a [CentralManager].
|
||||
CentralManager() : super(token: _token);
|
||||
|
||||
/// Tells the central manager discovered a peripheral while scanning for devices.
|
||||
Stream<DiscoveredEventArgs> get discovered;
|
||||
|
||||
/// Tells that retrieving the specified peripheral's state changed.
|
||||
Stream<PeripheralStateChangedEventArgs> get peripheralStateChanged;
|
||||
/// Tells that retrieving the specified peripheral's connection lost.
|
||||
Stream<ConnectionStateChangedEventArgs> get connectionStateChanged;
|
||||
|
||||
/// Tells that retrieving the specified characteristic’s value changed.
|
||||
Stream<GattCharacteristicValueChangedEventArgs>
|
||||
get characteristicValueChanged;
|
||||
Stream<GattCharacteristicNotifiedEventArgs> get characteristicNotified;
|
||||
|
||||
/// Scans for peripherals that are advertising services.
|
||||
Future<void> startDiscovery();
|
||||
@ -36,12 +63,6 @@ abstract class CentralManager extends BluetoothLowEnergyManager {
|
||||
/// Cancels an active or pending local connection to a peripheral.
|
||||
Future<void> disconnect(Peripheral peripheral);
|
||||
|
||||
/// Gets the maximum amount of data, in bytes, you can send to a characteristic in a single write type.
|
||||
Future<int> getMaximumWriteLength(
|
||||
Peripheral peripheral, {
|
||||
required GattCharacteristicWriteType type,
|
||||
});
|
||||
|
||||
/// Retrieves the current RSSI value for the peripheral while connected to the central manager.
|
||||
Future<int> readRSSI(Peripheral peripheral);
|
||||
|
||||
@ -52,6 +73,8 @@ abstract class CentralManager extends BluetoothLowEnergyManager {
|
||||
Future<Uint8List> readCharacteristic(GattCharacteristic characteristic);
|
||||
|
||||
/// Writes the value of a characteristic.
|
||||
///
|
||||
/// The maximum size of the value is 512, all bytes that exceed this size will be discarded.
|
||||
Future<void> writeCharacteristic(
|
||||
GattCharacteristic characteristic, {
|
||||
required Uint8List value,
|
||||
@ -59,7 +82,7 @@ abstract class CentralManager extends BluetoothLowEnergyManager {
|
||||
});
|
||||
|
||||
/// Sets notifications or indications for the value of a specified characteristic.
|
||||
Future<void> notifyCharacteristic(
|
||||
Future<void> setCharacteristicNotifyState(
|
||||
GattCharacteristic characteristic, {
|
||||
required bool state,
|
||||
});
|
||||
|
@ -1,45 +0,0 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'advertisement.dart';
|
||||
import 'event_args.dart';
|
||||
import 'gatt_characteristic.dart';
|
||||
import 'peripheral.dart';
|
||||
|
||||
/// The discovered event arguments.
|
||||
class DiscoveredEventArgs extends EventArgs {
|
||||
/// The disvered peripheral.
|
||||
final Peripheral peripheral;
|
||||
|
||||
/// The rssi of the peripheral.
|
||||
final int rssi;
|
||||
|
||||
/// The advertisement of the peripheral.
|
||||
final Advertisement advertisement;
|
||||
|
||||
/// Constructs a [DiscoveredEventArgs].
|
||||
DiscoveredEventArgs(this.peripheral, this.rssi, this.advertisement);
|
||||
}
|
||||
|
||||
/// The peripheral state changed event arguments.
|
||||
class PeripheralStateChangedEventArgs extends EventArgs {
|
||||
/// The peripheral which state is changed.
|
||||
final Peripheral peripheral;
|
||||
|
||||
/// The new state of the peripheral.
|
||||
final bool state;
|
||||
|
||||
/// Constructs a [PeripheralStateChangedEventArgs].
|
||||
PeripheralStateChangedEventArgs(this.peripheral, this.state);
|
||||
}
|
||||
|
||||
/// The GATT characteristic value changed event arguments.
|
||||
class GattCharacteristicValueChangedEventArgs extends EventArgs {
|
||||
/// The GATT characteristic which value is changed.
|
||||
final GattCharacteristic characteristic;
|
||||
|
||||
/// The changed value of the characteristic.
|
||||
final Uint8List value;
|
||||
|
||||
/// Constructs a [GattCharacteristicValueChangedEventArgs].
|
||||
GattCharacteristicValueChangedEventArgs(this.characteristic, this.value);
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'gatt_attribute.dart';
|
||||
import 'gatt_characteristic_property.dart';
|
||||
import 'gatt_descriptor.dart';
|
||||
@ -17,11 +19,13 @@ abstract class GattCharacteristic extends GattAttribute {
|
||||
factory GattCharacteristic({
|
||||
required UUID uuid,
|
||||
required List<GattCharacteristicProperty> properties,
|
||||
required Uint8List value,
|
||||
required List<GattDescriptor> descriptors,
|
||||
}) =>
|
||||
MyGattCharacteristic(
|
||||
uuid: uuid,
|
||||
properties: properties,
|
||||
value: value,
|
||||
descriptors: descriptors.cast<MyGattDescriptor>(),
|
||||
);
|
||||
}
|
||||
|
@ -0,0 +1,11 @@
|
||||
import 'bluetooth_low_energy_peer.dart';
|
||||
import 'uuid.dart';
|
||||
|
||||
abstract class MyBluetoothLowEnergyPeer implements BluetoothLowEnergyPeer {
|
||||
@override
|
||||
final UUID uuid;
|
||||
|
||||
MyBluetoothLowEnergyPeer({
|
||||
required this.uuid,
|
||||
});
|
||||
}
|
@ -1,13 +1,16 @@
|
||||
import 'central.dart';
|
||||
import 'my_object.dart';
|
||||
import 'uuid.dart';
|
||||
|
||||
class MyCentral extends MyObject implements Central {
|
||||
@override
|
||||
final UUID uuid;
|
||||
import 'my_bluetooth_low_energy_peer.dart';
|
||||
|
||||
class MyCentral extends MyBluetoothLowEnergyPeer implements Central {
|
||||
MyCentral({
|
||||
required super.hashCode,
|
||||
required this.uuid,
|
||||
required super.uuid,
|
||||
});
|
||||
|
||||
@override
|
||||
int get hashCode => uuid.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return other is Central && other.uuid == uuid;
|
||||
}
|
||||
}
|
||||
|
@ -1,41 +0,0 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:log_service/log_service.dart';
|
||||
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
|
||||
|
||||
import 'central_manager.dart';
|
||||
|
||||
/// Platform-specific implementations should implement this class to support
|
||||
/// [CentralManager].
|
||||
abstract class MyCentralManager extends PlatformInterface
|
||||
with LoggerController
|
||||
implements CentralManager {
|
||||
static final Object _token = Object();
|
||||
|
||||
static MyCentralManager? _instance;
|
||||
|
||||
/// The default instance of [MyCentralManager] to use.
|
||||
static MyCentralManager get instance {
|
||||
final instance = _instance;
|
||||
if (instance == null) {
|
||||
throw UnimplementedError(
|
||||
'CentralManager is not implemented on this platform.',
|
||||
);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/// Platform-specific implementations should set this with their own
|
||||
/// platform-specific class that extends [MyCentralManager] when
|
||||
/// they register themselves.
|
||||
static set instance(MyCentralManager instance) {
|
||||
PlatformInterface.verifyToken(instance, _token);
|
||||
_instance = instance;
|
||||
}
|
||||
|
||||
/// Constructs a [MyCentralManager].
|
||||
MyCentralManager() : super(token: _token);
|
||||
|
||||
@protected
|
||||
@override
|
||||
Logger get logger => Logger('$CentralManager');
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'gatt_attribute.dart';
|
||||
import 'uuid.dart';
|
||||
|
||||
abstract class MyGattAttribute implements GattAttribute {
|
||||
@override
|
||||
final UUID uuid;
|
||||
|
||||
MyGattAttribute({
|
||||
required this.uuid,
|
||||
});
|
||||
}
|
||||
|
||||
extension MyGattAttributeUint8List on Uint8List {
|
||||
Uint8List trimGATT() {
|
||||
return length > 512 ? sublist(0, 512) : this;
|
||||
}
|
||||
}
|
@ -1,21 +1,27 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'gatt_characteristic.dart';
|
||||
import 'gatt_characteristic_property.dart';
|
||||
import 'my_gatt_attribute.dart';
|
||||
import 'my_gatt_descriptor.dart';
|
||||
import 'my_object.dart';
|
||||
import 'uuid.dart';
|
||||
|
||||
class MyGattCharacteristic extends MyObject implements GattCharacteristic {
|
||||
@override
|
||||
final UUID uuid;
|
||||
class MyGattCharacteristic extends MyGattAttribute
|
||||
implements GattCharacteristic {
|
||||
@override
|
||||
final List<GattCharacteristicProperty> properties;
|
||||
Uint8List? _value;
|
||||
@override
|
||||
final List<MyGattDescriptor> descriptors;
|
||||
|
||||
MyGattCharacteristic({
|
||||
super.hashCode,
|
||||
required this.uuid,
|
||||
required super.uuid,
|
||||
required this.properties,
|
||||
Uint8List? value,
|
||||
required this.descriptors,
|
||||
});
|
||||
}) : _value = value?.trimGATT();
|
||||
|
||||
Uint8List get value => _value ?? Uint8List.fromList([]);
|
||||
set value(Uint8List value) {
|
||||
_value = value.trimGATT();
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,18 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'gatt_descriptor.dart';
|
||||
import 'my_object.dart';
|
||||
import 'uuid.dart';
|
||||
import 'my_gatt_attribute.dart';
|
||||
|
||||
class MyGattDescriptor extends MyObject implements GattDescriptor {
|
||||
@override
|
||||
final UUID uuid;
|
||||
final Uint8List? value;
|
||||
class MyGattDescriptor extends MyGattAttribute implements GattDescriptor {
|
||||
Uint8List? _value;
|
||||
|
||||
MyGattDescriptor({
|
||||
super.hashCode,
|
||||
required this.uuid,
|
||||
this.value,
|
||||
});
|
||||
required super.uuid,
|
||||
Uint8List? value,
|
||||
}) : _value = value?.trimGATT();
|
||||
|
||||
Uint8List get value => _value ?? Uint8List.fromList([]);
|
||||
set value(Uint8List value) {
|
||||
_value = value.trimGATT();
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,13 @@
|
||||
import 'gatt_service.dart';
|
||||
import 'my_gatt_attribute.dart';
|
||||
import 'my_gatt_characteristic.dart';
|
||||
import 'my_object.dart';
|
||||
import 'uuid.dart';
|
||||
|
||||
class MyGattService extends MyObject implements GattService {
|
||||
@override
|
||||
final UUID uuid;
|
||||
class MyGattService extends MyGattAttribute implements GattService {
|
||||
@override
|
||||
final List<MyGattCharacteristic> characteristics;
|
||||
|
||||
MyGattService({
|
||||
super.hashCode,
|
||||
required this.uuid,
|
||||
required super.uuid,
|
||||
required this.characteristics,
|
||||
});
|
||||
}
|
||||
|
@ -1,13 +0,0 @@
|
||||
abstract class MyObject {
|
||||
final int? _hashCode;
|
||||
|
||||
MyObject({int? hashCode}) : _hashCode = hashCode;
|
||||
|
||||
@override
|
||||
int get hashCode => _hashCode ?? super.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return other is MyObject && other.hashCode == hashCode;
|
||||
}
|
||||
}
|
@ -1,13 +1,16 @@
|
||||
import 'my_object.dart';
|
||||
import 'my_bluetooth_low_energy_peer.dart';
|
||||
import 'peripheral.dart';
|
||||
import 'uuid.dart';
|
||||
|
||||
class MyPeripheral extends MyObject implements Peripheral {
|
||||
@override
|
||||
final UUID uuid;
|
||||
|
||||
class MyPeripheral extends MyBluetoothLowEnergyPeer implements Peripheral {
|
||||
MyPeripheral({
|
||||
required super.hashCode,
|
||||
required this.uuid,
|
||||
required super.uuid,
|
||||
});
|
||||
|
||||
@override
|
||||
int get hashCode => uuid.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return other is Peripheral && other.uuid == uuid;
|
||||
}
|
||||
}
|
||||
|
@ -1,41 +0,0 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:log_service/log_service.dart';
|
||||
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
|
||||
|
||||
import 'peripheral_manager.dart';
|
||||
|
||||
/// Platform-specific implementations should implement this class to support
|
||||
/// [PeripheralManager].
|
||||
abstract class MyPeripheralManager extends PlatformInterface
|
||||
with LoggerController
|
||||
implements PeripheralManager {
|
||||
static final Object _token = Object();
|
||||
|
||||
static MyPeripheralManager? _instance;
|
||||
|
||||
/// The default instance of [MyPeripheralManager] to use.
|
||||
static MyPeripheralManager get instance {
|
||||
final instance = _instance;
|
||||
if (instance == null) {
|
||||
throw UnimplementedError(
|
||||
'PeripheralManager is not implemented on this platform.',
|
||||
);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/// Platform-specific implementations should set this with their own
|
||||
/// platform-specific class that extends [MyPeripheralManager] when
|
||||
/// they register themselves.
|
||||
static set instance(MyPeripheralManager instance) {
|
||||
PlatformInterface.verifyToken(instance, _token);
|
||||
_instance = instance;
|
||||
}
|
||||
|
||||
/// Constructs a [MyPeripheralManager].
|
||||
MyPeripheralManager() : super(token: _token);
|
||||
|
||||
@protected
|
||||
@override
|
||||
Logger get logger => Logger('$PeripheralManager');
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'central.dart';
|
||||
import 'gatt_characteristic.dart';
|
||||
|
||||
/// The GATT characteristic written event arguments.
|
||||
class GattCharacteristicReadEventArgs {
|
||||
/// The central which read this characteristic.
|
||||
final Central central;
|
||||
|
||||
/// The GATT characteristic which value is read.
|
||||
final GattCharacteristic characteristic;
|
||||
|
||||
/// The value.
|
||||
final Uint8List value;
|
||||
|
||||
/// Constructs a [GattCharacteristicReadEventArgs].
|
||||
GattCharacteristicReadEventArgs(
|
||||
this.central,
|
||||
this.characteristic,
|
||||
this.value,
|
||||
);
|
||||
}
|
||||
|
||||
/// The GATT characteristic written event arguments.
|
||||
class GattCharacteristicWrittenEventArgs {
|
||||
/// The central which wrote this characteristic.
|
||||
final Central central;
|
||||
|
||||
/// The GATT characteristic which value is written.
|
||||
final GattCharacteristic characteristic;
|
||||
|
||||
/// The value.
|
||||
final Uint8List value;
|
||||
|
||||
/// Constructs a [GattCharacteristicWrittenEventArgs].
|
||||
GattCharacteristicWrittenEventArgs(
|
||||
this.central,
|
||||
this.characteristic,
|
||||
this.value,
|
||||
);
|
||||
}
|
||||
|
||||
/// The GATT characteristic notify state changed event arguments.
|
||||
class GattCharacteristicNotifyStateChangedEventArgs {
|
||||
/// The central which set this notify state.
|
||||
final Central central;
|
||||
|
||||
/// The GATT characteristic which notify state changed.
|
||||
final GattCharacteristic characteristic;
|
||||
|
||||
/// The notify state.
|
||||
final bool state;
|
||||
|
||||
/// Constructs a [GattCharacteristicNotifyStateChangedEventArgs].
|
||||
GattCharacteristicNotifyStateChangedEventArgs(
|
||||
this.central,
|
||||
this.characteristic,
|
||||
this.state,
|
||||
);
|
||||
}
|
@ -1,29 +1,55 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:log_service/log_service.dart';
|
||||
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
|
||||
|
||||
import 'advertisement.dart';
|
||||
import 'bluetooth_low_energy_manager.dart';
|
||||
import 'central.dart';
|
||||
import 'gatt_characteristic.dart';
|
||||
import 'gatt_service.dart';
|
||||
import 'my_peripheral_manager.dart';
|
||||
import 'peripheral_manager_event_args.dart';
|
||||
import 'peripheral_event_args.dart';
|
||||
|
||||
/// An object that manages and advertises peripheral services exposed by this app.
|
||||
abstract class PeripheralManager extends BluetoothLowEnergyManager {
|
||||
/// The instance of [PeripheralManger] to use.
|
||||
static PeripheralManager get instance => MyPeripheralManager.instance;
|
||||
///
|
||||
/// Platform-specific implementations should implement this class to support [PeripheralManager].
|
||||
abstract class PeripheralManager extends PlatformInterface
|
||||
with LoggerProvider, LoggerController
|
||||
implements BluetoothLowEnergyManager {
|
||||
static final Object _token = Object();
|
||||
|
||||
/// Tells that the local peripheral received an Attribute Protocol (ATT) read request for a characteristic with a dynamic value.
|
||||
Stream<ReadGattCharacteristicCommandEventArgs>
|
||||
get readCharacteristicCommandReceived;
|
||||
static PeripheralManager? _instance;
|
||||
|
||||
/// The default instance of [PeripheralManager] to use.
|
||||
static PeripheralManager get instance {
|
||||
final instance = _instance;
|
||||
if (instance == null) {
|
||||
throw UnimplementedError(
|
||||
'PeripheralManager is not implemented on this platform.');
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/// Platform-specific implementations should set this with their own
|
||||
/// platform-specific class that extends [PeripheralManager] when
|
||||
/// they register themselves.
|
||||
static set instance(PeripheralManager instance) {
|
||||
PlatformInterface.verifyToken(instance, _token);
|
||||
_instance = instance;
|
||||
}
|
||||
|
||||
/// Constructs a [PeripheralManager].
|
||||
PeripheralManager() : super(token: _token);
|
||||
|
||||
/// Tells that the local peripheral device received an Attribute Protocol (ATT) read request for a characteristic with a dynamic value.
|
||||
Stream<GattCharacteristicReadEventArgs> get characteristicRead;
|
||||
|
||||
/// Tells that the local peripheral device received an Attribute Protocol (ATT) write request for a characteristic with a dynamic value.
|
||||
Stream<WriteGattCharacteristicCommandEventArgs>
|
||||
get writeCharacteristicCommandReceived;
|
||||
Stream<GattCharacteristicWrittenEventArgs> get characteristicWritten;
|
||||
|
||||
/// Tells that the peripheral manager received a characteristic’s notify changed.
|
||||
Stream<NotifyGattCharacteristicCommandEventArgs>
|
||||
get notifyCharacteristicCommandReceived;
|
||||
Stream<GattCharacteristicNotifyStateChangedEventArgs>
|
||||
get characteristicNotifyStateChanged;
|
||||
|
||||
/// Publishes a service and any of its associated characteristics and characteristic descriptors to the local GATT database.
|
||||
Future<void> addService(GattService service);
|
||||
@ -40,33 +66,15 @@ abstract class PeripheralManager extends BluetoothLowEnergyManager {
|
||||
/// Stops advertising peripheral manager data.
|
||||
Future<void> stopAdvertising();
|
||||
|
||||
/// Gets the maximum amount of data, in bytes, that the central can receive in a
|
||||
/// single notification or indication.
|
||||
Future<int> getMaximumWriteLength(Central central);
|
||||
/// Retrieves the value of a specified characteristic.
|
||||
Future<Uint8List> readCharacteristic(GattCharacteristic characteristic);
|
||||
|
||||
/// Responds to a read request from a connected central.
|
||||
Future<void> sendReadCharacteristicReply(
|
||||
Central central, {
|
||||
required GattCharacteristic characteristic,
|
||||
required int id,
|
||||
required int offset,
|
||||
required bool status,
|
||||
required Uint8List value,
|
||||
});
|
||||
|
||||
/// Responds to a write request from a connected central.
|
||||
Future<void> sendWriteCharacteristicReply(
|
||||
Central central, {
|
||||
required GattCharacteristic characteristic,
|
||||
required int id,
|
||||
required int offset,
|
||||
required bool status,
|
||||
});
|
||||
|
||||
/// Send an updated characteristic value to one or more subscribed centrals, using a notification or indication.
|
||||
Future<void> notifyCharacteristicValueChanged(
|
||||
Central central, {
|
||||
required GattCharacteristic characteristic,
|
||||
/// Writes the value of a characteristic and sends an updated characteristic value to one or more subscribed centrals, using a notification or indication.
|
||||
///
|
||||
/// The maximum size of the value is 512, all bytes that exceed this size will be discarded.
|
||||
Future<void> writeCharacteristic(
|
||||
GattCharacteristic characteristic, {
|
||||
required Uint8List value,
|
||||
Central? central,
|
||||
});
|
||||
}
|
||||
|
@ -1,73 +0,0 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'central.dart';
|
||||
import 'gatt_characteristic.dart';
|
||||
|
||||
/// The read GATT characteristic command event arguments.
|
||||
class ReadGattCharacteristicCommandEventArgs {
|
||||
/// The central which send this read command.
|
||||
final Central central;
|
||||
|
||||
/// The GATT characteristic which value is to read.
|
||||
final GattCharacteristic characteristic;
|
||||
|
||||
/// The id of this read command.
|
||||
final int id;
|
||||
|
||||
/// The offset of this read command.
|
||||
final int offset;
|
||||
|
||||
/// Constructs a [ReadGattCharacteristicCommandEventArgs].
|
||||
ReadGattCharacteristicCommandEventArgs(
|
||||
this.central,
|
||||
this.characteristic,
|
||||
this.id,
|
||||
this.offset,
|
||||
);
|
||||
}
|
||||
|
||||
/// The write GATT characteristic command event arguments.
|
||||
class WriteGattCharacteristicCommandEventArgs {
|
||||
/// The central which send this write command.
|
||||
final Central central;
|
||||
|
||||
/// The GATT characteristic which value is to write.
|
||||
final GattCharacteristic characteristic;
|
||||
|
||||
/// The id of this write command.
|
||||
final int id;
|
||||
|
||||
/// The offset of this write command.
|
||||
final int offset;
|
||||
|
||||
/// The value of this write command.
|
||||
final Uint8List value;
|
||||
|
||||
/// Constructs a [WriteGattCharacteristicCommandEventArgs].
|
||||
WriteGattCharacteristicCommandEventArgs(
|
||||
this.central,
|
||||
this.characteristic,
|
||||
this.id,
|
||||
this.offset,
|
||||
this.value,
|
||||
);
|
||||
}
|
||||
|
||||
/// The notify GATT characteristic command event arguments.
|
||||
class NotifyGattCharacteristicCommandEventArgs {
|
||||
/// The central which send this notify command.
|
||||
final Central central;
|
||||
|
||||
/// The GATT characteristic which value is to notify.
|
||||
final GattCharacteristic characteristic;
|
||||
|
||||
/// The state of this notify command.
|
||||
final bool state;
|
||||
|
||||
/// Constructs a [NotifyGattCharacteristicCommandEventArgs].
|
||||
NotifyGattCharacteristicCommandEventArgs(
|
||||
this.central,
|
||||
this.characteristic,
|
||||
this.state,
|
||||
);
|
||||
}
|
@ -95,6 +95,16 @@ class UUID {
|
||||
]);
|
||||
}
|
||||
|
||||
/// Creates a new UUID form MAC address.
|
||||
factory UUID.fromAddress(String address) {
|
||||
final node = address.splitMapJoin(
|
||||
':',
|
||||
onMatch: (m) => '',
|
||||
);
|
||||
// We don't know the timestamp of the bluetooth device, use nil UUID as prefix.
|
||||
return UUID.fromString("00000000-0000-0000-0000-$node");
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
var v0 = value[0].toRadixString(16).padLeft(2, '0');
|
||||
|
@ -1,6 +1,6 @@
|
||||
name: bluetooth_low_energy_platform_interface
|
||||
description: A common platform interface for the bluetooth_low_energy plugin.
|
||||
version: 4.0.0
|
||||
version: 5.0.0
|
||||
homepage: https://github.com/yanshouwang/bluetooth_low_energy
|
||||
|
||||
environment:
|
||||
@ -10,7 +10,7 @@ environment:
|
||||
dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
plugin_platform_interface: ^2.0.2
|
||||
plugin_platform_interface: ^2.1.7
|
||||
log_service: ^1.0.0
|
||||
|
||||
dev_dependencies:
|
||||
|
@ -0,0 +1,46 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:bluetooth_low_energy_platform_interface/bluetooth_low_energy_platform_interface.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'Trim when value is empty.',
|
||||
() {
|
||||
final value = Uint8List.fromList([]);
|
||||
final actual = value.trimGATT();
|
||||
final matcher = value;
|
||||
expect(actual, matcher);
|
||||
},
|
||||
);
|
||||
test(
|
||||
'Trim when value is 100 bytes.',
|
||||
() {
|
||||
final elements = List.generate(100, (i) => i % 0xff);
|
||||
final value = Uint8List.fromList(elements);
|
||||
final actual = value.trimGATT();
|
||||
final matcher = value;
|
||||
expect(actual, matcher);
|
||||
},
|
||||
);
|
||||
test(
|
||||
'Trim when value is 512 bytes.',
|
||||
() {
|
||||
final elements = List.generate(512, (i) => i % 0xff);
|
||||
final value = Uint8List.fromList(elements);
|
||||
final actual = value.trimGATT();
|
||||
final matcher = value;
|
||||
expect(actual, matcher);
|
||||
},
|
||||
);
|
||||
test(
|
||||
'Trim when value is 1000 bytes.',
|
||||
() {
|
||||
final elements = List.generate(1000, (i) => i % 0xff);
|
||||
final value = Uint8List.fromList(elements);
|
||||
final actual = value.trimGATT();
|
||||
final matcher = Uint8List.fromList(elements.take(512).toList());
|
||||
expect(actual, matcher);
|
||||
},
|
||||
);
|
||||
}
|
23
bluetooth_low_energy_platform_interface/test/uuid_test.dart
Normal file
23
bluetooth_low_energy_platform_interface/test/uuid_test.dart
Normal file
@ -0,0 +1,23 @@
|
||||
import 'package:bluetooth_low_energy_platform_interface/bluetooth_low_energy_platform_interface.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'Create UUID form MAC address with colons.',
|
||||
() {
|
||||
const address = 'AA:BB:CC:DD:EE:FF';
|
||||
final actual = UUID.fromAddress(address);
|
||||
final matcher = UUID.fromString('00000000-0000-0000-0000-AABBCCDDEEFF');
|
||||
expect(actual, matcher);
|
||||
},
|
||||
);
|
||||
test(
|
||||
'Create UUID form MAC address without colons.',
|
||||
() {
|
||||
const address = 'AABBCCDDEEFF';
|
||||
final actual = UUID.fromAddress(address);
|
||||
final matcher = UUID.fromString('00000000-0000-0000-0000-AABBCCDDEEFF');
|
||||
expect(actual, matcher);
|
||||
},
|
||||
);
|
||||
}
|
Reference in New Issue
Block a user