You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.9 KiB
63 lines
1.9 KiB
import 'dart:io';
|
|
import 'dart:math';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:nearby_connections/nearby_connections.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class Connect {
|
|
final serviceId = "com.yourdomain.appname";
|
|
final Strategy strategy = Strategy.P2P_STAR;
|
|
late final _userName;
|
|
|
|
Map<String, ConnectionInfo> endpointMap = Map();
|
|
|
|
Connect() {
|
|
SharedPreferences.getInstance()
|
|
.then((s) => _userName = s.getString('userName') ?? '0');
|
|
}
|
|
|
|
void startConnect() async {
|
|
// final prefs = await SharedPreferences.getInstance();
|
|
// final userName = prefs.getString('userName') ?? " ";
|
|
|
|
try {
|
|
bool advertise = await Nearby().startAdvertising(
|
|
_userName,
|
|
strategy,
|
|
onConnectionInitiated: (String id, ConnectionInfo info) {
|
|
// Called whenever a discoverer requests connection
|
|
//
|
|
// onConnectionInit
|
|
},
|
|
onConnectionResult: (String id, Status status) {
|
|
// Called when connection is accepted/rejected
|
|
// if connection is accepted send the transaction
|
|
//
|
|
//
|
|
},
|
|
onDisconnected: (String id) {
|
|
// Callled whenever a discoverer disconnects from advertiser
|
|
},
|
|
serviceId: serviceId, // uniquely identifies your app
|
|
);
|
|
|
|
bool discovery = await Nearby().startDiscovery(
|
|
_userName,
|
|
strategy,
|
|
onEndpointFound: (String id, String userName, String serviceId) {
|
|
// called when an advertiser is found
|
|
},
|
|
onEndpointLost: (String? id) {
|
|
//called when an advertiser is lost (only if we weren't connected to it )
|
|
},
|
|
serviceId: serviceId, // uniquely identifies your app
|
|
);
|
|
} catch (exception) {
|
|
// platform exceptions like unable to start bluetooth or insufficient permissions
|
|
}
|
|
}
|
|
}
|