diff --git a/android/build.gradle b/android/build.gradle index 54face2..32aa8e4 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -2,16 +2,16 @@ group 'com.tugberka.mdsflutter' version '1.0-SNAPSHOT' buildscript { - ext.kotlin_version = '1.3.50' + ext.kotlin_version = '1.5.31' repositories { google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.5.0' + classpath 'com.android.tools.build:gradle:7.0.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.10' + classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.15' } } @@ -49,26 +49,37 @@ protobuf { // Configure the protoc executable protoc { // Download from repositories - artifact = 'com.google.protobuf:protoc:3.9.1' + // artifact = 'com.google.protobuf:protoc:3.9.1' + artifact = 'com.google.protobuf:protoc:3.17.3' } - plugins { + /* plugins { javalite { // The codegen for lite comes as a separate artifact artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0' } - } - generateProtoTasks { + } */ + /* generateProtoTasks { all().each { task -> task.plugins { javalite { } } } + } */ + generateProtoTasks { + all().each { task -> + task.builtins { + java { + option "lite" + } + } + } } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - implementation 'com.google.protobuf:protobuf-lite:3.0.1' + //implementation 'com.google.protobuf:protobuf-lite:3.0.1' + implementation 'com.google.protobuf:protobuf-javalite:3.17.3' // RxAndroidBle helper library implementation "com.polidea.rxandroidble2:rxandroidble:1.11.0" implementation(name: 'mdslib', version: '+', ext: 'aar') diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties index 01a286e..297f2fe 100644 --- a/android/gradle/wrapper/gradle-wrapper.properties +++ b/android/gradle/wrapper/gradle-wrapper.properties @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip diff --git a/example/lib/AppModel.dart b/example/lib/AppModel.dart index 479af8a..f69ca9e 100644 --- a/example/lib/AppModel.dart +++ b/example/lib/AppModel.dart @@ -1,4 +1,3 @@ - import 'dart:collection'; import 'dart:convert'; @@ -11,13 +10,13 @@ import 'package:mdsflutter_example/DeviceConnectionStatus.dart'; import 'dart:developer' as developer; class AppModel extends ChangeNotifier { - - final Set _deviceList = Set(); + final Set _deviceList = Set(); bool _isScanning = false; - void Function(Device) _onDeviceMdsConnectedCb; - void Function(Device) _onDeviceDisonnectedCb; + void Function(Device)? _onDeviceMdsConnectedCb; + void Function(Device)? _onDeviceDisonnectedCb; - UnmodifiableListView get deviceList => UnmodifiableListView(_deviceList); + UnmodifiableListView get deviceList => + UnmodifiableListView(_deviceList); bool get isScanning => _isScanning; @@ -33,7 +32,8 @@ class AppModel extends ChangeNotifier { void startScan() { _deviceList.forEach((device) { - if (device.connectionStatus == DeviceConnectionStatus.CONNECTED) { + if (device != null && + device.connectionStatus == DeviceConnectionStatus.CONNECTED) { disconnectFromDevice(device); } }); @@ -43,7 +43,7 @@ class AppModel extends ChangeNotifier { try { Mds.startScan((name, address) { - Device device = Device(name, address); + Device device = Device(name ?? "", address ?? ""); if (!_deviceList.contains(device)) { _deviceList.add(device); notifyListeners(); @@ -65,36 +65,42 @@ class AppModel extends ChangeNotifier { void connectToDevice(Device device) { device.onConnecting(); - Mds.connect(device.address, - (serial) => _onDeviceMdsConnected(device.address, serial), - () => _onDeviceDisconnected(device.address), - () => _onDeviceConnectError(device.address) - ); + final address = device.address; + if (address == null) return; + Mds.connect( + address, + (serial) => _onDeviceMdsConnected(address, serial), + () => _onDeviceDisconnected(address), + () => _onDeviceConnectError(address)); } void disconnectFromDevice(Device device) { - Mds.disconnect(device.address); - _onDeviceDisconnected(device.address); + final address = device.address; + if (address == null) return; + Mds.disconnect(address); + _onDeviceDisconnected(address); } void _onDeviceMdsConnected(String address, String serial) { - Device foundDevice = _deviceList.firstWhere((element) => element.address == address); + Device? foundDevice = + _deviceList.firstWhere((element) => element?.address == address); if (foundDevice != null) { foundDevice.onMdsConnected(serial); notifyListeners(); if (_onDeviceMdsConnectedCb != null) { - _onDeviceMdsConnectedCb.call(foundDevice); + _onDeviceMdsConnectedCb?.call(foundDevice); } } } void _onDeviceDisconnected(String address) { - Device foundDevice = _deviceList.firstWhere((element) => element.address == address); + Device? foundDevice = + _deviceList.firstWhere((element) => element?.address == address); if (foundDevice != null) { foundDevice.onDisconnected(); notifyListeners(); if (_onDeviceDisonnectedCb != null) { - _onDeviceDisonnectedCb.call(foundDevice); + _onDeviceDisonnectedCb?.call(foundDevice); } } } @@ -102,4 +108,4 @@ class AppModel extends ChangeNotifier { void _onDeviceConnectError(String address) { _onDeviceDisconnected(address); } -} \ No newline at end of file +} diff --git a/example/lib/Device.dart b/example/lib/Device.dart index 73d5c5d..c1f2d38 100644 --- a/example/lib/Device.dart +++ b/example/lib/Device.dart @@ -1,21 +1,21 @@ - import 'package:mdsflutter_example/DeviceConnectionStatus.dart'; import 'package:mdsflutter_example/DeviceModel.dart'; class Device { - String _address; - String _name; - String _serial; - DeviceConnectionStatus _connectionStatus = DeviceConnectionStatus.NOT_CONNECTED; + String? _address; + String? _name; + String? _serial; + DeviceConnectionStatus _connectionStatus = + DeviceConnectionStatus.NOT_CONNECTED; Device(String name, String address) { _name = name; _address = address; } - String get name => _name != null ? _name : ""; - String get address => _address != null ? _address : ""; - String get serial => _serial != null ? _serial : ""; + String? get name => _name != null ? _name : ""; + String? get address => _address != null ? _address : ""; + String? get serial => _serial != null ? _serial : ""; DeviceConnectionStatus get connectionStatus => _connectionStatus; void onConnecting() => _connectionStatus = DeviceConnectionStatus.CONNECTING; @@ -23,8 +23,11 @@ class Device { _serial = serial; _connectionStatus = DeviceConnectionStatus.CONNECTED; } - void onDisconnected() => _connectionStatus = DeviceConnectionStatus.NOT_CONNECTED; - bool operator ==(o) => o is Device && o._address == _address && o._name == _name; + void onDisconnected() => + _connectionStatus = DeviceConnectionStatus.NOT_CONNECTED; + + bool operator ==(o) => + o is Device && o._address == _address && o._name == _name; int get hashCode => _address.hashCode * _name.hashCode; -} \ No newline at end of file +} diff --git a/example/lib/DeviceInteractionWidget.dart b/example/lib/DeviceInteractionWidget.dart index 71a8edb..95dff08 100644 --- a/example/lib/DeviceInteractionWidget.dart +++ b/example/lib/DeviceInteractionWidget.dart @@ -18,13 +18,13 @@ class DeviceInteractionWidget extends StatefulWidget { } class _DeviceInteractionWidgetState extends State { - AppModel _appModel; + AppModel? _appModel; @override void initState() { super.initState(); _appModel = Provider.of(context, listen: false); - _appModel.onDeviceMdsDisconnected((device) => { Navigator.pop(context) }); + _appModel?.onDeviceMdsDisconnected((device) => {Navigator.pop(context)}); } void _onAccelerometerButtonPressed(DeviceModel deviceModel) { @@ -45,7 +45,7 @@ class _DeviceInteractionWidgetState extends State { @override void dispose() { - _appModel.disconnectFromDevice(widget.device); + _appModel?.disconnectFromDevice(widget.device); super.dispose(); } @@ -53,12 +53,12 @@ class _DeviceInteractionWidgetState extends State { Widget build(BuildContext context) { Device device = widget.device; return ChangeNotifierProvider( - create: (context) => DeviceModel(device.name, device.serial), + create: (context) => DeviceModel(device.name ?? "", device.serial ?? ""), child: Consumer( builder: (context, model, child) { return Scaffold( appBar: AppBar( - title: Text(device.name), + title: Text(device.name ?? ""), ), body: Column( mainAxisSize: MainAxisSize.min, @@ -68,8 +68,7 @@ class _DeviceInteractionWidgetState extends State { _ledItem(model), _temperatureItem(model) ], - ) - ); + )); }, ), ); @@ -80,8 +79,10 @@ class _DeviceInteractionWidgetState extends State { child: ListTile( title: Text("Accelerometer"), subtitle: Text(deviceModel.accelerometerData), - trailing: RaisedButton( - child: Text(deviceModel.accelerometerSubscribed ? "Unsubscribe" : "Subscribe"), + trailing: ElevatedButton( + child: Text(deviceModel.accelerometerSubscribed + ? "Unsubscribe" + : "Subscribe"), onPressed: () => _onAccelerometerButtonPressed(deviceModel), ), ), @@ -93,7 +94,7 @@ class _DeviceInteractionWidgetState extends State { child: ListTile( title: Text("Heart rate"), subtitle: Text(deviceModel.hrData), - trailing: RaisedButton( + trailing: ElevatedButton( child: Text(deviceModel.hrSubscribed ? "Unsubscribe" : "Subscribe"), onPressed: () => _onHrButtonPressed(deviceModel), ), @@ -107,7 +108,7 @@ class _DeviceInteractionWidgetState extends State { title: Text("Led"), trailing: Switch( value: deviceModel.ledStatus, - onChanged: (b) => { deviceModel.switchLed() }, + onChanged: (b) => {deviceModel.switchLed()}, ), ), ); @@ -118,11 +119,11 @@ class _DeviceInteractionWidgetState extends State { child: ListTile( title: Text("Temperature"), subtitle: Text(deviceModel.temperature), - trailing: RaisedButton( + trailing: ElevatedButton( child: Text("Get"), onPressed: () => deviceModel.getTemperature(), ), ), ); } -} \ No newline at end of file +} diff --git a/example/lib/DeviceModel.dart b/example/lib/DeviceModel.dart index 9589df1..d96392a 100644 --- a/example/lib/DeviceModel.dart +++ b/example/lib/DeviceModel.dart @@ -12,12 +12,12 @@ class DeviceModel extends ChangeNotifier { String get name => _name; String get serial => _serial; - int _accSubscription; + int? _accSubscription; String _accelerometerData = ""; String get accelerometerData => _accelerometerData; bool get accelerometerSubscribed => _accSubscription != null; - int _hrSubscription; + int? _hrSubscription; String _hrData = ""; String get hrData => _hrData; bool get hrSubscribed => _hrSubscription != null; @@ -38,8 +38,7 @@ class DeviceModel extends ChangeNotifier { (d, c) => {}, (e, c) => {}, (data) => _onNewAccelerometerData(data), - (e, c) => {} - ); + (e, c) => {}); notifyListeners(); } @@ -48,12 +47,18 @@ class DeviceModel extends ChangeNotifier { Map body = accData["Body"]; List accArray = body["ArrayAcc"]; dynamic acc = accArray.last; - _accelerometerData = "x: " + acc["x"].toStringAsFixed(2) + "\ny: " + acc["y"].toStringAsFixed(2) + "\nz: " + acc["z"].toStringAsFixed(2); + _accelerometerData = "x: " + + acc["x"].toStringAsFixed(2) + + "\ny: " + + acc["y"].toStringAsFixed(2) + + "\nz: " + + acc["z"].toStringAsFixed(2); notifyListeners(); } void unsubscribeFromAccelerometer() { - Mds.unsubscribe(_accSubscription); + if (_accSubscription == null) return; + Mds.unsubscribe(_accSubscription!); _accSubscription = null; notifyListeners(); } @@ -61,13 +66,12 @@ class DeviceModel extends ChangeNotifier { void subscribeToHr() { _hrData = ""; _hrSubscription = Mds.subscribe( - Mds.createSubscriptionUri(_serial, "/Meas/HR"), - "{}", - (d, c) => {}, - (e, c) => {}, - (data) => _onNewHrData(data), - (e, c) => {} - ); + Mds.createSubscriptionUri(_serial, "/Meas/HR"), + "{}", + (d, c) => {}, + (e, c) => {}, + (data) => _onNewHrData(data), + (e, c) => {}); notifyListeners(); } @@ -80,7 +84,8 @@ class DeviceModel extends ChangeNotifier { } void unsubscribeFromHr() { - Mds.unsubscribe(_hrSubscription); + if (_hrSubscription == null) return; + Mds.unsubscribe(_hrSubscription!); _hrSubscription = null; notifyListeners(); } @@ -89,27 +94,19 @@ class DeviceModel extends ChangeNotifier { Map contract = new Map(); contract["isOn"] = !_ledStatus; Mds.put( - Mds.createRequestUri(_serial, "/Component/Led"), - jsonEncode(contract), - (data, code) { - _ledStatus = !_ledStatus; - notifyListeners(); - }, - (e, c) => {} - ); + Mds.createRequestUri(_serial, "/Component/Led"), jsonEncode(contract), + (data, code) { + _ledStatus = !_ledStatus; + notifyListeners(); + }, (e, c) => {}); } void getTemperature() { - Mds.get( - Mds.createRequestUri(_serial, "/Meas/Temp"), - "{}", - (data, code) { - double kelvin = jsonDecode(data)["Content"]["Measurement"]; - double temperatureVal = kelvin - 274.15; - _temperature = temperatureVal.toStringAsFixed(1) + " C"; - notifyListeners(); - }, - (e, c) => {} - ); + Mds.get(Mds.createRequestUri(_serial, "/Meas/Temp"), "{}", (data, code) { + double kelvin = jsonDecode(data)["Content"]["Measurement"]; + double temperatureVal = kelvin - 274.15; + _temperature = temperatureVal.toStringAsFixed(1) + " C"; + notifyListeners(); + }, (e, c) => {}); } -} \ No newline at end of file +} diff --git a/example/lib/ScanWidget.dart b/example/lib/ScanWidget.dart index 272d06e..07cd218 100644 --- a/example/lib/ScanWidget.dart +++ b/example/lib/ScanWidget.dart @@ -14,59 +14,61 @@ class ScanWidget extends StatefulWidget { } class _ScanWidgetState extends State { - - AppModel model; + AppModel? model; @override void initState() { super.initState(); initPlatformState(); model = Provider.of(context, listen: false); - model.onDeviceMdsConnected((device) => { - Navigator.push(context, MaterialPageRoute(builder: (context) => - DeviceInteractionWidget(device) - )) - }); + model?.onDeviceMdsConnected((device) => { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => DeviceInteractionWidget(device))) + }); } Future initPlatformState() async { if (!mounted) return; if (defaultTargetPlatform == TargetPlatform.android) { - Permission.locationWhenInUse.isUndetermined.then((value) => - Permission.locationWhenInUse.request() - ); + Permission.locationWhenInUse.isDenied + .then((value) => Permission.locationWhenInUse.request()); - Permission.locationWhenInUse.isDenied.then((value) => - Permission.locationWhenInUse.request() - ); + Permission.locationWhenInUse.isDenied + .then((value) => Permission.locationWhenInUse.request()); } } Widget _buildDeviceItem(BuildContext context, int index) { return Card( child: ListTile( - title: Text(model.deviceList[index].name), - subtitle: Text(model.deviceList[index].address), - trailing: Text(model.deviceList[index].connectionStatus.statusName), - onTap: () => model.connectToDevice(model.deviceList[index]), + title: Text(model?.deviceList[index]?.name ?? ""), + subtitle: Text(model?.deviceList[index]?.address ?? ""), + trailing: + Text(model?.deviceList[index]?.connectionStatus.statusName ?? ""), + onTap: () { + final device = model?.deviceList[index]; + if (device != null) model?.connectToDevice(device); + }, ), ); } - Widget _buildDeviceList(List deviceList) { - return new Expanded(child: new ListView.builder( - itemCount: model.deviceList.length, - itemBuilder: (BuildContext context, int index) => _buildDeviceItem(context, index) - ) - ); + Widget _buildDeviceList(List deviceList) { + return new Expanded( + child: new ListView.builder( + itemCount: model?.deviceList.length, + itemBuilder: (BuildContext context, int index) => + _buildDeviceItem(context, index))); } void onScanButtonPressed() { - if (model.isScanning) { - model.stopScan(); + if (model?.isScanning ?? false) { + model?.stopScan(); } else { - model.startScan(); + model?.startScan(); } } @@ -81,7 +83,7 @@ class _ScanWidgetState extends State { return Column( mainAxisSize: MainAxisSize.min, children: [ - RaisedButton( + ElevatedButton( onPressed: onScanButtonPressed, child: Text(model.scanButtonText), ), @@ -89,7 +91,6 @@ class _ScanWidgetState extends State { ], ); }, - ) - ); + )); } } diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 165ec81..8abcb40 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -6,27 +6,22 @@ description: Demonstrates how to use the mdsflutter plugin. publish_to: 'none' # Remove this line if you wish to publish to pub.dev environment: - sdk: ">=2.7.0 <3.0.0" + sdk: ">=2.12.0 <3.0.0" dependencies: flutter: sdk: flutter - mdsflutter: - # When depending on this package from a real application you should use: - # mdsflutter: ^x.y.z - # See https://dart.dev/tools/pub/dependencies#version-constraints - # The example app is bundled with the plugin so we use a path dependency on - # the parent directory to use the current plugin's version. - path: ../ + mdsflutter: + path: ../../mdsflutter # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. - cupertino_icons: ^0.1.3 + cupertino_icons: ^1.0.3 - provider: ^3.0.0 + provider: ^6.0.1 - permission_handler: ^5.0.1 + permission_handler: ^8.2.2 dev_dependencies: flutter_test: diff --git a/lib/Mds.dart b/lib/Mds.dart index 1aa186e..d27f32b 100644 --- a/lib/Mds.dart +++ b/lib/Mds.dart @@ -9,7 +9,7 @@ class Mds { /// devices will get UUID as the address parameter. Scanning is terminated /// automatically after 60 seconds. Only devices with Movesense services are /// returned. - static void startScan(void Function(String, String) onNewDeviceFound) { + static void startScan(void Function(String?, String?) onNewDeviceFound) { MdsImpl().startScan(onNewDeviceFound); } @@ -27,10 +27,8 @@ class Mds { /// Note: If you need DeviceInfo upon connection, you should manually /// subscribe to "MDS/ConnectedDevices" to get detailed device information /// upon connection. - static void connect(String address, - void Function(String) onConnected, - void Function() onDisconnected, - void Function() onConnectionError) { + static void connect(String address, void Function(String) onConnected, + void Function() onDisconnected, void Function() onConnectionError) { MdsImpl().connect(address, onConnected, onDisconnected, onConnectionError); } @@ -44,7 +42,8 @@ class Mds { /// request is successful, onSuccess is called with response data in json /// string format, and status code. Upon error, onError is called with reason /// and status code. - static void get(String uri, + static void get( + String uri, String contract, void Function(String, int) onSuccess, void Function(String, int) onError) { @@ -56,7 +55,8 @@ class Mds { /// request is successful, onSuccess is called with response data in json /// string format, and status code. Upon error, onError is called with reason /// and status code. - static void put(String uri, + static void put( + String uri, String contract, void Function(String, int) onSuccess, void Function(String, int) onError) { @@ -68,7 +68,8 @@ class Mds { /// request is successful, onSuccess is called with response data in json /// string format, and status code. Upon error, onError is called with reason /// and status code. - static void post(String uri, + static void post( + String uri, String contract, void Function(String, int) onSuccess, void Function(String, int) onError) { @@ -80,7 +81,8 @@ class Mds { /// request is successful, onSuccess is called with response data in json /// string format, and status code. Upon error, onError is called with reason /// and status code. - static void del(String uri, + static void del( + String uri, String contract, void Function(String, int) onSuccess, void Function(String, int) onError) { @@ -98,7 +100,8 @@ class Mds { /// /// This call returns a subscription id. It must be held and used when /// unsubscribing. - static int subscribe(String uri, + static int subscribe( + String uri, String contract, void Function(String, int) onSuccess, void Function(String, int) onError, @@ -118,6 +121,6 @@ class Mds { } static String createSubscriptionUri(String serial, String resource) { - return serial + resource; + return serial + resource; } } diff --git a/lib/gen/protos/mdsflutter.pb.dart b/lib/gen/protos/mdsflutter.pb.dart index f858f39..e70df27 100644 --- a/lib/gen/protos/mdsflutter.pb.dart +++ b/lib/gen/protos/mdsflutter.pb.dart @@ -2,7 +2,7 @@ // Generated code. Do not modify. // source: protos/mdsflutter.proto // -// @dart = 2.3 + // ignore_for_file: annotate_overrides,camel_case_types,unnecessary_const,non_constant_identifier_names,library_prefixes,unused_import,unused_shown_name,return_of_invalid_type import 'dart:core' as $core; @@ -22,7 +22,7 @@ class RequestResult extends $pb.GeneratedMessage { factory RequestResult.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r); factory RequestResult.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r); RequestResult clone() => RequestResult()..mergeFromMessage(this); - RequestResult copyWith(void Function(RequestResult) updates) => super.copyWith((message) => updates(message as RequestResult)); + RequestResult copyWith(void Function(RequestResult) updates) => super.copyWith((message) => updates(message as RequestResult)) as RequestResult; $pb.BuilderInfo get info_ => _i; @$core.pragma('dart2js:noInline') static RequestResult create() => RequestResult._(); @@ -30,7 +30,7 @@ class RequestResult extends $pb.GeneratedMessage { static $pb.PbList createRepeated() => $pb.PbList(); @$core.pragma('dart2js:noInline') static RequestResult getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor(create); - static RequestResult _defaultInstance; + static RequestResult? _defaultInstance; @$pb.TagNumber(1) $core.int get requestId => $_getIZ(0); @@ -73,7 +73,7 @@ class RequestError extends $pb.GeneratedMessage { factory RequestError.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r); factory RequestError.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r); RequestError clone() => RequestError()..mergeFromMessage(this); - RequestError copyWith(void Function(RequestError) updates) => super.copyWith((message) => updates(message as RequestError)); + RequestError copyWith(void Function(RequestError) updates) => super.copyWith((message) => updates(message as RequestError)) as RequestError; $pb.BuilderInfo get info_ => _i; @$core.pragma('dart2js:noInline') static RequestError create() => RequestError._(); @@ -81,7 +81,7 @@ class RequestError extends $pb.GeneratedMessage { static $pb.PbList createRepeated() => $pb.PbList(); @$core.pragma('dart2js:noInline') static RequestError getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor(create); - static RequestError _defaultInstance; + static RequestError? _defaultInstance; @$pb.TagNumber(1) $core.int get requestId => $_getIZ(0); @@ -123,7 +123,7 @@ class Notification extends $pb.GeneratedMessage { factory Notification.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r); factory Notification.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r); Notification clone() => Notification()..mergeFromMessage(this); - Notification copyWith(void Function(Notification) updates) => super.copyWith((message) => updates(message as Notification)); + Notification copyWith(void Function(Notification) updates) => super.copyWith((message) => updates(message as Notification)) as Notification; $pb.BuilderInfo get info_ => _i; @$core.pragma('dart2js:noInline') static Notification create() => Notification._(); @@ -131,7 +131,7 @@ class Notification extends $pb.GeneratedMessage { static $pb.PbList createRepeated() => $pb.PbList(); @$core.pragma('dart2js:noInline') static Notification getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor(create); - static Notification _defaultInstance; + static Notification? _defaultInstance; @$pb.TagNumber(1) $core.int get subscriptionId => $_getIZ(0); @@ -165,7 +165,7 @@ class NotificationError extends $pb.GeneratedMessage { factory NotificationError.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r); factory NotificationError.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r); NotificationError clone() => NotificationError()..mergeFromMessage(this); - NotificationError copyWith(void Function(NotificationError) updates) => super.copyWith((message) => updates(message as NotificationError)); + NotificationError copyWith(void Function(NotificationError) updates) => super.copyWith((message) => updates(message as NotificationError)) as NotificationError; $pb.BuilderInfo get info_ => _i; @$core.pragma('dart2js:noInline') static NotificationError create() => NotificationError._(); @@ -173,7 +173,7 @@ class NotificationError extends $pb.GeneratedMessage { static $pb.PbList createRepeated() => $pb.PbList(); @$core.pragma('dart2js:noInline') static NotificationError getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor(create); - static NotificationError _defaultInstance; + static NotificationError? _defaultInstance; @$pb.TagNumber(1) $core.int get subscriptionId => $_getIZ(0); diff --git a/lib/gen/protos/mdsflutter.pbenum.dart b/lib/gen/protos/mdsflutter.pbenum.dart index 4447420..f00db36 100644 --- a/lib/gen/protos/mdsflutter.pbenum.dart +++ b/lib/gen/protos/mdsflutter.pbenum.dart @@ -2,6 +2,6 @@ // Generated code. Do not modify. // source: protos/mdsflutter.proto // -// @dart = 2.3 + // ignore_for_file: annotate_overrides,camel_case_types,unnecessary_const,non_constant_identifier_names,library_prefixes,unused_import,unused_shown_name,return_of_invalid_type diff --git a/lib/gen/protos/mdsflutter.pbjson.dart b/lib/gen/protos/mdsflutter.pbjson.dart index 8bdb05a..62ae6dd 100644 --- a/lib/gen/protos/mdsflutter.pbjson.dart +++ b/lib/gen/protos/mdsflutter.pbjson.dart @@ -2,7 +2,7 @@ // Generated code. Do not modify. // source: protos/mdsflutter.proto // -// @dart = 2.3 + // ignore_for_file: annotate_overrides,camel_case_types,unnecessary_const,non_constant_identifier_names,library_prefixes,unused_import,unused_shown_name,return_of_invalid_type const RequestResult$json = const { diff --git a/lib/gen/protos/mdsflutter.pbserver.dart b/lib/gen/protos/mdsflutter.pbserver.dart index 47aa4da..73469b4 100644 --- a/lib/gen/protos/mdsflutter.pbserver.dart +++ b/lib/gen/protos/mdsflutter.pbserver.dart @@ -2,7 +2,7 @@ // Generated code. Do not modify. // source: protos/mdsflutter.proto // -// @dart = 2.3 + // ignore_for_file: annotate_overrides,camel_case_types,unnecessary_const,non_constant_identifier_names,library_prefixes,unused_import,unused_shown_name,return_of_invalid_type export 'mdsflutter.pb.dart'; diff --git a/lib/internal/MdsImpl.dart b/lib/internal/MdsImpl.dart index e4a1fb0..dce3c1a 100644 --- a/lib/internal/MdsImpl.dart +++ b/lib/internal/MdsImpl.dart @@ -1,4 +1,3 @@ - import 'dart:async'; import 'dart:convert'; import 'dart:typed_data'; @@ -19,10 +18,10 @@ class MdsImpl { Map _requestErrorCbMap = Map(); Map _notifyCbMap = Map(); Map _subscriptionErrorCbMap = Map(); - Map _serialToAddressMap = Map(); - void Function(String, String) _newScannedDeviceCb; + Map _serialToAddressMap = Map(); + void Function(String?, String?)? _newScannedDeviceCb; - void startScan(void Function(String, String) onNewDeviceFound) { + void startScan(void Function(String?, String?) onNewDeviceFound) { _newScannedDeviceCb = onNewDeviceFound; _channel.invokeMethod('startScan', null); } @@ -32,11 +31,8 @@ class MdsImpl { _channel.invokeMethod('stopScan', null); } - void connect( - String address, - void Function(String) onConnected, - void Function() onDisconnected, - void Function() onConnectionError) { + void connect(String address, void Function(String) onConnected, + void Function() onDisconnected, void Function() onConnectionError) { _channel.invokeMethod('connect', {"address": address}); _connectCbMap[address] = onConnected; _disconnectCbMap[address] = onDisconnected; @@ -47,60 +43,48 @@ class MdsImpl { _channel.invokeMethod('disconnect', {"address": address}); } - void get( - String uri, - String contract, - void Function(String, int) onSuccess, + void get(String uri, String contract, void Function(String, int) onSuccess, void Function(String, int) onError) { _idCounter++; _requestResultCbMap[_idCounter] = onSuccess; _requestErrorCbMap[_idCounter] = onError; - _channel.invokeMethod('get', { + _channel.invokeMethod('get', { "uri": uri, "contract": contract, "requestId": _idCounter }); } - void put( - String uri, - String contract, - void Function(String, int) onSuccess, + void put(String uri, String contract, void Function(String, int) onSuccess, void Function(String, int) onError) { _idCounter++; _requestResultCbMap[_idCounter] = onSuccess; _requestErrorCbMap[_idCounter] = onError; - _channel.invokeMethod('put', { + _channel.invokeMethod('put', { "uri": uri, "contract": contract, "requestId": _idCounter }); } - void post( - String uri, - String contract, - void Function(String, int) onSuccess, + void post(String uri, String contract, void Function(String, int) onSuccess, void Function(String, int) onError) { _idCounter++; _requestResultCbMap[_idCounter] = onSuccess; _requestErrorCbMap[_idCounter] = onError; - _channel.invokeMethod('post', { + _channel.invokeMethod('post', { "uri": uri, "contract": contract, "requestId": _idCounter }); } - void del( - String uri, - String contract, - void Function(String, int) onSuccess, + void del(String uri, String contract, void Function(String, int) onSuccess, void Function(String, int) onError) { _idCounter++; _requestResultCbMap[_idCounter] = onSuccess; _requestErrorCbMap[_idCounter] = onError; - _channel.invokeMethod('del', { + _channel.invokeMethod('del', { "uri": uri, "contract": contract, "requestId": _idCounter @@ -131,7 +115,7 @@ class MdsImpl { mdsContract = json.encode(contractMap); } - _channel.invokeMethod('subscribe', { + _channel.invokeMethod('subscribe', { "uri": mdsUri, "contract": mdsContract, "requestId": _idCounter, @@ -163,38 +147,31 @@ class MdsImpl { } void _doConnectSubscription() { - subscribe( - "MDS/ConnectedDevices", - "{}", - (d, c) => {}, - (e, c) => {}, - (data) { - final decoded = jsonDecode(data); - final method = decoded["Method"]; - if (method == "POST") { - if (decoded.containsKey("Body")) { - final body = decoded["Body"]; - final deviceInfo = body["DeviceInfo"]; - final connection = body["Connection"]; - final uuid = connection["UUID"] as String; - final serial = deviceInfo["serial"] as String; - _serialToAddressMap[serial] = uuid; - _onConnect(uuid, serial); - } - } else if (method == "DEL") { - final body = decoded["Body"]; - final serial = body["Serial"] as String; - if (_serialToAddressMap.containsKey(serial)) { - _onDisconnect(_serialToAddressMap[serial]); - } - } - }, - (e, c) => {} - ); + subscribe("MDS/ConnectedDevices", "{}", (d, c) => {}, (e, c) => {}, (data) { + final decoded = jsonDecode(data); + final method = decoded["Method"]; + if (method == "POST") { + if (decoded.containsKey("Body")) { + final body = decoded["Body"]; + final deviceInfo = body["DeviceInfo"]; + final connection = body["Connection"]; + final uuid = connection["UUID"] as String?; + final serial = deviceInfo["serial"] as String?; + _serialToAddressMap[serial] = uuid; + _onConnect(uuid, serial); + } + } else if (method == "DEL") { + final body = decoded["Body"]; + final serial = body["Serial"] as String?; + if (_serialToAddressMap.containsKey(serial)) { + _onDisconnect(_serialToAddressMap[serial]); + } + } + }, (e, c) => {}); } Future _onNativeMethodCall(MethodCall call) async { - switch(call.method) { + switch (call.method) { case "onNewScannedDevice": Map args = call.arguments; _onNewScannedDevice(args["name"], args["address"]); @@ -203,24 +180,26 @@ class MdsImpl { Map args = call.arguments; _onConnect(args["address"], args["serial"]); break; - break; + case "onDisconnect": - String address = call.arguments; + String? address = call.arguments; _onDisconnect(address); break; case "onConnectionError": - String address = call.arguments; + String? address = call.arguments; _onConnectionError(address); break; case "onRequestResult": final Uint8List proto = call.arguments; final RequestResult requestResult = RequestResult.fromBuffer(proto); - _onRequestResult(requestResult.requestId, requestResult.data, requestResult.statusCode); + _onRequestResult(requestResult.requestId, requestResult.data, + requestResult.statusCode); break; case "onRequestError": final Uint8List proto = call.arguments; final RequestError requestError = RequestError.fromBuffer(proto); - _onRequestError(requestError.requestId, requestError.error, requestError.statusCode); + _onRequestError(requestError.requestId, requestError.error, + requestError.statusCode); break; case "onNotification": final Uint8List proto = call.arguments; @@ -230,28 +209,29 @@ class MdsImpl { case "onNotificationError": final Uint8List proto = call.arguments; final NotificationError error = NotificationError.fromBuffer(proto); - _onSubscriptionError(error.subscriptionId, error.error, error.statusCode); + _onSubscriptionError( + error.subscriptionId, error.error, error.statusCode); break; } } - void _onNewScannedDevice(String name, String address) { + void _onNewScannedDevice(String? name, String? address) { if (_newScannedDeviceCb != null) { - _newScannedDeviceCb(name, address); + _newScannedDeviceCb!(name, address); } } - void _onConnect(String address, String serial) { + void _onConnect(String? address, String? serial) { if (_connectCbMap.containsKey(address)) { - developer.log("New connected device with serial: " + serial); + developer.log("New connected device with serial: " + serial!); void Function(String) cb = _connectCbMap[address]; cb(serial); } } - void _onDisconnect(String address) { + void _onDisconnect(String? address) { if (_disconnectCbMap.containsKey(address)) { - developer.log("Device disconnected, address: " + address); + developer.log("Device disconnected, address: " + address!); void Function() cb = _disconnectCbMap[address]; cb(); _connectCbMap.remove(address); @@ -260,9 +240,9 @@ class MdsImpl { } } - void _onConnectionError(String address) { + void _onConnectionError(String? address) { if (_connectErrorCbMap.containsKey(address)) { - developer.log("Device connection error, address: " + address); + developer.log("Device connection error, address: " + address!); void Function() cb = _connectErrorCbMap[address]; cb(); _connectCbMap.remove(address); diff --git a/pubspec.yaml b/pubspec.yaml index 375587b..0bddec0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,17 +1,16 @@ name: mdsflutter description: Flutter plugin for MDS (Movesense Device Service) that is used for communicating with Movesense devices. -version: 1.0.0 -author: Tugberk Akdogan +version: 1.1.0 homepage: https://github.com/tugberka/mdsflutter environment: - sdk: ">=2.7.0 <3.0.0" - flutter: ">=1.10.0" + sdk: '>=2.12.0 <3.0.0' + flutter: ">=2.0.0" dependencies: flutter: sdk: flutter - protobuf: any + protobuf: ^2.0.0 dev_dependencies: flutter_test: