added android example(untested)

multipeer_ios
Prerak Mann 5 years ago
parent 146d76bc9f
commit 7f4f3ede19

@ -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) ");
}
},
);
},

@ -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"

@ -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:

@ -12,7 +12,7 @@ import 'package:flutter/services.dart';
enum Strategy { P2P_CLUSTER, P2P_STAR, P2P_POINT_TO_POINT }
enum Status { CONNECTED, REJECTED, ERROR }
enum PayloadStatus { NONE, SUCCESS, FAILURE, IN_PROGRRESS, CANCELED }
enum PayloadType { NONE, BYTES, FILES, STREAM }
enum PayloadType { NONE, BYTES, FILE, STREAM }
typedef void OnConnctionInitiated(
String endpointId, ConnectionInfo connectionInfo);
typedef void OnConnectionResult(String endpointId, Status status);
@ -22,21 +22,42 @@ typedef void OnEndpointFound(
String endpointId, String endpointName, String serviceId);
typedef void OnEndpointLost(String endpointId);
/// For Bytes, this contains the bytes dala
/// Bytes may be null if [Payload.type] is not [PayloadType.BYTES]
class Payload {
int id;
PayloadType type;
Uint8List bytes;
Payload({
this.id,
this.bytes,
this.type = PayloadType.NONE,
});
}
/// gives payload status, bytes transfered and total bytes.
class PayloadTransferUpdate {
int id, bytesTransferred, totalBytes;
PayloadStatus status;
PayloadTransferUpdate({
this.id,
this.bytesTransferred,
this.totalBytes,
this.status = PayloadStatus.NONE,
});
}
/// For Bytes, this contains the bytes data
///
/// For File, this marks the start of transfer
///
/// Uint8List bytes may be null, if [payloadType] is not [PayloadType.BYTES]
typedef void OnPayloadReceived(
String endpointId, Uint8List bytes, PayloadType payloadType);
/// Uint8List bytes may be null, if [type] is not [PayloadType.BYTES]
typedef void OnPayloadReceived(String endpointId, Payload payload);
/// Called only once for Bytes and repeatedly for File until transfer is complete
typedef void OnPayloadTransferUpdate(
{String endpointId,
int payloadId,
PayloadStatus payloadStatus,
int bytesTransferred,
int totalBytes});
String endpointId, PayloadTransferUpdate payloadTransferUpdate);
// typedef void OnPayloadTransferUpdate();
/// The NearbyConnection class
@ -138,24 +159,32 @@ class Nearby {
String endpointId = args['endpointId'];
int type = args['type'];
Uint8List bytes = args['bytes'];
int payloadId = args['payloadId'];
_onPayloadReceived?.call(endpointId, bytes, PayloadType.values[type]);
Payload payload = Payload(
type: PayloadType.values[type],
bytes: bytes,
id: payloadId,
);
_onPayloadReceived?.call(endpointId, payload);
break;
case "onPayloadTransferUpdate":
String endpointId = args['endpointId'];
int payloadId = args['payloadId'];
int success = args['success'];
int status = args['status'];
int bytesTransferred = args['bytesTransferred'];
int totalBytes = args['totalBytes'];
_onPayloadTransferUpdate?.call(
endpointId: endpointId,
payloadId: payloadId,
payloadStatus: PayloadStatus.values[success],
PayloadTransferUpdate payloadTransferUpdate = PayloadTransferUpdate(
id: payloadId,
status: PayloadStatus.values[status],
bytesTransferred: bytesTransferred,
totalBytes: totalBytes,
);
_onPayloadTransferUpdate?.call(endpointId, payloadTransferUpdate);
break;
}
return null;
@ -301,8 +330,6 @@ class Nearby {
);
}
/// Accept Connection
///
/// Needs be called by both discoverer and advertiser
/// to connect
///

Loading…
Cancel
Save