mirror of
https://github.com/hackku21/loc-chain-app.git
synced 2024-10-27 20:34:05 +00:00
Merge branch 'main' of github.com:hackku21/loc-
This commit is contained in:
commit
01dd228968
@ -1,5 +1,4 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:loc_chain_app/util/bluetooth.dart';
|
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import 'package:loc_chain_app/widgets/navbar.dart';
|
import 'package:loc_chain_app/widgets/navbar.dart';
|
||||||
import 'package:flutter_udid/flutter_udid.dart';
|
import 'package:flutter_udid/flutter_udid.dart';
|
||||||
@ -15,8 +14,6 @@ class App extends StatelessWidget {
|
|||||||
// This widget is the root of your application.
|
// This widget is the root of your application.
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
Connect.start();
|
|
||||||
|
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
title: 'Loc-Chain',
|
title: 'Loc-Chain',
|
||||||
theme: ThemeData(
|
theme: ThemeData(
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
import 'dart:typed_data';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_udid/flutter_udid.dart';
|
import 'package:flutter_udid/flutter_udid.dart';
|
||||||
import 'package:loc_chain_app/util/bluetooth.dart';
|
import 'package:loc_chain_app/util/transaction_manager.dart';
|
||||||
|
import 'package:nearby_connections/nearby_connections.dart';
|
||||||
|
|
||||||
|
import 'package:loc_chain_app/util/keyfile_manager.dart';
|
||||||
import 'package:loc_chain_app/util/transaction_manager.dart';
|
import 'package:loc_chain_app/util/transaction_manager.dart';
|
||||||
|
|
||||||
class HomePage extends StatefulWidget {
|
class HomePage extends StatefulWidget {
|
||||||
@ -12,70 +18,202 @@ class HomePage extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _HomePageState extends State<HomePage> {
|
class _HomePageState extends State<HomePage> {
|
||||||
final Future<String> _id = FlutterUdid.consistentUdid;
|
@override
|
||||||
Connect connector = Connect();
|
void initState() {
|
||||||
Map<String, Transaction> transactionMap = Map();
|
super.initState();
|
||||||
// String getId() =>
|
WidgetsBinding.instance!.addPostFrameCallback((_) {
|
||||||
// SharedPreferences.getInstance().then((s) => s.getString('id') ?? '0');
|
getUUID();
|
||||||
|
startAdvertising();
|
||||||
void refreshTransactions() {
|
startDiscovery();
|
||||||
setState(() {
|
|
||||||
transactionMap = Connect.transactionMap;
|
|
||||||
Connect.context = context;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static late String userName;
|
||||||
|
final Strategy strategy = Strategy.P2P_STAR;
|
||||||
|
Map<String, ConnectionInfo> endpointMap = Map();
|
||||||
|
static Map<String, Transaction> transactionMap = Map();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Center(
|
||||||
appBar: AppBar(
|
child: Padding(
|
||||||
title: Text(widget.title),
|
padding: const EdgeInsets.all(8.0),
|
||||||
),
|
child: ListView(
|
||||||
body: FutureBuilder<String>(
|
|
||||||
future: _id, // a previously-obtained Future<String> or null
|
|
||||||
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
|
|
||||||
String content = "Loading id...";
|
|
||||||
if (snapshot.hasData) {
|
|
||||||
content = "ID: ${snapshot.data!}";
|
|
||||||
}
|
|
||||||
List<Card> transactions = List.generate(
|
|
||||||
transactionMap.length,
|
|
||||||
(index) {
|
|
||||||
Transaction t = transactionMap.values.elementAt(index);
|
|
||||||
return Card(
|
|
||||||
child: Row(
|
|
||||||
children: [Text(t.hash), Text(t.pubKey)],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
return ListView(
|
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
Container(
|
Text("User Name: " + userName),
|
||||||
child: Text(content),
|
Divider(),
|
||||||
),
|
Text("Number of connected devices: ${endpointMap.length}"),
|
||||||
] +
|
ElevatedButton(
|
||||||
transactions,
|
child: Text("Stop All Endpoints"),
|
||||||
);
|
onPressed: () async {
|
||||||
}),
|
await Nearby().stopAllEndpoints();
|
||||||
floatingActionButton: FloatingActionButton(
|
setState(() {
|
||||||
onPressed: () {
|
endpointMap.clear();
|
||||||
refreshTransactions();
|
});
|
||||||
try {
|
|
||||||
Connect.stop();
|
|
||||||
Connect.start();
|
|
||||||
} catch (e) {
|
|
||||||
print(e);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
child: Icon(Icons.refresh_sharp),
|
),
|
||||||
|
Divider(),
|
||||||
|
Text(
|
||||||
|
"Sending Data",
|
||||||
|
),
|
||||||
|
ElevatedButton(
|
||||||
|
child: Text("Send Bytes Payload"),
|
||||||
|
onPressed: () async {
|
||||||
|
sendPayload();
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<String> genPayload(ConnectionInfo value) async {
|
||||||
|
print("Sender ID: $userName");
|
||||||
|
print("Receiver ID: ${value.endpointName}");
|
||||||
|
|
||||||
|
var dateTime = DateTime.now();
|
||||||
|
|
||||||
|
print('Milliseconds since Epoch: ${dateTime.millisecondsSinceEpoch}');
|
||||||
|
|
||||||
|
Transaction p2p = await Transaction.create(value.endpointName);
|
||||||
|
String json = jsonEncode(p2p);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sendPayload() async {
|
||||||
|
endpointMap.forEach((key, value) async {
|
||||||
|
String a = await genPayload(value);
|
||||||
|
|
||||||
|
showSnackbar("Sending $a to ${value.endpointName}, id: $key");
|
||||||
|
Nearby().sendBytesPayload(key, Uint8List.fromList(a.codeUnits));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void showSnackbar(dynamic a) {
|
void showSnackbar(dynamic a) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||||
content: Text(a.toString()),
|
content: Text(a.toString()),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void getUUID() async {
|
||||||
|
userName = await FlutterUdid.consistentUdid;
|
||||||
|
}
|
||||||
|
|
||||||
|
void stopAll() async {
|
||||||
|
await Nearby().stopAllEndpoints();
|
||||||
|
await Nearby().stopAdvertising();
|
||||||
|
await Nearby().stopDiscovery();
|
||||||
|
setState(() {});
|
||||||
|
}
|
||||||
|
|
||||||
|
void startDiscovery() async {
|
||||||
|
try {
|
||||||
|
userName = await FlutterUdid.consistentUdid;
|
||||||
|
bool a = await Nearby().startDiscovery(
|
||||||
|
userName,
|
||||||
|
strategy,
|
||||||
|
onEndpointFound: (id, name, serviceId) {
|
||||||
|
print("userName = $userName");
|
||||||
|
print("id = $id");
|
||||||
|
|
||||||
|
Nearby().requestConnection(
|
||||||
|
userName,
|
||||||
|
id,
|
||||||
|
onConnectionInitiated: (id, info) {
|
||||||
|
onConnectionInit(id, info);
|
||||||
|
sleep(Duration(microseconds: 500));
|
||||||
|
sendPayload();
|
||||||
|
},
|
||||||
|
onConnectionResult: (id, status) {
|
||||||
|
showSnackbar(status);
|
||||||
|
},
|
||||||
|
onDisconnected: (id) {
|
||||||
|
setState(() {
|
||||||
|
endpointMap.remove(id);
|
||||||
|
});
|
||||||
|
showSnackbar(
|
||||||
|
"Disconnected from: ${endpointMap[id]!.endpointName}, id $id");
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
onEndpointLost: (id) {
|
||||||
|
showSnackbar(
|
||||||
|
"Lost discovered Endpoint: ${endpointMap[id]!.endpointName}, id $id");
|
||||||
|
},
|
||||||
|
);
|
||||||
|
showSnackbar("DISCOVERING: " + a.toString());
|
||||||
|
} catch (e) {
|
||||||
|
showSnackbar(e);
|
||||||
|
}
|
||||||
|
setState(() {});
|
||||||
|
}
|
||||||
|
|
||||||
|
void startAdvertising() async {
|
||||||
|
try {
|
||||||
|
userName = await FlutterUdid.consistentUdid;
|
||||||
|
|
||||||
|
bool a = await Nearby().startAdvertising(
|
||||||
|
userName,
|
||||||
|
strategy,
|
||||||
|
onConnectionInitiated: (id, info) {
|
||||||
|
onConnectionInit(id, info);
|
||||||
|
sleep(Duration(microseconds: 500));
|
||||||
|
sendPayload();
|
||||||
|
}, //advertisingOnConnectionInit,
|
||||||
|
onConnectionResult: (id, status) {
|
||||||
|
print("userName = $userName");
|
||||||
|
print("id = $id");
|
||||||
|
print(status);
|
||||||
|
showSnackbar(status);
|
||||||
|
},
|
||||||
|
onDisconnected: (id) {
|
||||||
|
showSnackbar(
|
||||||
|
"Disconnected: ${endpointMap[id]!.endpointName}, id $id");
|
||||||
|
setState(() {
|
||||||
|
endpointMap.remove(id);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
showSnackbar("ADVERTISING: " + a.toString());
|
||||||
|
} catch (exception) {
|
||||||
|
showSnackbar(exception);
|
||||||
|
}
|
||||||
|
setState(() {});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Called upon Connection request (on both devices)
|
||||||
|
/// Both need to accept connection to start sending/receiving
|
||||||
|
void onConnectionInit(String id, ConnectionInfo info) {
|
||||||
|
setState(() {
|
||||||
|
endpointMap[id] = info;
|
||||||
|
});
|
||||||
|
Nearby().acceptConnection(
|
||||||
|
id,
|
||||||
|
onPayLoadRecieved: (endid, payload) async {
|
||||||
|
if (payload.type == PayloadType.BYTES) {
|
||||||
|
Transaction transaction = Transaction.fromJson(jsonDecode(
|
||||||
|
String.fromCharCodes(payload.bytes!),
|
||||||
|
));
|
||||||
|
|
||||||
|
// first device recives here
|
||||||
|
print("Receiving:");
|
||||||
|
print("${transaction.hash}");
|
||||||
|
|
||||||
|
showSnackbar(endid + ": " + transaction.hash);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onPayloadTransferUpdate: (endid, payloadTransferUpdate) {
|
||||||
|
if (payloadTransferUpdate.status == PayloadStatus.IN_PROGRESS) {
|
||||||
|
print(payloadTransferUpdate.bytesTransferred);
|
||||||
|
} else if (payloadTransferUpdate.status == PayloadStatus.FAILURE) {
|
||||||
|
print("failed");
|
||||||
|
showSnackbar(endid + ": FAILED to transfer file");
|
||||||
|
} else if (payloadTransferUpdate.status == PayloadStatus.SUCCESS) {
|
||||||
|
showSnackbar(
|
||||||
|
"$endid success, total bytes = ${payloadTransferUpdate.totalBytes}");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user