mirror of
https://github.com/hackku21/loc-chain-app.git
synced 2026-03-02 03:40:13 +00:00
expetion error
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_udid/flutter_udid.dart';
|
||||
import 'package:nearby_connections/nearby_connections.dart';
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
@@ -11,20 +10,15 @@ import 'package:loc_chain_app/util/keyfile_manager.dart';
|
||||
import 'package:loc_chain_app/util/transaction_manager.dart';
|
||||
|
||||
class Connect {
|
||||
final serviceId = "com.yourdomain.appname";
|
||||
final Strategy strategy = Strategy.P2P_STAR;
|
||||
late final _userName;
|
||||
final BuildContext? context;
|
||||
static final serviceId = "com.yourdomain.appname";
|
||||
static final Strategy strategy = Strategy.P2P_STAR;
|
||||
static String _userName = "";
|
||||
static BuildContext? context;
|
||||
|
||||
Map<String, ConnectionInfo> endpointMap = Map();
|
||||
Map<String, Transaction> transactionMap = Map();
|
||||
static Map<String, ConnectionInfo> endpointMap = Map();
|
||||
static Map<String, Transaction> transactionMap = Map();
|
||||
|
||||
Connect({this.context}) {
|
||||
SharedPreferences.getInstance()
|
||||
.then((s) => _userName = s.getString('userName') ?? '0');
|
||||
}
|
||||
|
||||
void showSnackbar(dynamic a) {
|
||||
static void showSnackbar(dynamic a) {
|
||||
if (context == null) {
|
||||
return;
|
||||
}
|
||||
@@ -33,78 +27,100 @@ class Connect {
|
||||
));
|
||||
}
|
||||
|
||||
void startConnect() async {
|
||||
static void stop() {
|
||||
Nearby().stopAdvertising();
|
||||
Nearby().stopDiscovery();
|
||||
Nearby().stopAllEndpoints();
|
||||
}
|
||||
|
||||
static void start() async {
|
||||
_userName = await FlutterUdid.consistentUdid;
|
||||
// final prefs = await SharedPreferences.getInstance();
|
||||
// final userName = prefs.getString('userName') ?? " ";
|
||||
|
||||
try {
|
||||
bool advertise = await Nearby().startAdvertising(
|
||||
_userName,
|
||||
strategy,
|
||||
onConnectionInitiated: onConnectionInit,
|
||||
onConnectionResult: (String id, Status status) async {
|
||||
// Called when connection is accepted/rejected
|
||||
// if connection is accepted send the transaction
|
||||
if (status != Status.CONNECTED) {
|
||||
return;
|
||||
}
|
||||
// Connected to other device, send combined hash and pub key
|
||||
String str = await Transaction.generateHash(id);
|
||||
Nearby().sendBytesPayload(id, Uint8List.fromList(str.codeUnits));
|
||||
},
|
||||
onDisconnected: (String id) {
|
||||
// Callled whenever a discoverer disconnects from advertiser
|
||||
// delete connection info
|
||||
endpointMap.remove(id);
|
||||
// delete transaction info
|
||||
transactionMap.remove(id);
|
||||
},
|
||||
serviceId: serviceId, // uniquely identifies your app
|
||||
);
|
||||
Nearby().startAdvertising(
|
||||
_userName,
|
||||
strategy,
|
||||
onConnectionInitiated: onConnectionInit,
|
||||
onConnectionResult: (String id, Status status) async {
|
||||
// Called when connection is accepted/rejected
|
||||
// if connection is accepted send the transaction
|
||||
if (status != Status.CONNECTED) {
|
||||
return;
|
||||
}
|
||||
// Connected to other device, send combined hash and pub key
|
||||
String str = await Transaction.generateHash(id);
|
||||
print(str);
|
||||
Nearby().sendBytesPayload(id, Uint8List.fromList(str.codeUnits));
|
||||
},
|
||||
onDisconnected: (String id) {
|
||||
// Callled whenever a discoverer disconnects from advertiser
|
||||
// delete connection info
|
||||
endpointMap.remove(id);
|
||||
// delete transaction info
|
||||
transactionMap.remove(id);
|
||||
},
|
||||
serviceId: serviceId, // uniquely identifies your app
|
||||
).catchError((e) {
|
||||
print(e);
|
||||
return true;
|
||||
});
|
||||
|
||||
bool discovery = await Nearby().startDiscovery(
|
||||
_userName,
|
||||
strategy,
|
||||
onEndpointFound: (String id, String userName, String serviceId) {
|
||||
// called when an advertiser is found
|
||||
Nearby().requestConnection(
|
||||
userName,
|
||||
id,
|
||||
onConnectionInitiated: onConnectionInit,
|
||||
onConnectionResult: (String id, Status status) async {
|
||||
// Called when connection is accepted/rejected
|
||||
// if connection is accepted send the transaction
|
||||
if (status != Status.CONNECTED) {
|
||||
return;
|
||||
}
|
||||
// Connected to other device, send combined hash and pub key
|
||||
String str = await Transaction.generateHash(id);
|
||||
Nearby().sendBytesPayload(id, Uint8List.fromList(str.codeUnits));
|
||||
},
|
||||
onDisconnected: (id) {
|
||||
endpointMap.remove(id);
|
||||
showSnackbar(
|
||||
"Disconnected from: ${endpointMap[id]!.endpointName}, id $id");
|
||||
},
|
||||
);
|
||||
},
|
||||
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
|
||||
}
|
||||
Nearby().startDiscovery(
|
||||
_userName,
|
||||
strategy,
|
||||
onEndpointFound: (String id, String userName, String serviceId) {
|
||||
// called when an advertiser is found
|
||||
Nearby().requestConnection(
|
||||
userName,
|
||||
id,
|
||||
onConnectionInitiated: onConnectionInit,
|
||||
onConnectionResult: (String id, Status status) async {
|
||||
// Called when connection is accepted/rejected
|
||||
// if connection is accepted send the transaction
|
||||
if (status != Status.CONNECTED) {
|
||||
return;
|
||||
}
|
||||
// Connected to other device, send combined hash and pub key
|
||||
String str = await Transaction.generateHash(id);
|
||||
Nearby()
|
||||
.sendBytesPayload(id, Uint8List.fromList(str.codeUnits))
|
||||
.catchError((e) {
|
||||
print(e);
|
||||
});
|
||||
},
|
||||
onDisconnected: (id) {
|
||||
endpointMap.remove(id);
|
||||
print(
|
||||
"Disconnected from: ${endpointMap[id]!.endpointName}, id $id");
|
||||
},
|
||||
).catchError((e) {
|
||||
print(e);
|
||||
return true;
|
||||
});
|
||||
},
|
||||
onEndpointLost: (String? id) {
|
||||
//called when an advertiser is lost (only if we weren't connected to it )
|
||||
},
|
||||
serviceId: serviceId, // uniquely identifies your app
|
||||
).catchError((e) {
|
||||
print(e);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
void onConnectionInit(String otherId, ConnectionInfo info) {
|
||||
static void onConnectionInit(String otherId, ConnectionInfo info) {
|
||||
print('Connection initialized with $otherId');
|
||||
// Called whenever a discoverer requests connection
|
||||
//
|
||||
// onConnectionInit
|
||||
if (endpointMap.containsKey(otherId)) {
|
||||
print('Connection rejected to/from $otherId');
|
||||
Nearby().rejectConnection(otherId);
|
||||
return;
|
||||
}
|
||||
print('Connection accepted to/from $otherId');
|
||||
|
||||
endpointMap[otherId] = info;
|
||||
Nearby().acceptConnection(
|
||||
otherId,
|
||||
@@ -114,7 +130,7 @@ class Connect {
|
||||
String str = String.fromCharCodes(payload.bytes!);
|
||||
var parts = str.split(':');
|
||||
if (parts.length != 2) {
|
||||
showSnackbar("$_otherId invalid payload: $str");
|
||||
print("$_otherId invalid payload: $str");
|
||||
return;
|
||||
}
|
||||
// Store transaction
|
||||
@@ -123,7 +139,6 @@ class Connect {
|
||||
transactionMap[_otherId] =
|
||||
Transaction(hash: combinedHash, pubKey: publicKey);
|
||||
// sign combined hash with our private key
|
||||
showSnackbar('Received $_otherId payload: $combinedHash');
|
||||
print('Received $_otherId payload: $combinedHash:$publicKey');
|
||||
// upload hash+otherKey to firebase
|
||||
},
|
||||
@@ -131,14 +146,16 @@ class Connect {
|
||||
if (payloadTransferUpdate.status == PayloadStatus.IN_PROGRESS) {
|
||||
print(payloadTransferUpdate.bytesTransferred);
|
||||
} else if (payloadTransferUpdate.status == PayloadStatus.FAILURE) {
|
||||
print("failed");
|
||||
showSnackbar(endid + ": FAILED to transfer file");
|
||||
print(endid + ": FAILED to transfer file");
|
||||
} else if (payloadTransferUpdate.status == PayloadStatus.SUCCESS) {
|
||||
showSnackbar(
|
||||
print(
|
||||
"$endid success, total bytes = ${payloadTransferUpdate.totalBytes}");
|
||||
}
|
||||
},
|
||||
);
|
||||
).catchError((e) {
|
||||
print(e);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user