* feat: 调整插件接口

* feat: 增加 `Logger` 类

* fix: 部分参数改为可选参数

* fix: 调整接口

* fix: 重命名 AdvertiseData 为 Advertisement

* fix: 移除 `Logger.level` 属性

* fix: 适配 4.0.0

* fix: 更新依赖项

* fix: 优化导入方式

* fix: 修改版本号

* fix: 适配 4.0.0

* feat: 部分适配 4.0.0

* feat: 适配 4.0.0

* feat: 适配 4.0.0

* fix: 调整接口

* feat: 调整日志接口

* fix: 修复日志错误

* fix: 临时提交

* draft: 临时提交

* fix: 使用 log_service 插件替换 logging 插件

* fix: 更新 log_service 版本

* fix: 4.0.0

* feat: 4.0.0

* feat: 4.0.0
This commit is contained in:
iAMD
2023-10-30 18:06:14 +08:00
committed by GitHub
parent 3c10caa5dd
commit cfe0eda4a3
68 changed files with 1316 additions and 814 deletions

View File

@ -1,3 +1,73 @@
## 4.0.0
* Remove `BluetoothLowEnergy` class.
* Update `CentralManger` to extends `PlatformInterface`.
* Update `PeripheralManager` to extends `PlatformInterface`.
* Update `README.md`.
* Change some `PeripheralManager` methods' arguments to required optional arguments.
* Move `AdvertiseData` class to `Advertisement` class.
* Remove `BluetoothLowEnergyError` class.
* Add `MyCentralManager` and `MyPeripheralManager` abstract classes.
* Add `LogController` interface to `BluetoothLowEnergyManager`.
* Fix issues.
## 4.0.0-dev.12
* Update `log_service` dependency.
## 4.0.0-dev.11
* Use `log_service` instead of `logging` to simplify project structure.
## 4.0.0-dev.10
* Fix the issue that messages were logged twice and other logger's messages were also logged by this logger.
## 4.0.0-dev.9
* Remove `Logger`, use `logging` package instead.
* Add `SetUp` interface class and `MySetUp` mixin.
* Add `LoggerController` interface class and `MyLoggerController` mixin.
* Update `CentralManager` to implements `SetUp` and `LoggerController`.
* Update `PeripheralManager` to implements `SetUp` and `LoggerController`.
## 4.0.0-dev.8
* Remove `BluetoothLowEnergyError`, use `PlatformException` instead.
* Add `MyCentralManager` and `MyPeripheralManager` abstract classes.
* Optimize project's structure.
## 4.0.0-dev.7
* Remove `Logger.level` filed, as the level of `logging` package is a global option, which can be changed by anyone.
## 4.0.0-dev.6
* Move `AdvertiseData` class to `Advertisement` class.
## 4.0.0-dev.5
* Fix export error.
## 4.0.0-dev.4
* Optimize project's structure.
## 4.0.0-dev.3
* Change some `PeripheralManager` methods' arguments to required optional arguments.
## 4.0.0-dev.2
* Add `Logger` class.
## 4.0.0-dev.1
* Remove `BluetoothLowEnergy` class.
* Update `CentralManger` to extends `PlatformInterface`.
* Update `PeripheralManager` to extends `PlatformInterface`.
* Update `README.md`.
## 3.0.0
* Add `PeripheralManager` api.

View File

@ -8,11 +8,13 @@ same interface.
# Usage
To implement a new platform-specific implementation of `bluetooth_low_energy`, extend
[`CentralController`][2] with an implementation that performs the
To implement a new platform-specific implementation of `bluetooth_low_energy`,
extend [`MyCentralManager`][2] with an implementation that performs the
platform-specific behavior, and when you register your plugin, set the default
`CentralController` by calling
`CentralController.instance = MyCentralController()`.
`MyCentralManager` by calling `MyCentralManager.instance = MyCentralManagerImpl()`,
extend [`MyPeripheralManager`][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()`.
# Note on breaking changes
@ -23,4 +25,5 @@ See https://flutter.dev/go/platform-interface-breaking-changes for a discussion
on why a less-clean interface is preferable to a breaking change.
[1]: https://pub.dev/packages/bluetooth_low_energy
[2]: lib/src/central_controller.dart
[2]: lib/src/central_manager.dart
[3]: lib/src/peripheral_manager.dart

View File

@ -1,24 +1,38 @@
export 'src/errors.dart';
/// A common platform interface for the [`bluetooth_low_energy`][1] plugin.
///
/// This interface allows platform-specific implementations of the `bluetooth_low_energy`
/// plugin, as well as the plugin itself, to ensure they are supporting the
/// same interface.
///
/// [1]: https://pub.dev/packages/bluetooth_low_energy
library;
export 'package:log_service/log_service.dart';
export 'src/event_args.dart';
export 'src/bluetooth_low_energy.dart';
export 'src/bluetooth_low_energy_manager.dart';
export 'src/bluetooth_low_energy_state.dart';
export 'src/central_manager.dart';
export 'src/peripheral_manager.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_manager.dart';
export 'src/central.dart';
export 'src/peripheral_manager_event_args.dart';
export 'src/peripheral_manager.dart';
export 'src/peripheral.dart';
export 'src/uuid.dart';
export 'src/advertise_data.dart';
export 'src/advertisement.dart';
export 'src/manufacturer_specific_data.dart';
export 'src/gatt_service.dart';
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_central.dart';
export 'src/my_peripheral.dart';
export 'src/my_gatt_service.dart';
export 'src/my_gatt_characteristic.dart';
export 'src/my_gatt_descriptor.dart';
export 'src/my_central.dart';

View File

@ -3,8 +3,8 @@ import 'dart:typed_data';
import 'manufacturer_specific_data.dart';
import 'uuid.dart';
/// The advertise data discovered from a peripheral.
class AdvertiseData {
/// The advertisement of the peripheral.
class Advertisement {
/// The name of the peripheral.
final String? name;
@ -17,8 +17,8 @@ class AdvertiseData {
/// The manufacturer specific data of the peripheral.
final ManufacturerSpecificData? manufacturerSpecificData;
/// Constructs an [AdvertiseData].
AdvertiseData({
/// Constructs an [Advertisement].
Advertisement({
this.name,
this.serviceUUIDs = const [],
this.serviceData = const {},

View File

@ -1,41 +0,0 @@
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'central_manager.dart';
import 'peripheral_manager.dart';
/// The bluetooth low energy interface.
///
/// Call `setUp` before use any api.
abstract class BluetoothLowEnergy extends PlatformInterface {
/// Constructs a [BluetoothLowEnergy].
BluetoothLowEnergy() : super(token: _token);
static final Object _token = Object();
static BluetoothLowEnergy? _instance;
/// The default instance of [BluetoothLowEnergy] to use.
static BluetoothLowEnergy get instance {
final instance = _instance;
if (instance == null) {
throw UnimplementedError(
'`BluetoothLowEnergy` is not implemented on this platform.',
);
}
return instance;
}
/// Platform-specific implementations should set this with their own
/// platform-specific class that extends [BluetoothLowEnergy] when
/// they register themselves.
static set instance(BluetoothLowEnergy instance) {
PlatformInterface.verifyToken(instance, _token);
_instance = instance;
}
/// Gets the instance of central manager.
CentralManager get centralManager;
/// Gets the instance of peripheral manager.
PeripheralManager get peripheralManager;
}

View File

@ -0,0 +1,11 @@
import 'bluetooth_low_energy_state.dart';
import 'event_args.dart';
/// The bluetooth low energy state changed event arguments.
class BluetoothLowEnergyStateChangedEventArgs extends EventArgs {
/// The new state of the bluetooth low energy.
final BluetoothLowEnergyState state;
/// Constructs a [BluetoothLowEnergyStateChangedEventArgs].
BluetoothLowEnergyStateChangedEventArgs(this.state);
}

View File

@ -1,14 +1,16 @@
import 'package:log_service/log_service.dart';
import 'bluetooth_low_energy_event_args.dart';
import 'bluetooth_low_energy_state.dart';
import 'event_args.dart';
/// The abstract base class that manages central and peripheral objects.
abstract class BluetoothLowEnergyManager {
abstract class BluetoothLowEnergyManager implements LogController {
/// The current state of the manager.
BluetoothLowEnergyState get state;
/// Tells the managers state updated.
Stream<BluetoothLowEnergyStateChangedEventArgs> get stateChanged;
/// Sets up this bluetooth low energy manager.
/// Sets up the manager.
Future<void> setUp();
}

View File

@ -1,19 +1,18 @@
import 'dart:typed_data';
import 'bluetooth_low_energy.dart';
import 'bluetooth_low_energy_manager.dart';
import 'event_args.dart';
import 'central_manager_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 {
/// Gets the instance of [CentralManager].
static CentralManager get instance =>
BluetoothLowEnergy.instance.centralManager;
/// The instance of [CentralManager] to use.
static CentralManager get instance => MyCentralManager.instance;
/// Tells the central manager discovered a peripheral while scanning for devices.
Stream<DiscoveredEventArgs> get discovered;

View File

@ -0,0 +1,45 @@
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);
}

View File

@ -1,13 +0,0 @@
/// The bluetooth low energy error.
class BluetoothLowEnergyError extends Error {
/// The message of this error.
final String message;
/// Constructs a [BluetoothLowEnergyError].
BluetoothLowEnergyError(this.message);
@override
String toString() {
return 'BluetoothLowEnergyError: $message';
}
}

View File

@ -1,100 +1,2 @@
import 'dart:typed_data';
import 'advertise_data.dart';
import 'bluetooth_low_energy_state.dart';
import 'central.dart';
import 'gatt_characteristic.dart';
import 'peripheral.dart';
/// The base event arguments.
abstract class EventArgs {}
/// The bluetooth low energy state changed event arguments.
class BluetoothLowEnergyStateChangedEventArgs extends EventArgs {
/// The new state of the bluetooth low energy.
final BluetoothLowEnergyState state;
/// Constructs a [BluetoothLowEnergyStateChangedEventArgs].
BluetoothLowEnergyStateChangedEventArgs(this.state);
}
/// The discovered event arguments.
class DiscoveredEventArgs extends EventArgs {
/// The disvered peripheral.
final Peripheral peripheral;
/// The rssi of the peripheral.
final int rssi;
/// The advertise data of the peripheral.
final AdvertiseData advertiseData;
/// Constructs a [DiscoveredEventArgs].
DiscoveredEventArgs(this.peripheral, this.rssi, this.advertiseData);
}
/// 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);
}
class ReadGattCharacteristicCommandEventArgs {
final Central central;
final GattCharacteristic characteristic;
final int id;
final int offset;
ReadGattCharacteristicCommandEventArgs(
this.central,
this.characteristic,
this.id,
this.offset,
);
}
class WriteGattCharacteristicCommandEventArgs {
final Central central;
final GattCharacteristic characteristic;
final int id;
final int offset;
final Uint8List value;
WriteGattCharacteristicCommandEventArgs(
this.central,
this.characteristic,
this.id,
this.offset,
this.value,
);
}
class NotifyGattCharacteristicCommandEventArgs {
final Central central;
final GattCharacteristic characteristic;
final bool state;
NotifyGattCharacteristicCommandEventArgs(
this.central,
this.characteristic,
this.state,
);
}

View File

@ -0,0 +1,41 @@
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');
}

View File

@ -0,0 +1,41 @@
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');
}

View File

@ -1,18 +1,17 @@
import 'dart:typed_data';
import 'advertise_data.dart';
import 'bluetooth_low_energy.dart';
import 'advertisement.dart';
import 'bluetooth_low_energy_manager.dart';
import 'central.dart';
import 'event_args.dart';
import 'gatt_characteristic.dart';
import 'gatt_service.dart';
import 'my_peripheral_manager.dart';
import 'peripheral_manager_event_args.dart';
/// An object that manages and advertises peripheral services exposed by this app.
abstract class PeripheralManager extends BluetoothLowEnergyManager {
/// Gets the instance of [PeripheralManager].
static PeripheralManager get instance =>
BluetoothLowEnergy.instance.peripheralManager;
/// The instance of [PeripheralManger] to use.
static PeripheralManager get instance => MyPeripheralManager.instance;
/// Tells that the local peripheral received an Attribute Protocol (ATT) read request for a characteristic with a dynamic value.
Stream<ReadGattCharacteristicCommandEventArgs>
@ -36,7 +35,7 @@ abstract class PeripheralManager extends BluetoothLowEnergyManager {
Future<void> clearServices();
/// Advertises peripheral manager data.
Future<void> startAdvertising(AdvertiseData advertiseData);
Future<void> startAdvertising(Advertisement advertisement);
/// Stops advertising peripheral manager data.
Future<void> stopAdvertising();
@ -47,27 +46,27 @@ abstract class PeripheralManager extends BluetoothLowEnergyManager {
/// Responds to a read request from a connected central.
Future<void> sendReadCharacteristicReply(
Central central,
GattCharacteristic characteristic,
int id,
int offset,
bool status,
Uint8List value,
);
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,
GattCharacteristic characteristic,
int id,
int offset,
bool status,
);
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,
GattCharacteristic characteristic,
Uint8List value,
);
Central central, {
required GattCharacteristic characteristic,
required Uint8List value,
});
}

View File

@ -0,0 +1,73 @@
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,
);
}

View File

@ -1,6 +1,6 @@
name: bluetooth_low_energy_platform_interface
description: A common platform interface for the bluetooth_low_energy plugin.
version: 3.0.0
version: 4.0.0
homepage: https://github.com/yanshouwang/bluetooth_low_energy
environment:
@ -11,8 +11,9 @@ dependencies:
flutter:
sdk: flutter
plugin_platform_interface: ^2.0.2
log_service: ^1.0.0
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
flutter_lints: ^3.0.0