mirror of
https://github.com/hackku21/nearby_connections.git
synced 2024-10-27 19:14:01 +00:00
sendpayload working
This commit is contained in:
parent
a5a4221188
commit
bf1a101c56
@ -75,10 +75,12 @@ public class NearbyConnectionsPlugin implements MethodCallHandler {
|
||||
result.success(null);
|
||||
break;
|
||||
case "stopAdvertising":
|
||||
Log.d("NearbyCon java", "stopAdvertising");
|
||||
Nearby.getConnectionsClient(activity).stopAdvertising();
|
||||
result.success(null);
|
||||
break;
|
||||
case "stopDiscovery":
|
||||
Log.d("NearbyCon java", "stopDiscovery");
|
||||
Nearby.getConnectionsClient(activity).stopDiscovery();
|
||||
result.success(null);
|
||||
break;
|
||||
@ -209,7 +211,15 @@ public class NearbyConnectionsPlugin implements MethodCallHandler {
|
||||
break;
|
||||
}
|
||||
case "sendPayload": {
|
||||
//Nearby.getConnectionsClient(activity).sendPayload()
|
||||
String endpointId = (String) call.argument("endpointId");
|
||||
byte[] bytes = call.argument("bytes");
|
||||
|
||||
assert endpointId != null;
|
||||
assert bytes != null;
|
||||
String hello = "okay";
|
||||
Nearby.getConnectionsClient(activity).sendPayload(endpointId, Payload.fromBytes(hello.getBytes()));
|
||||
Log.d("NearbyCon java", "sentPayload");
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@ -313,13 +323,19 @@ public class NearbyConnectionsPlugin implements MethodCallHandler {
|
||||
|
||||
private final PayloadCallback payloadCallback = new PayloadCallback() {
|
||||
@Override
|
||||
public void onPayloadReceived(@NonNull String s, @NonNull Payload payload) {
|
||||
|
||||
public void onPayloadReceived(@NonNull String endpointId, @NonNull Payload payload) {
|
||||
Log.d("NearbyCon java", "onPayloadReceived");
|
||||
Map<String, Object> args = new HashMap<>();
|
||||
args.put("endpointId", endpointId);
|
||||
byte[] bytes = payload.asBytes();
|
||||
assert bytes != null;
|
||||
args.put("bytes", bytes);
|
||||
channel.invokeMethod("onPayloadReceived", args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPayloadTransferUpdate(@NonNull String s, @NonNull PayloadTransferUpdate payloadTransferUpdate) {
|
||||
|
||||
//required for files and streams
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
import 'dart:math';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'dart:async';
|
||||
@ -35,6 +36,7 @@ class Body extends StatefulWidget {
|
||||
class _MyBodyState extends State<Body> {
|
||||
final String userName = Random().nextInt(1000).toString();
|
||||
final Strategy strategy = Strategy.P2P_STAR;
|
||||
String cId = "0";
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@ -138,6 +140,7 @@ class _MyBodyState extends State<Body> {
|
||||
RaisedButton(
|
||||
child: Text("Request Connection"),
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
Nearby().requestConnection(
|
||||
userName,
|
||||
id,
|
||||
@ -160,14 +163,26 @@ class _MyBodyState extends State<Body> {
|
||||
child:
|
||||
Text("Accept Connection"),
|
||||
onPressed: () {
|
||||
Nearby()
|
||||
.acceptConnection(id);
|
||||
cId = id;
|
||||
Nearby().acceptConnection(
|
||||
id,
|
||||
onPayLoadRecieved:
|
||||
(id, bytes) {
|
||||
showSnackbar(id +
|
||||
": " +
|
||||
String
|
||||
.fromCharCodes(
|
||||
bytes));
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
RaisedButton(
|
||||
child:
|
||||
Text("Reject Connection"),
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
|
||||
Nearby()
|
||||
.rejectConnection(id);
|
||||
},
|
||||
@ -215,6 +230,14 @@ class _MyBodyState extends State<Body> {
|
||||
await Nearby().stopAllEndpoints();
|
||||
},
|
||||
),
|
||||
RaisedButton(
|
||||
child: Text("Send Random Payload"),
|
||||
onPressed: () async {
|
||||
String a = Random().nextInt(100).toString();
|
||||
showSnackbar("Sending $a");
|
||||
Nearby().sendPayload(cId, Uint8List.fromList(a.codeUnits));
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
@ -1,4 +1,5 @@
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
@ -15,6 +16,8 @@ typedef void OnEndpointFound(
|
||||
String endpointId, String endpointName, String serviceId);
|
||||
typedef void OnEndpointLost(String endpointId);
|
||||
|
||||
typedef void OnPayloadReceived(String endpointId, Uint8List bytes);
|
||||
|
||||
class Nearby {
|
||||
//for maintaining only 1 instance of this class
|
||||
static Nearby _instance;
|
||||
@ -28,9 +31,10 @@ class Nearby {
|
||||
|
||||
Nearby._() {
|
||||
_channel.setMethodCallHandler((handler) {
|
||||
print("=========in handler============");
|
||||
|
||||
Map<dynamic, dynamic> args = handler.arguments;
|
||||
|
||||
print("=====================");
|
||||
print(handler.method);
|
||||
args.forEach((s, d) {
|
||||
print(s + " : " + d.toString());
|
||||
@ -103,6 +107,13 @@ class Nearby {
|
||||
_onEndpointLost?.call(endpointId);
|
||||
|
||||
return null;
|
||||
case "onPayloadReceived":
|
||||
String endpointId = args['endpointId'];
|
||||
Uint8List bytes = args['bytes'];
|
||||
|
||||
_onPayloadReceived?.call(endpointId,bytes);
|
||||
|
||||
break;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
@ -116,6 +127,8 @@ class Nearby {
|
||||
OnEndpointFound _onEndpointFound;
|
||||
OnEndpointLost _onEndpointLost;
|
||||
|
||||
OnPayloadReceived _onPayloadReceived;
|
||||
|
||||
static const MethodChannel _channel =
|
||||
const MethodChannel('nearby_connections');
|
||||
|
||||
@ -201,7 +214,12 @@ class Nearby {
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool> acceptConnection(String endpointId) async {
|
||||
Future<bool> acceptConnection(
|
||||
String endpointId, {
|
||||
@required OnPayloadReceived onPayLoadRecieved,
|
||||
}) async {
|
||||
this._onPayloadReceived = onPayLoadRecieved;
|
||||
|
||||
return await _channel.invokeMethod(
|
||||
'acceptConnection',
|
||||
<String, dynamic>{
|
||||
@ -218,6 +236,16 @@ class Nearby {
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> sendPayload(String endpointId, Uint8List bytes) async {
|
||||
return await _channel.invokeMethod(
|
||||
'sendPayload',
|
||||
<String, dynamic>{
|
||||
'endpointId': endpointId,
|
||||
'bytes': bytes,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
abstract class ConnectionLifecycleCallback {
|
||||
|
Loading…
Reference in New Issue
Block a user