sendpayload working

This commit is contained in:
Prerak Mann 2019-05-12 00:43:34 +05:30
parent a5a4221188
commit bf1a101c56
3 changed files with 75 additions and 8 deletions

View File

@ -75,10 +75,12 @@ public class NearbyConnectionsPlugin implements MethodCallHandler {
result.success(null); result.success(null);
break; break;
case "stopAdvertising": case "stopAdvertising":
Log.d("NearbyCon java", "stopAdvertising");
Nearby.getConnectionsClient(activity).stopAdvertising(); Nearby.getConnectionsClient(activity).stopAdvertising();
result.success(null); result.success(null);
break; break;
case "stopDiscovery": case "stopDiscovery":
Log.d("NearbyCon java", "stopDiscovery");
Nearby.getConnectionsClient(activity).stopDiscovery(); Nearby.getConnectionsClient(activity).stopDiscovery();
result.success(null); result.success(null);
break; break;
@ -209,7 +211,15 @@ public class NearbyConnectionsPlugin implements MethodCallHandler {
break; break;
} }
case "sendPayload": { 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; break;
} }
default: default:
@ -313,13 +323,19 @@ public class NearbyConnectionsPlugin implements MethodCallHandler {
private final PayloadCallback payloadCallback = new PayloadCallback() { private final PayloadCallback payloadCallback = new PayloadCallback() {
@Override @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 @Override
public void onPayloadTransferUpdate(@NonNull String s, @NonNull PayloadTransferUpdate payloadTransferUpdate) { public void onPayloadTransferUpdate(@NonNull String s, @NonNull PayloadTransferUpdate payloadTransferUpdate) {
//required for files and streams
} }
}; };

View File

@ -1,4 +1,5 @@
import 'dart:math'; import 'dart:math';
import 'dart:typed_data';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'dart:async'; import 'dart:async';
@ -35,6 +36,7 @@ class Body extends StatefulWidget {
class _MyBodyState extends State<Body> { class _MyBodyState extends State<Body> {
final String userName = Random().nextInt(1000).toString(); final String userName = Random().nextInt(1000).toString();
final Strategy strategy = Strategy.P2P_STAR; final Strategy strategy = Strategy.P2P_STAR;
String cId = "0";
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -138,6 +140,7 @@ class _MyBodyState extends State<Body> {
RaisedButton( RaisedButton(
child: Text("Request Connection"), child: Text("Request Connection"),
onPressed: () { onPressed: () {
Navigator.pop(context);
Nearby().requestConnection( Nearby().requestConnection(
userName, userName,
id, id,
@ -160,14 +163,26 @@ class _MyBodyState extends State<Body> {
child: child:
Text("Accept Connection"), Text("Accept Connection"),
onPressed: () { onPressed: () {
Nearby() cId = id;
.acceptConnection(id); Nearby().acceptConnection(
id,
onPayLoadRecieved:
(id, bytes) {
showSnackbar(id +
": " +
String
.fromCharCodes(
bytes));
},
);
}, },
), ),
RaisedButton( RaisedButton(
child: child:
Text("Reject Connection"), Text("Reject Connection"),
onPressed: () { onPressed: () {
Navigator.pop(context);
Nearby() Nearby()
.rejectConnection(id); .rejectConnection(id);
}, },
@ -215,6 +230,14 @@ class _MyBodyState extends State<Body> {
await Nearby().stopAllEndpoints(); 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));
},
),
], ],
), ),
); );

View File

@ -1,4 +1,5 @@
import 'dart:async'; import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
@ -15,6 +16,8 @@ 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);
typedef void OnPayloadReceived(String endpointId, Uint8List bytes);
class Nearby { class Nearby {
//for maintaining only 1 instance of this class //for maintaining only 1 instance of this class
static Nearby _instance; static Nearby _instance;
@ -28,9 +31,10 @@ class Nearby {
Nearby._() { Nearby._() {
_channel.setMethodCallHandler((handler) { _channel.setMethodCallHandler((handler) {
print("=========in handler============");
Map<dynamic, dynamic> args = handler.arguments; Map<dynamic, dynamic> args = handler.arguments;
print("=====================");
print(handler.method); print(handler.method);
args.forEach((s, d) { args.forEach((s, d) {
print(s + " : " + d.toString()); print(s + " : " + d.toString());
@ -103,6 +107,13 @@ class Nearby {
_onEndpointLost?.call(endpointId); _onEndpointLost?.call(endpointId);
return null; return null;
case "onPayloadReceived":
String endpointId = args['endpointId'];
Uint8List bytes = args['bytes'];
_onPayloadReceived?.call(endpointId,bytes);
break;
default: default:
return null; return null;
} }
@ -116,6 +127,8 @@ class Nearby {
OnEndpointFound _onEndpointFound; OnEndpointFound _onEndpointFound;
OnEndpointLost _onEndpointLost; OnEndpointLost _onEndpointLost;
OnPayloadReceived _onPayloadReceived;
static const MethodChannel _channel = static const MethodChannel _channel =
const MethodChannel('nearby_connections'); 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( return await _channel.invokeMethod(
'acceptConnection', 'acceptConnection',
<String, dynamic>{ <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 { abstract class ConnectionLifecycleCallback {