diff --git a/example/android/build.gradle b/example/android/build.gradle index dc5cdbc..c505a86 100644 --- a/example/android/build.gradle +++ b/example/android/build.gradle @@ -6,7 +6,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:4.0.1' + classpath 'com.android.tools.build:gradle:4.1.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } diff --git a/example/android/gradle/wrapper/gradle-wrapper.properties b/example/android/gradle/wrapper/gradle-wrapper.properties index 72a6e93..3a1d81b 100644 --- a/example/android/gradle/wrapper/gradle-wrapper.properties +++ b/example/android/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip diff --git a/example/lib/main.dart b/example/lib/main.dart index 132a8e3..3532955 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,11 +1,10 @@ import 'dart:async'; import 'dart:convert'; import 'dart:io'; - import 'package:device_info/device_info.dart'; +import 'package:flutter_styled_toast/flutter_styled_toast.dart'; import 'package:flutter/material.dart'; import 'package:flutter_nearby_connections/flutter_nearby_connections.dart'; -import 'package:fluttertoast/fluttertoast.dart'; void main() { runApp(MyApp()); @@ -85,7 +84,7 @@ class Home extends StatelessWidget { enum DeviceType { advertiser, browser } class DevicesListScreen extends StatefulWidget { - const DevicesListScreen({this.deviceType}); + const DevicesListScreen({required this.deviceType}); final DeviceType deviceType; @@ -96,9 +95,9 @@ class DevicesListScreen extends StatefulWidget { class _DevicesListScreenState extends State { List devices = []; List connectedDevices = []; - NearbyService nearbyService; - StreamSubscription subscription; - StreamSubscription receivedDataSubscription; + late NearbyService nearbyService; + late StreamSubscription subscription; + late StreamSubscription receivedDataSubscription; bool isInit = false; @@ -110,8 +109,8 @@ class _DevicesListScreenState extends State { @override void dispose() { - subscription?.cancel(); - receivedDataSubscription?.cancel(); + subscription.cancel(); + receivedDataSubscription.cancel(); nearbyService.stopBrowsingForPeers(); nearbyService.stopAdvertisingPeer(); super.dispose(); @@ -237,13 +236,13 @@ class _DevicesListScreenState extends State { title: Text("Send message"), content: TextField(controller: myController), actions: [ - FlatButton( + TextButton( child: Text("Cancel"), onPressed: () { Navigator.of(context).pop(); }, ), - FlatButton( + TextButton( child: Text("Send"), onPressed: () { nearbyService.sendMessage( @@ -283,8 +282,19 @@ class _DevicesListScreenState extends State { void init() async { nearbyService = NearbyService(); + String devInfo = ''; + DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); + if (Platform.isAndroid) { + AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo; + devInfo = androidInfo.model; + } + if (Platform.isIOS) { + IosDeviceInfo iosInfo = await deviceInfo.iosInfo; + devInfo = iosInfo.localizedModel; + } await nearbyService.init( - serviceType: 'mp-connection', + serviceType: 'mpconn', + deviceName: devInfo, strategy: Strategy.P2P_CLUSTER, callback: (isRunning) async { if (isRunning) { @@ -302,7 +312,7 @@ class _DevicesListScreenState extends State { }); subscription = nearbyService.stateChangedSubscription(callback: (devicesList) { - devicesList?.forEach((element) { + devicesList.forEach((element) { print( " deviceId: ${element.deviceId} | deviceName: ${element.deviceName} | state: ${element.state}"); @@ -328,7 +338,11 @@ class _DevicesListScreenState extends State { receivedDataSubscription = nearbyService.dataReceivedSubscription(callback: (data) { print("dataReceivedSubscription: ${jsonEncode(data)}"); - Fluttertoast.showToast(msg: jsonEncode(data)); + showToast(jsonEncode(data), + context: context, + axis: Axis.horizontal, + alignment: Alignment.center, + position: StyledToastPosition.bottom); }); } } diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 302523c..75a0172 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -6,20 +6,20 @@ description: Demonstrates how to use the flutter_nearby_connections 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 - fluttertoast: ^7.0.4 + flutter_styled_toast: ^2.0.0 flutter_nearby_connections: path: ../ - cupertino_icons: ^0.1.3 - device_info: ^0.4.0+4 - flutter_udid: ^1.0.1 + cupertino_icons: ^1.0.2 + device_info: ^2.0.0 + flutter_udid: ^2.0.0 dev_dependencies: flutter_test: diff --git a/example/test/widget_test.dart b/example/test/widget_test.dart index ed5773a..e8a9379 100644 --- a/example/test/widget_test.dart +++ b/example/test/widget_test.dart @@ -19,7 +19,7 @@ void main() { expect( find.byWidgetPredicate( (Widget widget) => - widget is Text && widget.data.startsWith('Running on:'), + widget is Text && widget.data!.startsWith('Running on:'), ), findsOneWidget, ); diff --git a/lib/flutter_nearby_connections.dart b/lib/flutter_nearby_connections.dart index 3986e92..e8b951c 100644 --- a/lib/flutter_nearby_connections.dart +++ b/lib/flutter_nearby_connections.dart @@ -2,6 +2,8 @@ library flutter_nearby_connections; import 'dart:async'; import 'dart:convert'; +import 'dart:core'; + import 'dart:io'; import 'package:flutter/foundation.dart'; diff --git a/lib/src/nearby_service.dart b/lib/src/nearby_service.dart index 38106c6..5ea414a 100644 --- a/lib/src/nearby_service.dart +++ b/lib/src/nearby_service.dart @@ -48,12 +48,12 @@ class NearbyService { /// param [deviceId] is unique, you should use the UDID for [deviceId] /// param [strategy] Nearby Connections supports different Strategies for advertising and discovery. The best Strategy to use depends on the use case. only support android OS Future init( - {@required String serviceType, - @required Strategy strategy, - String deviceName, - @required Function callback}) async { - assert(serviceType.length <= 15 && - serviceType != null && + {@required String? serviceType, + @required Strategy? strategy, + String? deviceName, + @required Function? callback}) async { + assert(serviceType!.length <= 15 && + //serviceType != null && serviceType.isNotEmpty); _channel.setMethodCallHandler((handler) async { @@ -72,7 +72,7 @@ class NearbyService { break; case _invokeNearbyRunning: await Future.delayed(Duration(seconds: 1)); - callback(handler.arguments as bool); + callback!(handler.arguments as bool); break; } }); @@ -91,6 +91,9 @@ class NearbyService { case Strategy.Wi_Fi_P2P: strategyValue = 0; break; + case null : + strategyValue = -1; //error + break; } _channel.invokeMethod( @@ -103,7 +106,7 @@ class NearbyService { ); if (Platform.isIOS) { await Future.delayed(Duration(seconds: 1)); - callback(true); + callback!(true); } } @@ -137,7 +140,7 @@ class NearbyService { /// Invites a discovered peer to join a nearby connections session. /// the [deviceID] is current Device FutureOr invitePeer( - {@required String deviceID, @required String deviceName}) async { + {@required String? deviceID, @required String? deviceName}) async { await _channel.invokeMethod( _invitePeer, { @@ -149,7 +152,7 @@ class NearbyService { /// Disconnects the local peer from the session. /// the [deviceID] is current Device - FutureOr disconnectPeer({@required String deviceID}) async { + FutureOr disconnectPeer({@required String? deviceID}) async { await _channel.invokeMethod(_disconnectPeer, { 'deviceId': deviceID, }); @@ -169,7 +172,7 @@ class NearbyService { /// [stateChangedSubscription] will return you a list of [Device]. /// see [StateChangedCallback] StreamSubscription stateChangedSubscription( - {@required StateChangedCallback callback}) => + {@required StateChangedCallback? callback}) => _stateChangedStream.listen(callback); /// The [dataReceivedSubscription] helps you listen when a peer sends you @@ -177,6 +180,6 @@ class NearbyService { /// It returns a [StreamSubscription] so you can cancel listening at any time. /// see [DataReceivedCallback] StreamSubscription dataReceivedSubscription( - {@required DataReceivedCallback callback}) => + {@required DataReceivedCallback? callback}) => _dataReceivedStream.listen(callback); } diff --git a/pubspec.yaml b/pubspec.yaml index e412d74..cd7c144 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,14 +5,14 @@ author: vn.apnic@gmail.com homepage: https://github.com/VNAPNIC/flutter_nearby_connections environment: - sdk: ">=2.7.0 <3.0.0" + sdk: '>=2.12.0 <3.0.0' flutter: ">=1.10.0" dependencies: flutter: sdk: flutter - pedantic: ^1.9.0 - fluttertoast: ^7.1.1 + + dev_dependencies: flutter_test: