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

Update json scan_result parsing #584

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
29 changes: 14 additions & 15 deletions lib/scan_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,25 @@ class ScanResult {

/// A packet of data advertised by the peripheral.
final AdvertisementData advertisementData;

ScanResult._(
this.peripheral,
this.peripheral,
this.rssi,
this.advertisementData,
{this.isConnectable,
List<String>? overflowServiceUuids,
this.advertisementData, {
this.isConnectable,
List<String>? overflowServiceUuids,
}) : overflowServiceUuids = overflowServiceUuids ?? <String>[];


factory ScanResult.fromJson(
Map<String, dynamic?> json,
ManagerForPeripheral manager
) {
Map<String, dynamic?> json, ManagerForPeripheral manager) {
assert(json[_ScanResultMetadata.rssi] is int);
return ScanResult._(
Peripheral.fromJson(json, manager),
Peripheral.fromJson(json, manager),
json[_ScanResultMetadata.rssi],
AdvertisementData._fromJson(json),
isConnectable: json[_ScanResultMetadata.isConnectable],
overflowServiceUuids: json[_ScanResultMetadata.overflowServiceUuids]
overflowServiceUuids: _mapToListOfStringsOrNull(
json[_ScanResultMetadata.overflowServiceUuids]),
);
}
}
Expand Down Expand Up @@ -83,10 +81,8 @@ class AdvertisementData {
_mapToListOfStringsOrNull(json[_ScanResultMetadata.serviceUuids]),
localName = json[_ScanResultMetadata.localName],
txPowerLevel = json[_ScanResultMetadata.txPowerLevel],
solicitedServiceUuids =
_mapToListOfStringsOrNull(
json[_ScanResultMetadata.solicitedServiceUuids]
);
solicitedServiceUuids = _mapToListOfStringsOrNull(
json[_ScanResultMetadata.solicitedServiceUuids]);

static Map<String, Uint8List>? _getServiceDataOrNull(
Map<String, dynamic>? serviceData) {
Expand All @@ -106,3 +102,6 @@ class AdvertisementData {
static List<String>? _mapToListOfStringsOrNull(List<dynamic>? values) =>
values?.cast<String>();
}

List<String> _mapToListOfStringsOrNull(List<dynamic>? values) =>
(values ?? []).cast();