mirror of
https://github.com/hackku21/nearby_connections.git
synced 2024-10-27 19:14:01 +00:00
added android example(untested)
This commit is contained in:
parent
146d76bc9f
commit
7f4f3ede19
@ -1,8 +1,10 @@
|
|||||||
|
import 'dart:io';
|
||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:nearby_connections/nearby_connections.dart';
|
import 'package:nearby_connections/nearby_connections.dart';
|
||||||
|
import 'package:image_picker/image_picker.dart';
|
||||||
|
|
||||||
void main() => runApp(MyApp());
|
void main() => runApp(MyApp());
|
||||||
|
|
||||||
@ -154,13 +156,25 @@ class _MyBodyState extends State<Body> {
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
RaisedButton(
|
RaisedButton(
|
||||||
child: Text("Send Random Payload"),
|
child: Text("Send Random Bytes Payload"),
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
String a = Random().nextInt(100).toString();
|
String a = Random().nextInt(100).toString();
|
||||||
showSnackbar("Sending $a to $cId");
|
showSnackbar("Sending $a to $cId");
|
||||||
Nearby().sendPayload(cId, Uint8List.fromList(a.codeUnits));
|
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) {
|
void oci(String id, ConnectionInfo info) {
|
||||||
showModalBottomSheet(
|
showModalBottomSheet(
|
||||||
context: context,
|
context: context,
|
||||||
@ -190,8 +205,29 @@ class _MyBodyState extends State<Body> {
|
|||||||
cId = id;
|
cId = id;
|
||||||
Nearby().acceptConnection(
|
Nearby().acceptConnection(
|
||||||
id,
|
id,
|
||||||
onPayLoadRecieved: (endid, bytes, payloadType) {
|
onPayLoadRecieved: (endid, payload) {
|
||||||
showSnackbar(endid + ": " + String.fromCharCodes(bytes));
|
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
|
description: flutter
|
||||||
source: sdk
|
source: sdk
|
||||||
version: "0.0.0"
|
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:
|
matcher:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -74,6 +81,13 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.6.2"
|
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:
|
pedantic:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -151,3 +165,4 @@ packages:
|
|||||||
version: "2.0.8"
|
version: "2.0.8"
|
||||||
sdks:
|
sdks:
|
||||||
dart: ">=2.2.2 <3.0.0"
|
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.
|
# The following adds the Cupertino Icons font to your application.
|
||||||
# Use with the CupertinoIcons class for iOS style icons.
|
# Use with the CupertinoIcons class for iOS style icons.
|
||||||
cupertino_icons: ^0.1.2
|
cupertino_icons: ^0.1.2
|
||||||
|
path_provider: ^1.2.0
|
||||||
|
image_picker: ^0.6.1+2
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
@ -12,7 +12,7 @@ import 'package:flutter/services.dart';
|
|||||||
enum Strategy { P2P_CLUSTER, P2P_STAR, P2P_POINT_TO_POINT }
|
enum Strategy { P2P_CLUSTER, P2P_STAR, P2P_POINT_TO_POINT }
|
||||||
enum Status { CONNECTED, REJECTED, ERROR }
|
enum Status { CONNECTED, REJECTED, ERROR }
|
||||||
enum PayloadStatus { NONE, SUCCESS, FAILURE, IN_PROGRRESS, CANCELED }
|
enum PayloadStatus { NONE, SUCCESS, FAILURE, IN_PROGRRESS, CANCELED }
|
||||||
enum PayloadType { NONE, BYTES, FILES, STREAM }
|
enum PayloadType { NONE, BYTES, FILE, STREAM }
|
||||||
typedef void OnConnctionInitiated(
|
typedef void OnConnctionInitiated(
|
||||||
String endpointId, ConnectionInfo connectionInfo);
|
String endpointId, ConnectionInfo connectionInfo);
|
||||||
typedef void OnConnectionResult(String endpointId, Status status);
|
typedef void OnConnectionResult(String endpointId, Status status);
|
||||||
@ -22,21 +22,42 @@ typedef void OnEndpointFound(
|
|||||||
String endpointId, String endpointName, String serviceId);
|
String endpointId, String endpointName, String serviceId);
|
||||||
typedef void OnEndpointLost(String endpointId);
|
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
|
/// For File, this marks the start of transfer
|
||||||
///
|
///
|
||||||
/// Uint8List bytes may be null, if [payloadType] is not [PayloadType.BYTES]
|
/// Uint8List bytes may be null, if [type] is not [PayloadType.BYTES]
|
||||||
typedef void OnPayloadReceived(
|
typedef void OnPayloadReceived(String endpointId, Payload payload);
|
||||||
String endpointId, Uint8List bytes, PayloadType payloadType);
|
|
||||||
|
|
||||||
/// Called only once for Bytes and repeatedly for File until transfer is complete
|
/// Called only once for Bytes and repeatedly for File until transfer is complete
|
||||||
typedef void OnPayloadTransferUpdate(
|
typedef void OnPayloadTransferUpdate(
|
||||||
{String endpointId,
|
String endpointId, PayloadTransferUpdate payloadTransferUpdate);
|
||||||
int payloadId,
|
|
||||||
PayloadStatus payloadStatus,
|
|
||||||
int bytesTransferred,
|
|
||||||
int totalBytes});
|
|
||||||
|
|
||||||
// typedef void OnPayloadTransferUpdate();
|
// typedef void OnPayloadTransferUpdate();
|
||||||
/// The NearbyConnection class
|
/// The NearbyConnection class
|
||||||
@ -138,24 +159,32 @@ class Nearby {
|
|||||||
String endpointId = args['endpointId'];
|
String endpointId = args['endpointId'];
|
||||||
int type = args['type'];
|
int type = args['type'];
|
||||||
Uint8List bytes = args['bytes'];
|
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;
|
break;
|
||||||
case "onPayloadTransferUpdate":
|
case "onPayloadTransferUpdate":
|
||||||
String endpointId = args['endpointId'];
|
String endpointId = args['endpointId'];
|
||||||
int payloadId = args['payloadId'];
|
int payloadId = args['payloadId'];
|
||||||
int success = args['success'];
|
int status = args['status'];
|
||||||
int bytesTransferred = args['bytesTransferred'];
|
int bytesTransferred = args['bytesTransferred'];
|
||||||
int totalBytes = args['totalBytes'];
|
int totalBytes = args['totalBytes'];
|
||||||
|
|
||||||
_onPayloadTransferUpdate?.call(
|
PayloadTransferUpdate payloadTransferUpdate = PayloadTransferUpdate(
|
||||||
endpointId: endpointId,
|
id: payloadId,
|
||||||
payloadId: payloadId,
|
status: PayloadStatus.values[status],
|
||||||
payloadStatus: PayloadStatus.values[success],
|
|
||||||
bytesTransferred: bytesTransferred,
|
bytesTransferred: bytesTransferred,
|
||||||
totalBytes: totalBytes,
|
totalBytes: totalBytes,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
_onPayloadTransferUpdate?.call(endpointId, payloadTransferUpdate);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@ -301,8 +330,6 @@ class Nearby {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Accept Connection
|
|
||||||
///
|
|
||||||
/// Needs be called by both discoverer and advertiser
|
/// Needs be called by both discoverer and advertiser
|
||||||
/// to connect
|
/// to connect
|
||||||
///
|
///
|
||||||
|
Loading…
Reference in New Issue
Block a user