Giter Site home page Giter Site logo

protoss78 / nearby_connections Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 133 KB

A Flutter plugin for the Nearby Connections API

License: Apache License 2.0

Kotlin 38.93% Swift 2.22% Objective-C 0.98% Dart 56.30% Ruby 1.58%
flutter nearby connections

nearby_connections's Introduction

nearby_connections

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.

Getting Started

Import package

import 'package:nearby_connections/nearby_connections.dart';

Start advertising

NearbyConnections.startAdvertising(strategy: Strategy.P2P_CLUSTER, name: myName, idService: MY_SERVICE_ID);

Listen for connection requests and auto accept them

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);

Start discovery

NearbyConnections.startDiscovery(strategy: Strategy.P2P_CLUSTER, idService: MY_SERVICE_ID);

Listen for discovered connections

NearbyConnections.getDiscoveryStream().listen((discovery) { 
  if (discovery.type == TypeDiscovery.found) {
    // New advertiser discovered
  } else if (discovery.type == TypeDiscovery.lost) {
    // Advertised connection was lost
  }
});

Reject a connection

NearbyConnections.rejectConnection(endpointId);

Send a payload to a single endpoint

const Utf8Encoder encoder = Utf8Encoder();
NearbyConnections.sendPayloads(receiverEndpointId, encoder.convert(value.toString()));

Send a payload to multiple endpoints

const Utf8Encoder encoder = Utf8Encoder();
NearbyConnections.sendPayloads(receiverEndpointIdList, encoder.convert(value.toString()));

Handle incoming payloads

const Utf8Decoder decoder = Utf8Decoder();
NearbyConnections.getPayloadStream().listen((payload) {
  String textValue = decoder.convert(payload.bytes);
});

Stop advertising

NearbyConnections.stopAdvertising();

Stop discovery

NearbyConnections.stopDiscovery();

Disconnect a single endpoint

NearbyConnections.disconnectFromEndpoint(endpointId);

Stop all connections

NearbyConnections.stopAllEndpoints();

nearby_connections's People

Contributors

protoss78 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

nearby_connections's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.