import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:nearby_service/nearby_service.dart'; import 'nearby_service_platform_interface.dart'; /// An implementation of [NearbyServicePlatform] that uses method channels. class MethodChannelNearbyService extends NearbyServicePlatform { /// The method channel used to interact with the native platform. @visibleForTesting final methodChannel = const MethodChannel('nearby_service'); @override Future getPlatformVersion() { return methodChannel.invokeMethod('getPlatformVersion'); } @override Future getPlatformModel() { return methodChannel.invokeMethod('getPlatformModel'); } @override Future getCurrentDevice() async { return NearbyDeviceMapper.instance.mapToDevice( await methodChannel.invokeMethod('getCurrentDevice'), ); } @override Future openServicesSettings() async { await methodChannel.invokeMethod('openServicesSettings'); } @override Future> getPeers() async { return NearbyDeviceMapper.instance.mapToDeviceList( await methodChannel.invokeMethod('fetchPeers'), ); } @override Stream> getPeersStream() { const peersChannel = EventChannel("nearby_service_peers"); return peersChannel.receiveBroadcastStream().map((e) { return NearbyDeviceMapper.instance.mapToDeviceList(e); }); } @override Stream getConnectedDeviceStream(NearbyDevice device) { const connectedDeviceChannel = EventChannel( "nearby_service_connected_device", ); return connectedDeviceChannel.receiveBroadcastStream(device.info.id).map( (e) => NearbyDeviceMapper.instance.mapToDevice(e), ); } }