1
1
package com .wearconnectivity ;
2
2
3
- import android .app .ActivityManager ;
4
- import android .content .Context ;
5
3
import android .os .Build ;
6
4
import android .util .Log ;
7
5
import androidx .annotation .NonNull ;
8
- import androidx .annotation .Nullable ;
9
6
import com .facebook .common .logging .FLog ;
10
7
import com .facebook .react .bridge .Arguments ;
11
8
import com .facebook .react .bridge .Callback ;
12
9
import com .facebook .react .bridge .JSONArguments ;
13
10
import com .facebook .react .bridge .LifecycleEventListener ;
11
+ import com .facebook .react .bridge .Promise ;
14
12
import com .facebook .react .bridge .ReactApplicationContext ;
15
- import com .facebook .react .bridge .ReactContext ;
16
13
import com .facebook .react .bridge .ReactMethod ;
17
14
import com .facebook .react .bridge .ReadableMap ;
18
15
import com .facebook .react .bridge .WritableMap ;
19
- import com .facebook .react .modules .core .DeviceEventManagerModule ;
20
16
import com .google .android .gms .common .ConnectionResult ;
21
17
import com .google .android .gms .tasks .OnFailureListener ;
22
18
import com .google .android .gms .tasks .OnSuccessListener ;
27
23
import com .google .android .gms .wearable .Node ;
28
24
import com .google .android .gms .wearable .NodeClient ;
29
25
import com .google .android .gms .wearable .Wearable ;
26
+
30
27
import java .util .List ;
31
28
import org .json .JSONException ;
32
29
import org .json .JSONObject ;
@@ -43,7 +40,10 @@ public class WearConnectivityModule extends WearConnectivitySpec
43
40
private static ReactApplicationContext reactContext ;
44
41
public static final String NAME = "WearConnectivity" ;
45
42
private static final String TAG = "react-native-wear-connectivity " ;
46
- private final MessageClient client ;
43
+ private final MessageClient messageClient ;
44
+ private final WearConnectivityDataClient dataClient ;
45
+ private boolean isListenerAdded = false ;
46
+
47
47
private String CLIENT_ADDED =
48
48
TAG + "onMessageReceived listener added when activity is created. Client receives messages." ;
49
49
private String NO_NODES_FOUND = TAG + "sendMessage failed. No connected nodes found." ;
@@ -61,9 +61,10 @@ public class WearConnectivityModule extends WearConnectivitySpec
61
61
super (context );
62
62
reactContext = context ;
63
63
context .addLifecycleEventListener (this );
64
- client = Wearable .getMessageClient (context );
64
+ messageClient = Wearable .getMessageClient (context );
65
+ dataClient = new WearConnectivityDataClient (context );
65
66
Log .d (TAG , CLIENT_ADDED );
66
- client .addListener (this );
67
+ messageClient .addListener (this );
67
68
}
68
69
69
70
@ Override
@@ -72,32 +73,19 @@ public String getName() {
72
73
return NAME ;
73
74
}
74
75
75
- private List <Node > retrieveNodes (Callback errorCb ) {
76
- try {
77
- int result = GoogleApiAvailability .getInstance ().isGooglePlayServicesAvailable (getReactApplicationContext ());
78
- ConnectionResult connectionResult = new ConnectionResult (result );
79
- if (!connectionResult .isSuccess ()) {
80
- errorCb .invoke ( MISSING_GOOGLE_PLAY_SERVICES + connectionResult .getErrorMessage ());
81
- return null ;
82
- }
83
- NodeClient nodeClient = Wearable .getNodeClient (getReactApplicationContext ());
84
- try {
85
- Tasks .await (GoogleApiAvailability .getInstance ().checkApiAvailability (nodeClient ));
86
- } catch (Exception e ) {
87
- errorCb .invoke (INSTALL_GOOGLE_PLAY_WEARABLE + e );
88
- return null ;
89
- }
90
- return Tasks .await (nodeClient .getConnectedNodes ());
91
- } catch (Exception e ) {
92
- errorCb .invoke (RETRIEVE_NODES_FAILED + e );
93
- return null ;
76
+ @ ReactMethod
77
+ public void sendFile (String fileName , Promise promise ) {
78
+ if (dataClient != null ) {
79
+ dataClient .sendFile (fileName , promise );
80
+ } else {
81
+ promise .reject ("E_SEND_FAILED" , "Failed to send file" );
94
82
}
95
83
}
96
84
97
85
@ ReactMethod
98
86
public void sendMessage (ReadableMap messageData , Callback replyCb , Callback errorCb ) {
99
87
List <Node > connectedNodes = retrieveNodes (errorCb );
100
- if (connectedNodes != null && connectedNodes .size () > 0 && client != null ) {
88
+ if (connectedNodes != null && connectedNodes .size () > 0 && messageClient != null ) {
101
89
for (Node connectedNode : connectedNodes ) {
102
90
if (connectedNode .isNearby ()) {
103
91
sendMessageToClient (messageData , connectedNode , replyCb , errorCb );
@@ -122,7 +110,7 @@ private void sendMessageToClient(
122
110
try {
123
111
// the last parameter is for file transfer (for ex. audio)
124
112
JSONObject messageJSON = new JSONObject (messageData .toHashMap ());
125
- Task <Integer > sendTask = client .sendMessage (node .getId (), messageJSON .toString (), null );
113
+ Task <Integer > sendTask = messageClient .sendMessage (node .getId (), messageJSON .toString (), null );
126
114
sendTask .addOnSuccessListener (onSuccessListener );
127
115
sendTask .addOnFailureListener (onFailureListener );
128
116
} catch (Exception e ) {
@@ -155,28 +143,52 @@ public void onMessageReceived(MessageEvent messageEvent) {
155
143
}
156
144
}
157
145
158
- public static ReactApplicationContext getReactContext () {
159
- return reactContext ;
160
- }
161
-
162
146
@ Override
163
147
public void onHostResume () {
164
- if (client != null ) {
165
- Log .d (TAG , ADD_CLIENT );
166
- client .addListener (this );
148
+ if (messageClient != null && !isListenerAdded ) {
149
+ Log .d (TAG , "Adding listener on host resume" );
150
+ messageClient .addListener (this );
151
+ isListenerAdded = true ;
167
152
}
168
153
}
169
154
170
155
@ Override
171
156
public void onHostPause () {
172
- // Log.d(TAG, REMOVE_CLIENT);
173
- // removed this to allow to send updates when app is in the background
174
- // client.removeListener(this);
157
+ Log .d (TAG , "onHostPause: leaving listener active for background events" );
175
158
}
176
159
177
160
@ Override
178
161
public void onHostDestroy () {
179
- Log .d (TAG , REMOVE_CLIENT );
180
- client .removeListener (this );
162
+ if (messageClient != null && isListenerAdded ) {
163
+ Log .d (TAG , "Removing listener on host destroy" );
164
+ messageClient .removeListener (this );
165
+ isListenerAdded = false ;
166
+ }
167
+ }
168
+
169
+ private List <Node > retrieveNodes (Callback errorCb ) {
170
+ try {
171
+ int result = GoogleApiAvailability .getInstance ().isGooglePlayServicesAvailable (getReactContext ());
172
+ ConnectionResult connectionResult = new ConnectionResult (result );
173
+ if (!connectionResult .isSuccess ()) {
174
+ errorCb .invoke ( MISSING_GOOGLE_PLAY_SERVICES + connectionResult .getErrorMessage ());
175
+ return null ;
176
+ }
177
+ NodeClient nodeClient = Wearable .getNodeClient (getReactContext ());
178
+ try {
179
+ Tasks .await (GoogleApiAvailability .getInstance ().checkApiAvailability (nodeClient ));
180
+ } catch (Exception e ) {
181
+ errorCb .invoke (INSTALL_GOOGLE_PLAY_WEARABLE + e );
182
+ return null ;
183
+ }
184
+ return Tasks .await (nodeClient .getConnectedNodes ());
185
+ } catch (Exception e ) {
186
+ errorCb .invoke (RETRIEVE_NODES_FAILED + e );
187
+ return null ;
188
+ }
189
+ }
190
+
191
+ private static ReactApplicationContext getReactContext () {
192
+ return reactContext ;
181
193
}
182
194
}
0 commit comments