4.0.0 (#24)
* 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:
@ -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';
|
||||
|
@ -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 {},
|
@ -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;
|
||||
}
|
@ -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);
|
||||
}
|
@ -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 manager’s state updated.
|
||||
Stream<BluetoothLowEnergyStateChangedEventArgs> get stateChanged;
|
||||
|
||||
/// Sets up this bluetooth low energy manager.
|
||||
/// Sets up the manager.
|
||||
Future<void> setUp();
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
@ -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';
|
||||
}
|
||||
}
|
@ -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,
|
||||
);
|
||||
}
|
||||
|
@ -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');
|
||||
}
|
@ -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');
|
||||
}
|
@ -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,
|
||||
});
|
||||
}
|
||||
|
@ -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,
|
||||
);
|
||||
}
|
Reference in New Issue
Block a user