11/*
2- Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
2+ Copyright 2020-2021 . Huawei Technologies Co., Ltd. All rights reserved.
33
44 Licensed under the Apache License, Version 2.0 (the "License")
55 you may not use this file except in compliance with the License.
2222import com .facebook .react .bridge .Promise ;
2323import com .facebook .react .bridge .ReactApplicationContext ;
2424import com .facebook .react .bridge .ReactMethod ;
25+ import com .facebook .react .bridge .ReadableMap ;
2526import com .facebook .react .bridge .WritableMap ;
2627import com .huawei .hms .nearby .Nearby ;
2728import com .huawei .hms .nearby .discovery .BroadcastOption ;
2829import com .huawei .hms .nearby .discovery .ConnectCallback ;
2930import com .huawei .hms .nearby .discovery .ConnectInfo ;
31+ import com .huawei .hms .nearby .discovery .ConnectOption ;
3032import com .huawei .hms .nearby .discovery .ConnectResult ;
3133import com .huawei .hms .nearby .discovery .Policy ;
3234import com .huawei .hms .nearby .discovery .ScanEndpointCallback ;
@@ -83,7 +85,7 @@ public void acceptConnect(String endpointId, final Promise promise) {
8385 }
8486
8587 handleResult ("acceptConnect" ,
86- Nearby .getDiscoveryEngine (getCurrentActivity ()).acceptConnect (endpointId , getDataCallback ()),
88+ Nearby .getDiscoveryEngine (getContext ()).acceptConnect (endpointId , getDataCallback ()),
8789 promise );
8890 }
8991
@@ -102,7 +104,7 @@ public void disconnect(String endpointId, final Promise promise) {
102104 return ;
103105 }
104106
105- Nearby .getDiscoveryEngine (getCurrentActivity ()).disconnect (endpointId );
107+ Nearby .getDiscoveryEngine (getContext ()).disconnect (endpointId );
106108 handleResult ("disconnect" , SUCCESS , promise );
107109 }
108110
@@ -122,7 +124,7 @@ public void rejectConnect(String endpointId, final Promise promise) {
122124 }
123125
124126 handleResult ("rejectConnect" ,
125- Nearby .getDiscoveryEngine (getCurrentActivity ()).rejectConnect (endpointId ),
127+ Nearby .getDiscoveryEngine (getContext ()).rejectConnect (endpointId ),
126128 promise );
127129 }
128130
@@ -143,8 +145,34 @@ public void requestConnect(String name, String endpointId, final Promise promise
143145 return ;
144146 }
145147
146- handleResult ("rejectConnect" ,
147- Nearby .getDiscoveryEngine (getCurrentActivity ()).requestConnect (name , endpointId , getConnectCallback ()),
148+ handleResult ("requestConnect" ,
149+ Nearby .getDiscoveryEngine (getContext ()).requestConnect (name , endpointId , getConnectCallback ()),
150+ promise );
151+ }
152+
153+ /**
154+ * Sends a connection request carrying specific connection options to the remote endpoint.
155+ * This is an extended method for requestConnect(String, String, ConnectCallback).
156+ * Sets {@link #getConnectCallback()} : A callback listener class called during connection.
157+ * Promise Resolve : Result Object
158+ *
159+ * @param name Local endpoint name.
160+ * @param endpointId ID of the remote endpoint.
161+ * @param connectOptionMap Options Map.
162+ */
163+ @ ReactMethod
164+ public void requestConnectEx (String name , String endpointId , ReadableMap connectOptionMap , final Promise promise ) {
165+ startMethodExecTimer ("requestConnectEx" );
166+
167+ if (TextUtils .isEmpty (name ) || TextUtils .isEmpty (endpointId )) {
168+ handleResult ("requestConnectEx" , STRING_PARAM_FAIL , promise );
169+ return ;
170+ }
171+
172+ ConnectOption connectOption = HMSUtils .getInstance ().getConnectOptionFromReadableMap (connectOptionMap );
173+
174+ handleResult ("requestConnectEx" ,
175+ Nearby .getDiscoveryEngine (getContext ()).requestConnectEx (name , endpointId , getConnectCallback (), connectOption ),
148176 promise );
149177 }
150178
@@ -173,7 +201,7 @@ public void startBroadcasting(String name, String serviceId, int policy, final P
173201 }
174202
175203 handleResult ("startBroadCasting" ,
176- Nearby .getDiscoveryEngine (getCurrentActivity ()).startBroadcasting (name ,
204+ Nearby .getDiscoveryEngine (getContext ()).startBroadcasting (name ,
177205 serviceId ,
178206 getConnectCallback (),
179207 new BroadcastOption .Builder ().setPolicy (broadcastPolicy ).build ()),
@@ -205,7 +233,7 @@ public void startScan(String serviceId, int policy, final Promise promise) {
205233 }
206234
207235 handleResult ("startScan" ,
208- Nearby .getDiscoveryEngine (getCurrentActivity ()).startScan (serviceId ,
236+ Nearby .getDiscoveryEngine (getContext ()).startScan (serviceId ,
209237 getScanEndpointCallback (),
210238 new ScanOption .Builder ().setPolicy (scanPolicy ).build ()),
211239 promise );
@@ -218,7 +246,7 @@ public void startScan(String serviceId, int policy, final Promise promise) {
218246 @ ReactMethod
219247 public void stopBroadCasting (final Promise promise ) {
220248 startMethodExecTimer ("stopBroadCasting" );
221- Nearby .getDiscoveryEngine (getCurrentActivity ()).stopBroadcasting ();
249+ Nearby .getDiscoveryEngine (getContext ()).stopBroadcasting ();
222250 handleResult ("stopBroadCasting" , SUCCESS , promise );
223251 }
224252
@@ -229,7 +257,7 @@ public void stopBroadCasting(final Promise promise) {
229257 @ ReactMethod
230258 public void disconnectAll (final Promise promise ) {
231259 startMethodExecTimer ("disconnectAll" );
232- Nearby .getDiscoveryEngine (getCurrentActivity ()).disconnectAll ();
260+ Nearby .getDiscoveryEngine (getContext ()).disconnectAll ();
233261 handleResult ("disconnectAll" , SUCCESS , promise );
234262 }
235263
@@ -240,7 +268,7 @@ public void disconnectAll(final Promise promise) {
240268 @ ReactMethod
241269 public void stopScan (final Promise promise ) {
242270 startMethodExecTimer ("stopScan" );
243- Nearby .getDiscoveryEngine (getCurrentActivity ()).stopScan ();
271+ Nearby .getDiscoveryEngine (getContext ()).stopScan ();
244272 handleResult ("stopScan" , SUCCESS , promise );
245273 }
246274
@@ -260,7 +288,10 @@ public void onReceived(String endpointId, Data data) {
260288
261289 if (data .getType () == Data .Type .FILE ) {
262290 wm .putString ("size" , Long .toString (data .asFile ().getSize ()));
263- wm .putString ("fileUri" , data .asFile ().asJavaFile ().toURI ().toString ());
291+ String fileUri = HMSUtils .getInstance ().getFileUri (data .asFile ());
292+ if (fileUri != null ){
293+ wm .putString ("fileUri" , fileUri );
294+ }
264295 } else if (data .getType () == Data .Type .BYTES ) {
265296 wm .putArray ("data" , HMSUtils .getInstance ().convertByteArrayToWritableArray (data .asBytes ()));
266297 } else if (data .getType () == Data .Type .STREAM ) {
@@ -311,6 +342,7 @@ public void onResult(String endpointId, ConnectResult connectResult) {
311342 onResult .putString ("endpointId" , endpointId );
312343 onResult .putInt ("statusCode" , connectResult .getStatus ().getStatusCode ());
313344 onResult .putString ("statusMessage" , connectResult .getStatus ().getStatusMessage ());
345+ onResult .putString ("channelPolicy" , connectResult .getChannelPolicy ().toString ());
314346 sendEvent (CONNECT_ON_RESULT , CONNECT_CALLBACK , onResult );
315347 }
316348
0 commit comments