Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Support multiple service/characteristic instances with same UUID #1042

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

455 changes: 200 additions & 255 deletions ios/Classes/FlutterBluePlusPlugin.m

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions lib/flutter_blue_plus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
library flutter_blue_plus;

import 'dart:async';
import 'dart:io';
import 'dart:convert';
import 'dart:io';

import 'package:flutter/services.dart';

part 'src/bluetooth_attribute.dart';
part 'src/bluetooth_characteristic.dart';
part 'src/bluetooth_descriptor.dart';
part 'src/bluetooth_device.dart';
part 'src/bluetooth_msgs.dart';
part 'src/bluetooth_events.dart';
part 'src/bluetooth_msgs.dart';
part 'src/bluetooth_service.dart';
part 'src/bluetooth_utils.dart';
part 'src/flutter_blue_plus.dart';
Expand Down
77 changes: 77 additions & 0 deletions lib/src/bluetooth_attribute.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2017-2023, Charles Weinberger & Paul DeMarco.
// All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of flutter_blue_plus;

abstract class BluetoothAttribute {
final BluetoothDevice device;
final Guid uuid;
final int? index;

BluetoothAttribute({
required this.device,
required this.uuid,
this.index,
});

DeviceIdentifier get remoteId => device.remoteId;

@Deprecated('Use remoteId instead')
DeviceIdentifier get deviceId => remoteId;

BluetoothAttribute? get _parentAttribute => null;
Copy link
Owner

@chipweinberger chipweinberger Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this variable should only exist for services. Not chrs & descs. It's just confusing otherwise.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was mainly for identifier path generation, so will be removed if we are using a map instead.


String get identifier => "$uuid:$index";

String get identifierPath =>
_parentAttribute != null ? "${_parentAttribute!.identifierPath}/$identifier" : identifier;
}

abstract class BluetoothValueAttribute extends BluetoothAttribute {
List<int> _lastValue = [];

BluetoothValueAttribute({
required BluetoothDevice device,
required Guid uuid,
int? index,
}) : super(device: device, uuid: uuid, index: index);

/// this variable is updated:
/// - anytime `read()` is called
/// - anytime `write()` is called
/// - anytime a notification arrives (characteristics, if subscribed)
/// - when the device is disconnected it is cleared
List<int> get lastValue => _lastValue;

/// this stream emits values:
/// - anytime `read()` is called
/// - anytime `write()` is called
/// - anytime a notification arrives (characteristics, if subscribed)
/// - and when first listened to, it re-emits the last value for convenience
Stream<List<int>> get lastValueStream => FlutterBluePlus._methodStream.stream
.where((e) =>
e is OnCharacteristicReceivedEvent ||
e is OnCharacteristicWrittenEvent ||
e is OnDescriptorReadEvent ||
e is OnDescriptorWrittenEvent)
.map((e) => e as GetAttributeValueMixin)
.where((e) => e.attribute == this)
.map((e) => e.value)
.newStreamWithInitialValue(lastValue);

/// this stream emits values:
/// - anytime `read()` is called
/// - anytime a notification arrives (if subscribed)
Stream<List<int>> get onValueReceived => FlutterBluePlus._methodStream.stream
.where((e) => e is OnCharacteristicReceivedEvent || e is OnDescriptorReadEvent)
.map((e) => e as GetAttributeValueMixin)
.where((e) => e.attribute == this)
.map((e) => e.value);

@Deprecated('Use onValueReceived instead')
Stream<List<int>> get value => onValueReceived;

@Deprecated('Use onValueReceived instead')
Stream<List<int>> get onValueChangedStream => onValueReceived;
}
Loading