flutter_nearby_connections 0.0.1 flutter_nearby_connections: ^0.0.1 copied to clipboard
Flutter plugin supports peer-to-peer connectivity and discovers nearby devices for Android and IOS
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_nearby_connections/flutter_nearby_connections.dart';
import 'package:fluttertoast/fluttertoast.dart';
void main() {
runApp(MyApp());
}
Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
case '/':
return MaterialPageRoute(builder: (_) => Home());
case 'browser':
return MaterialPageRoute(
builder: (_) => DevicesListScreen(deviceType: DeviceType.browser));
case 'advertiser':
return MaterialPageRoute(
builder: (_) => DevicesListScreen(deviceType: DeviceType.advertiser));
default:
return MaterialPageRoute(
builder: (_) => Scaffold(
body: Center(
child: Text('No route defined for ${settings.name}')),
));
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
onGenerateRoute: generateRoute,
initialRoute: '/',
);
}
}
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Expanded(
child: InkWell(
onTap: () {
Navigator.pushNamed(context, 'browser');
},
child: Container(
color: Colors.red,
child: Center(
child: Text(
'BROWSER',
style: TextStyle(color: Colors.white, fontSize: 40),
)),
),
),
),
Expanded(
child: InkWell(
onTap: () {
Navigator.pushNamed(context, 'advertiser');
},
child: Container(
color: Colors.green,
child: Center(
child: Text(
'ADVERTISER',
style: TextStyle(color: Colors.white, fontSize: 40),
)),
),
),
),
],
),
);
}
}
enum DeviceType { advertiser, browser }
class DevicesListScreen extends StatefulWidget {
const DevicesListScreen({this.deviceType});
final DeviceType deviceType;
@override
_DevicesListScreenState createState() => _DevicesListScreenState();
}
class _DevicesListScreenState extends State<DevicesListScreen> {
List<Device> devices = [];
List<Device> connectedDevices = [];
final nearbyService = NearbyService(serviceType: 'mp-connection');
StreamSubscription subscription;
StreamSubscription receivedDataSubscription;
@override
void initState() {
super.initState();
subscription = nearbyService.stateChangedSubscription(callback: (devicesList) {
setState(() {
devices.clear();
devices.addAll(devicesList);
connectedDevices.clear();
connectedDevices.addAll(devicesList
.where((d) => d.state == SessionState.connected)
.toList());
});
});
receivedDataSubscription = nearbyService.dataReceivedSubscription(callback: (data) {
Fluttertoast.showToast(
msg: "Device ID: ${data.deviceID} , message: ${data.message}");
});
if (widget.deviceType == DeviceType.browser) {
nearbyService.startBrowsingForPeers();
} else {
nearbyService.startAdvertisingPeer();
nearbyService.startBrowsingForPeers();
}
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
});
}
@override
void dispose() {
subscription?.cancel();
receivedDataSubscription?.cancel();
nearbyService.stopBrowsingForPeers();
nearbyService.startAdvertisingPeer();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.deviceType.toString().substring(11).toUpperCase()),
),
backgroundColor: Colors.white,
body: ListView.builder(
itemCount: getItemCount(),
itemBuilder: (context, index) {
final device = widget.deviceType == DeviceType.advertiser
? connectedDevices[index]
: devices[index];
return Container(
margin: EdgeInsets.all(8.0),
child: Column(
children: [
Row(
children: [
Expanded(
child: GestureDetector(
onTap: ()=> _onTabItemListener(device),
child: Column(
children: [
Text(device.displayName),
Text(
getStateName(device.state),
style:
TextStyle(color: getStateColor(device.state)),
),
],
crossAxisAlignment: CrossAxisAlignment.start,
),
)),
GestureDetector(
onTap: ()=> _onButtonClicked(device),
child: Container(
margin: EdgeInsets.symmetric(horizontal: 8.0),
padding: EdgeInsets.all(8.0),
height: 35,
width: 100,
color: getButtonColor(device.state),
child: Center(
child: Text(
getButtonStateName(device.state),
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
),
),
),
)
],
),
SizedBox(
height: 8.0,
),
Divider(
height: 1,
color: Colors.grey,
)
],
),
);
}),
);
}
String getStateName(SessionState state) {
switch (state) {
case SessionState.notConnected:
return "disconnected";
case SessionState.connecting:
return "waiting";
default:
return "connected";
}
}
String getButtonStateName(SessionState state) {
switch (state) {
case SessionState.notConnected:
case SessionState.connecting:
return "Connect";
default:
return "Disconnect";
}
}
Color getStateColor(SessionState state) {
switch (state) {
case SessionState.notConnected:
return Colors.black;
case SessionState.connecting:
return Colors.grey;
default:
return Colors.green;
}
}
Color getButtonColor(SessionState state) {
switch (state) {
case SessionState.notConnected:
case SessionState.connecting:
return Colors.green;
default:
return Colors.red;
}
}
_onTabItemListener(Device device) {
if(device.state == SessionState.connected) {
showDialog(
context: context,
builder: (BuildContext context) {
final myController = TextEditingController();
return AlertDialog(
title: Text("Send message"),
content: TextField(controller: myController),
actions: [
FlatButton(
child: Text("Cancel"),
onPressed: () {
Navigator.of(context).pop();
},
),
FlatButton(
child: Text("Send"),
onPressed: () {
nearbyService.sendMessage(
device.deviceID, myController.text);
myController.text = '';
},
)
],
);
});
}
}
int getItemCount() {
if (widget.deviceType == DeviceType.advertiser) {
return connectedDevices.length;
} else {
return devices.length;
}
}
_onButtonClicked(Device device) {
switch (device.state) {
case SessionState.notConnected:
nearbyService.invitePeer(deviceID: device.deviceID);
break;
case SessionState.connected:
nearbyService.disconnectPeer(deviceID: device.deviceID);
break;
case SessionState.connecting:
break;
}
}
}