diff --git a/android/src/main/kotlin/com/xenikii/nearby_service/NearbyServiceManager.kt b/android/src/main/kotlin/com/xenikii/nearby_service/NearbyServiceManager.kt index e8aa797..8728cd6 100644 --- a/android/src/main/kotlin/com/xenikii/nearby_service/NearbyServiceManager.kt +++ b/android/src/main/kotlin/com/xenikii/nearby_service/NearbyServiceManager.kt @@ -289,4 +289,28 @@ class NearbyServiceManager(private var context: Context) { handler.removeCallbacks(postCallback) } } + var connectionInfoHandler = object : EventChannel.StreamHandler { + private var handler: Handler = Handler(Looper.getMainLooper()) + private var eventSink: EventChannel.EventSink? = null + + val postCallback = object : Runnable { + override fun run() { + handler.post { eventSink?.success(receiver.wifiInfo?.toJsonString()) } + handler.postDelayed(this, 1000) + } + } + + override fun onListen(arguments: Any?, sink: EventChannel.EventSink?) { + onCancel(null) + eventSink = sink + Logger.d("Listen connection info") + handler.postDelayed(postCallback, 1000) + } + + override fun onCancel(p0: Any?) { + Logger.d("Kill last process connection info") + eventSink = null + handler.removeCallbacks(postCallback) + } + } } \ No newline at end of file diff --git a/android/src/main/kotlin/com/xenikii/nearby_service/NearbyServicePlugin.kt b/android/src/main/kotlin/com/xenikii/nearby_service/NearbyServicePlugin.kt index c577c89..9a9ac65 100644 --- a/android/src/main/kotlin/com/xenikii/nearby_service/NearbyServicePlugin.kt +++ b/android/src/main/kotlin/com/xenikii/nearby_service/NearbyServicePlugin.kt @@ -17,6 +17,7 @@ import kotlinx.coroutines.launch const val CHANNEL_NAME = "nearby_service" const val PEERS_CHANNEL_NAME = "nearby_service_peers" const val CONNECTED_DEVICE_CHANNEL_NAME = "nearby_service_connected_device" +const val CONNECTION_INFO_CHANNEL_NAME = "nearby_service_connection_info" /** * Plugin for creating connections in the Wi-fi Direct scope. @@ -27,6 +28,7 @@ class NearbyServicePlugin : FlutterPlugin, MethodCallHandler, ActivityAware { private lateinit var manager: NearbyServiceManager private lateinit var peersChannel: EventChannel private lateinit var connectedDeviceChannel: EventChannel + private lateinit var connectionInfoChannel: EventChannel @OptIn(DelicateCoroutinesApi::class) @@ -157,6 +159,9 @@ class NearbyServicePlugin : FlutterPlugin, MethodCallHandler, ActivityAware { connectedDeviceChannel = EventChannel(binaryMessenger, CONNECTED_DEVICE_CHANNEL_NAME) connectedDeviceChannel.setStreamHandler(manager.connectedDeviceInfoHandler) + + connectionInfoChannel = EventChannel(binaryMessenger, CONNECTION_INFO_CHANNEL_NAME) + connectionInfoChannel.setStreamHandler(manager.connectionInfoHandler) } diff --git a/example/lib/main.dart b/example/lib/main.dart index b5484aa..bcbbbb8 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -46,27 +46,30 @@ class MyApp extends StatelessWidget { crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Padding( - padding: const EdgeInsets.fromLTRB(16, 20, 16, 0), + padding: const EdgeInsets.fromLTRB(16, 10, 16, 10), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( - 'Platform: ${service.platformVersion}\n' - 'Model: ${service.platformModel}', + 'Platform: ${service.platformVersion}, Model: ${service.platformModel}', ), if (service.currentDeviceInfo != null) Text( - 'Device Name: ${service.currentDeviceInfo!.displayName}' - '${Platform.isIOS ? '\nDevice ID: ${service.currentDeviceInfo!.id}' : ''}', + 'Device Name: ${service.currentDeviceInfo!.displayName} ${Platform.isIOS ? '\nDevice ID: ${service.currentDeviceInfo!.id}' : ''}', ), + Text( + 'Communication channel state: ${service.communicationChannelState.previewName}\n', + ), if (Platform.isIOS) Text( 'You are ${service.isIOSBrowser ? 'going to find your friend' : 'waiting for another user to connect'}', ), - Text( - 'Communication channel state: ${service.communicationChannelState.previewName}', - ) + if (Platform.isAndroid && + service.isAndroidGroupOwner != null) + Text( + 'You ${service.isAndroidGroupOwner! ? 'are' : 'are not'} a group owner', + ), ], ), ), @@ -201,8 +204,7 @@ class AppService extends ChangeNotifier { StreamSubscription? peersSubscription; StreamSubscription? connectedDeviceSubscription; - - final _filesAccepts = >{}; + StreamSubscription? connectionInfoSubscription; @override void dispose() { @@ -218,8 +220,8 @@ class AppService extends ChangeNotifier { return _nearbyService.ios?.isBrowser.value ?? false; } - bool get isAndroidGroupOwner { - return Platform.isAndroid && (connectionAndroidInfo?.isGroupOwner ?? false); + bool? get isAndroidGroupOwner { + return connectionAndroidInfo?.isGroupOwner; } Future getPlatformInfo() async { @@ -287,6 +289,7 @@ class AppService extends ChangeNotifier { final result = await _nearbyService.discover(); if (result) { updateState(AppState.discoveringPeers); + startListeningConnectionInfo(); } } catch (e) { if (kDebugMode) { @@ -325,15 +328,36 @@ class AppService extends ChangeNotifier { } Future stopListeningPeers() async { - peersSubscription?.cancel(); + await peersSubscription?.cancel(); peers = null; updateState(AppState.discoveringPeers); } + Future stopListeningConnectionInfo() async { + await connectionInfoSubscription?.cancel(); + connectionInfoSubscription = null; + } + Future connect(NearbyDevice device) async { try { await _nearbyService.connect(device); - connectionAndroidInfo = await _nearbyService.android?.getConnectionInfo(); + } catch (e) { + if (kDebugMode) { + print(e); + } + } + notifyListeners(); + } + + void startListeningConnectionInfo() { + try { + connectionInfoSubscription = + _nearbyService.android?.getConnectionInfoStream().listen( + (event) async { + connectionAndroidInfo = event; + notifyListeners(); + }, + ); } catch (e) { if (kDebugMode) { print(e); @@ -468,10 +492,6 @@ class AppService extends ChangeNotifier { ); } - void setFileAcceptFuture(String id, Future future) { - _filesAccepts[id] = future; - } - Future disconnect(NearbyDevice device) async { try { await _nearbyService.disconnect(device); @@ -488,6 +508,7 @@ class AppService extends ChangeNotifier { Future stopListeningAll() async { await stopListeningConnectedDevice(); await stopListeningPeers(); + await stopListeningConnectionInfo(); await stopDiscovery(); } @@ -817,7 +838,7 @@ class _ConnectedBody extends StatelessWidget { else Text( 'Connecting socket.. ' - '${service.isAndroidGroupOwner ? "Waiting a client for connect" : "Waiting a connection"}', + '${service.isAndroidGroupOwner != null ? service.isAndroidGroupOwner! ? "Waiting a client for connect" : "Waiting a server for connect" : "Waiting a connection"}', ) ], ), diff --git a/lib/src/platforms/android/nearby_android_service.dart b/lib/src/platforms/android/nearby_android_service.dart index 40a41ea..fa40b2c 100644 --- a/lib/src/platforms/android/nearby_android_service.dart +++ b/lib/src/platforms/android/nearby_android_service.dart @@ -126,6 +126,14 @@ class NearbyAndroidService extends NearbyService { return NearbyServiceAndroidPlatform.instance.getConnectionInfo(); } + /// + /// Streams [NearbyConnectionAndroidInfo] - + /// information about the connection information. + /// + Stream getConnectionInfoStream() { + return NearbyServiceAndroidPlatform.instance.getConnectionInfoStream(); + } + void _requireAndroidDevice(NearbyDevice device) { assert( device is NearbyAndroidDevice, diff --git a/lib/src/platforms/android/nearby_service_android_interface.dart b/lib/src/platforms/android/nearby_service_android_interface.dart index 26a44fb..949d981 100644 --- a/lib/src/platforms/android/nearby_service_android_interface.dart +++ b/lib/src/platforms/android/nearby_service_android_interface.dart @@ -54,4 +54,10 @@ abstract class NearbyServiceAndroidPlatform extends PlatformInterface { Future disconnect(String deviceAddress) { throw UnimplementedError('disconnect() has not been implemented.'); } + + Stream getConnectionInfoStream() { + throw UnimplementedError( + 'getConnectionInfoStream() has not been implemented.', + ); + } } diff --git a/lib/src/platforms/android/nearby_service_android_method_channel.dart b/lib/src/platforms/android/nearby_service_android_method_channel.dart index 2d92c69..8fc9454 100644 --- a/lib/src/platforms/android/nearby_service_android_method_channel.dart +++ b/lib/src/platforms/android/nearby_service_android_method_channel.dart @@ -64,4 +64,14 @@ class MethodChannelAndroidNearbyService extends NearbyServiceAndroidPlatform { )) ?? false; } + + @override + Stream getConnectionInfoStream() { + const connectedDeviceChannel = EventChannel( + "nearby_service_connection_info", + ); + return connectedDeviceChannel.receiveBroadcastStream().map( + (e) => NearbyConnectionInfoMapper.mapToInfo(e), + ); + } } diff --git a/lib/src/utils/file_socket.dart b/lib/src/utils/file_socket.dart index a2a9c16..98cce0f 100644 --- a/lib/src/utils/file_socket.dart +++ b/lib/src/utils/file_socket.dart @@ -31,8 +31,8 @@ class FilesSocket { final NearbyMessageFilesContent content; final void Function(String) onDestroy; final NearbyServiceFilesListener? listener; - final WebSocket _socket; + final _files = []; final _bytesTable = >{'0': []}; final _futures = [];