A Flutter plugin for the Nearby Connections API. Inspired and partially forked from nearby-connectivity.
IMPORTANT:
- Only Android side is implemented so far.
- Any help regarding an iOS adaptation would be very much appreciated.
- Also FILE and STREAM payloads are not supported yet. Only BYTE payloads can be exchanged at the moment.
import 'package:nearby_connections/nearby_connections.dart';
NearbyConnections.startAdvertising(strategy: Strategy.P2P_CLUSTER, name: myName, idService: MY_SERVICE_ID);
NearbyConnections.getConnectionLifecycleStream().listen((connection) {
switch (advertise.type) {
case TypeLifecycle.initiated:
// accept connection here
NearbyConnections.acceptConnection(connection.lifecycle.idEndpoint);
break;
case TypeLifecycle.result:
break;
case TypeLifecycle.disconnected:
// you are now disconnected
break;
}
});
NearbyConnections.startAdvertising(strategy: Strategy.P2P_CLUSTER, name: myName, idService: MY_SERVICE_ID);
NearbyConnections.startDiscovery(strategy: Strategy.P2P_CLUSTER, idService: MY_SERVICE_ID);
NearbyConnections.getDiscoveryStream().listen((discovery) {
if (discovery.type == TypeDiscovery.found) {
// New advertiser discovered
} else if (discovery.type == TypeDiscovery.lost) {
// Advertised connection was lost
}
});
NearbyConnections.rejectConnection(endpointId);
const Utf8Encoder encoder = Utf8Encoder();
NearbyConnections.sendPayloads(receiverEndpointId, encoder.convert(value.toString()));
const Utf8Encoder encoder = Utf8Encoder();
NearbyConnections.sendPayloads(receiverEndpointIdList, encoder.convert(value.toString()));
const Utf8Decoder decoder = Utf8Decoder();
NearbyConnections.getPayloadStream().listen((payload) {
String textValue = decoder.convert(payload.bytes);
});
NearbyConnections.stopAdvertising();
NearbyConnections.stopDiscovery();
NearbyConnections.disconnectFromEndpoint(endpointId);
NearbyConnections.stopAllEndpoints();