added android example(untested)

This commit is contained in:
Prerak Mann
2019-08-11 21:11:40 +05:30
parent 146d76bc9f
commit 7f4f3ede19
4 changed files with 101 additions and 21 deletions

View File

@@ -1,8 +1,10 @@
import 'dart:io';
import 'dart:math';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:nearby_connections/nearby_connections.dart';
import 'package:image_picker/image_picker.dart';
void main() => runApp(MyApp());
@@ -154,13 +156,25 @@ class _MyBodyState extends State<Body> {
},
),
RaisedButton(
child: Text("Send Random Payload"),
child: Text("Send Random Bytes Payload"),
onPressed: () async {
String a = Random().nextInt(100).toString();
showSnackbar("Sending $a to $cId");
Nearby().sendPayload(cId, Uint8List.fromList(a.codeUnits));
},
),
RaisedButton(
child: Text("Send File Payload"),
onPressed: () async {
File file =
await ImagePicker.pickImage(source: ImageSource.gallery);
if (file == null) return;
Nearby().sendFilePayload(cId, file.path);
showSnackbar("Sending file to $cId");
},
),
],
),
);
@@ -172,6 +186,7 @@ class _MyBodyState extends State<Body> {
));
}
/// Called on a Connection request (on both devices)
void oci(String id, ConnectionInfo info) {
showModalBottomSheet(
context: context,
@@ -190,8 +205,29 @@ class _MyBodyState extends State<Body> {
cId = id;
Nearby().acceptConnection(
id,
onPayLoadRecieved: (endid, bytes, payloadType) {
showSnackbar(endid + ": " + String.fromCharCodes(bytes));
onPayLoadRecieved: (endid, payload) {
if (payload.type == PayloadType.BYTES) {
showSnackbar(
endid + ": " + String.fromCharCodes(payload.bytes));
} else if (payload.type == PayloadType.FILE) {
showSnackbar(endid + ": File transfer started");
}
},
onPayloadTransferUpdate: (endid, payloadTransferUpdate) {
if (payloadTransferUpdate.status ==
PayloadStatus.IN_PROGRRESS) {
print(payloadTransferUpdate.bytesTransferred);
} else if (payloadTransferUpdate.status ==
PayloadStatus.FAILURE) {
print("failed");
showSnackbar(endid + ": FAILED to transfer file");
} else if (payloadTransferUpdate.status ==
PayloadStatus.SUCCESS) {
print(
"success, total bytes = ${payloadTransferUpdate.totalBytes}");
showSnackbar(endid +
": SUCCESS in file transfer (file is un-named in downloads) ");
}
},
);
},

View File

@@ -46,6 +46,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
image_picker:
dependency: "direct main"
description:
name: image_picker
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.1+2"
matcher:
dependency: transitive
description:
@@ -74,6 +81,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.2"
path_provider:
dependency: "direct main"
description:
name: path_provider
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
pedantic:
dependency: transitive
description:
@@ -151,3 +165,4 @@ packages:
version: "2.0.8"
sdks:
dart: ">=2.2.2 <3.0.0"
flutter: ">=1.5.0 <2.0.0"

View File

@@ -11,6 +11,8 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
path_provider: ^1.2.0
image_picker: ^0.6.1+2
dev_dependencies:
flutter_test: