5
5
import android .bluetooth .BluetoothAdapter ;
6
6
import android .content .Intent ;
7
7
import android .content .pm .PackageManager ;
8
- import android .net .Uri ;
9
8
import android .os .Build .VERSION ;
10
9
import android .os .Build .VERSION_CODES ;
11
10
import android .support .annotation .Nullable ;
12
11
import android .support .v4 .app .ActivityCompat ;
13
- import android .widget .Toast ;
14
12
import com .facebook .react .bridge .Arguments ;
13
+ import com .facebook .react .bridge .Callback ;
15
14
import com .facebook .react .bridge .ReactApplicationContext ;
16
15
import com .facebook .react .bridge .ReactContextBaseJavaModule ;
17
16
import com .facebook .react .bridge .ReactMethod ;
17
+ import com .facebook .react .bridge .WritableArray ;
18
18
import com .facebook .react .bridge .WritableMap ;
19
+ import com .facebook .react .bridge .WritableNativeArray ;
20
+ import com .facebook .react .bridge .WritableNativeMap ;
19
21
import com .facebook .react .modules .core .DeviceEventManagerModule ;
22
+ import com .google .gson .GsonBuilder ;
20
23
import com .mybubbles .sdk .BuildConfig ;
21
24
import com .mybubbles .sdk .activities .ActivityAllowBluetooth ;
22
25
import com .mybubbles .sdk .instance .MyBubblesSDK ;
29
32
import com .mybubbles .sdk .observables .UniqueIdObservable ;
30
33
import com .mybubbles .sdk .utils .Const ;
31
34
import com .mybubbles .sdk .utils .ML ;
32
- import java .util .HashMap ;
35
+ import java .util .Iterator ;
33
36
import java .util .List ;
34
- import java .util .Map ;
35
37
import java .util .Observable ;
36
38
import java .util .Observer ;
37
39
import org .json .JSONArray ;
@@ -56,13 +58,16 @@ public class RNBubblesReactBridgeModule extends ReactContextBaseJavaModule {
56
58
}
57
59
}
58
60
59
- private static final String DURATION_SHORT_KEY = "SHORT" ;
60
- private static final String DURATION_LONG_KEY = "LONG" ;
61
-
62
- public static boolean isReactUpToDate = false ;
61
+ private static final int CALLBACK_CODE_JSON_EXCEPTION = 0 ;
62
+ private static final int CALLBACK_CODE_UNKNOWN_SERVICE = 1 ;
63
+ private static final int CALLBACK_CODE_BRIDGE_VERSION = 2 ;
64
+ private static final int CALLBACK_CODE_BLUETOOTH_ERROR = 3 ;
65
+ private static final int CALLBACK_CODE_BLUETOOTH_ON = 4 ;
63
66
64
67
private final ReactApplicationContext reactContext ;
65
68
69
+ public static boolean isReactUpToDate = false ;
70
+
66
71
public RNBubblesReactBridgeModule (ReactApplicationContext reactContext ) {
67
72
super (reactContext );
68
73
this .reactContext = reactContext ;
@@ -78,15 +83,6 @@ public String getName() {
78
83
return "BubblesReactBridge" ;
79
84
}
80
85
81
- // Optional
82
- @ Override
83
- public Map <String , Object > getConstants () {
84
- final Map <String , Object > constants = new HashMap <>();
85
- constants .put (DURATION_SHORT_KEY , Toast .LENGTH_SHORT );
86
- constants .put (DURATION_LONG_KEY , Toast .LENGTH_LONG );
87
- return constants ;
88
- }
89
-
90
86
////////////////////////////////////////////////////////////////////////////////////////////////////
91
87
///////////////////////////////// CALL FROM JAVASCRIPT TO PHONE OS /////////////////////////////////
92
88
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -102,15 +98,13 @@ public void log(String data) {
102
98
}
103
99
104
100
@ ReactMethod
105
- public void getBeaconsAround () {
101
+ public void getBeaconsAround (Callback callback ) {
106
102
log ("getBeaconsAround" );
107
103
108
- List <MyBeacon > beaconsList = MyBubblesSDK .getInstance ().getCurrentBeacons ();
109
-
110
- String ret ;
111
104
try {
112
105
JSONObject result = new JSONObject ();
113
106
JSONArray beacons = new JSONArray ();
107
+ List <MyBeacon > beaconsList = MyBubblesSDK .getInstance ().getCurrentBeacons ();
114
108
for (int i = 0 ; i < beaconsList .size (); i ++) {
115
109
MyBeacon beacon = beaconsList .get (i );
116
110
JSONObject beaconJson = new JSONObject ();
@@ -121,18 +115,10 @@ public void getBeaconsAround() {
121
115
beacons .put (beaconJson );
122
116
}
123
117
result .put ("beacons" , beacons );
124
- result .put ("success" , true );
125
- ret = result .toString ();
118
+ callback .invoke (null , result .toString ());
126
119
} catch (JSONException e ) {
127
- ret = "{'success':false}" ;
120
+ callback . invoke ( createRejectCallback ( CALLBACK_CODE_JSON_EXCEPTION , e . getMessage ()), null ) ;
128
121
}
129
-
130
- log ("list : " + ret );
131
-
132
- WritableMap params = Arguments .createMap ();
133
- params .putString ("beaconsList" , ret );
134
-
135
- sendEvent ("getBeaconsAround" , params );
136
122
}
137
123
138
124
@ ReactMethod
@@ -146,37 +132,20 @@ public void closeService() {
146
132
}
147
133
148
134
@ ReactMethod
149
- public void openURI (String uri ) {
150
- log ("openURI" );
151
-
152
- ML .e (TAG , "jsonData : " + uri );
135
+ public void getBluetoothState (Callback callback ) {
136
+ log ("getBluetoothState" );
153
137
154
138
try {
155
- JSONObject jsonData = new JSONObject (uri );
156
- uri = jsonData .getString ("uri" );
139
+ JSONObject result = new JSONObject ();
140
+ result .put ("isActivated" , getPhoneBluetoothState ());
141
+ callback .invoke (null , result .toString ());
157
142
} catch (JSONException e ) {
158
- return ;
143
+ callback . invoke ( createRejectCallback ( CALLBACK_CODE_JSON_EXCEPTION , e . getMessage ()), null ) ;
159
144
}
160
-
161
- // Otherwise, the link is not for a page on our Service, so launch another Activity that handles URLs
162
- Intent intent = new Intent (Intent .ACTION_VIEW , Uri .parse (uri ));
163
- Activity activity = getCurrentActivity ();
164
- if (activity != null ) {
165
- activity .startActivity (intent );
166
- }
167
- }
168
-
169
- @ ReactMethod
170
- public void getBluetoothState () {
171
- log ("getBluetoothState" );
172
-
173
- WritableMap params = Arguments .createMap ();
174
- params .putBoolean ("isActivated" , getPhoneBluetoothState ());
175
- sendEvent ("getBluetoothState" , params );
176
145
}
177
146
178
147
@ ReactMethod
179
- public void getLocalizationPermissionState () {
148
+ public void getLocalizationPermissionState (Callback callback ) {
180
149
log ("getLocalizationPermissionState" );
181
150
182
151
boolean isAuthorized = true ;
@@ -185,9 +154,11 @@ public void getLocalizationPermissionState() {
185
154
isAuthorized = false ;
186
155
}
187
156
188
- WritableMap params = Arguments .createMap ();
189
- params .putBoolean ("isAuthorized" , isAuthorized );
190
- sendEvent ("getLocalizationPermissionState" , params );
157
+ try {
158
+ callback .invoke (null , new JSONObject ().put ("isAuthorized" , isAuthorized ).toString ());
159
+ } catch (JSONException e ) {
160
+ callback .invoke (createRejectCallback (CALLBACK_CODE_JSON_EXCEPTION , e .getMessage ()), null );
161
+ }
191
162
}
192
163
193
164
@ ReactMethod
@@ -216,11 +187,9 @@ public void askForLocalizationPermission() {
216
187
}
217
188
218
189
@ ReactMethod
219
- public void getServices () {
190
+ public void getServices (Callback callback ) {
220
191
log ("getServices" );
221
192
222
- WritableMap params = Arguments .createMap ();
223
-
224
193
try {
225
194
JSONObject result = new JSONObject ();
226
195
JSONArray services = new JSONArray ();
@@ -246,13 +215,11 @@ public void getServices() {
246
215
services .put (jsonService );
247
216
248
217
result .put ("services" , services );
249
- result .put ("success" , true );
250
- params .putString ("result" , result .toString ());
218
+ callback .invoke (null , result .toString ());
251
219
252
220
} catch (JSONException e ) {
253
- params . putString ( "result" , DEFAULT_FAILED_HANDLER_RETURN );
221
+ callback . invoke ( createRejectCallback ( CALLBACK_CODE_JSON_EXCEPTION , e . getMessage ()), null );
254
222
}
255
- sendEvent ("getServices" , params );
256
223
}
257
224
258
225
@ ReactMethod
@@ -263,22 +230,19 @@ public void fetchServices() {
263
230
}
264
231
265
232
@ ReactMethod
266
- public void openService (String serviceId ) {
233
+ public void openService (String serviceId , Callback callback ) {
267
234
log ("openService" );
268
235
269
236
log ("jsonData : " + serviceId );
270
237
271
- WritableMap params = Arguments .createMap ();
272
238
String ret ;
273
239
274
240
String id ;
275
241
try {
276
242
JSONObject jsonData = new JSONObject (serviceId );
277
243
id = jsonData .getString ("service_id" );
278
244
} catch (JSONException e ) {
279
- ret = createFormattableFailedReturn (e .getMessage ());
280
- params .putString ("callback" , ret );
281
- sendEvent ("openService" , params );
245
+ callback .invoke (createRejectCallback (CALLBACK_CODE_JSON_EXCEPTION , e .getMessage ()), null );
282
246
return ;
283
247
}
284
248
@@ -290,9 +254,7 @@ public void openService(String serviceId) {
290
254
}
291
255
}
292
256
if (!flag ) {
293
- ret = createFormattableFailedReturn ("unknown service" );
294
- params .putString ("callback" , ret );
295
- sendEvent ("openService" , params );
257
+ callback .invoke (createRejectCallback (CALLBACK_CODE_UNKNOWN_SERVICE , "unknown service" ), null );
296
258
return ;
297
259
}
298
260
@@ -302,25 +264,44 @@ public void openService(String serviceId) {
302
264
}
303
265
304
266
ret = DEFAULT_SUCCESS_HANDLER_RETURN ;
305
- params .putString ("callback" , ret );
306
- sendEvent ("openService" , params );
267
+ callback .invoke (null , ret );
307
268
}
308
269
309
270
@ ReactMethod
310
- public void getVersion () {
271
+ public void getVersion (Callback callback ) {
311
272
log ("getVersion" );
312
273
313
- WritableMap params = Arguments .createMap ();
314
- params .putString ("version" , BuildConfig .BRIDGE_VERSION );
315
- sendEvent ("getVersion" , params );
274
+ String version = BuildConfig .BRIDGE_VERSION ;
275
+ if (ML .isValidStr (version )) {
276
+ try {
277
+ callback .invoke (null , new JSONObject ().put ("version" , version ).toString ());
278
+ } catch (JSONException e ) {
279
+ callback .invoke (createRejectCallback (CALLBACK_CODE_JSON_EXCEPTION , e .getMessage ()), null );
280
+ }
281
+ } else {
282
+ callback .invoke (createRejectCallback (CALLBACK_CODE_BRIDGE_VERSION , "bridge version not found" ), null );
283
+ }
316
284
}
317
285
318
-
319
286
@ ReactMethod
320
- public void enableBluetooth () {
287
+ public void enableBluetooth (Callback callback ) {
321
288
log ("enableBluetooth" );
322
289
323
- MyBubblesSDK .getInstance ().enableBluetooth ();
290
+ BluetoothAdapter bluetoothAdapter = BluetoothAdapter .getDefaultAdapter ();
291
+ if (bluetoothAdapter != null ) {
292
+ if (!bluetoothAdapter .isEnabled ()) {
293
+ bluetoothAdapter .enable ();
294
+ try {
295
+ callback .invoke (null , new JSONObject ().put ("enabled" , true ).toString ());
296
+ } catch (JSONException e ) {
297
+ callback .invoke (createRejectCallback (CALLBACK_CODE_JSON_EXCEPTION , e .getMessage ()), null );
298
+ }
299
+ return ;
300
+ }
301
+ callback .invoke (createRejectCallback (CALLBACK_CODE_BLUETOOTH_ON , "bluetooth is already activated" ), null );
302
+ return ;
303
+ }
304
+ callback .invoke (createRejectCallback (CALLBACK_CODE_BLUETOOTH_ERROR , "bluetooth activation failed" ), null );
324
305
}
325
306
326
307
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -446,6 +427,11 @@ private void sendEvent(String eventName, @Nullable WritableMap params) {
446
427
.emit (eventName , params );
447
428
}
448
429
430
+ private boolean getPhoneBluetoothState () {
431
+ final BluetoothAdapter adapter = BluetoothAdapter .getDefaultAdapter ();
432
+ return adapter != null && adapter .isEnabled ();
433
+ }
434
+
449
435
private String createFormattableFailedReturn (final String message ) {
450
436
try {
451
437
return FORMATTABLE_FAILED_HANDLER_RETURN .put ("error" , message ).toString ();
@@ -455,8 +441,77 @@ private String createFormattableFailedReturn(final String message) {
455
441
return null ;
456
442
}
457
443
458
- private boolean getPhoneBluetoothState () {
459
- final BluetoothAdapter adapter = BluetoothAdapter .getDefaultAdapter ();
460
- return adapter != null && adapter .isEnabled ();
444
+ private WritableMap createRejectCallback (int code , String message ) {
445
+ ErrorObject errorObject = new ErrorObject (code , message );
446
+ WritableMap errorMap = null ;
447
+ try {
448
+ errorMap = convertJsonToMap (new JSONObject (errorObject .toJsonString ()));
449
+ } catch (JSONException e ) {
450
+ e .printStackTrace ();
451
+ }
452
+ return errorMap ;
453
+ }
454
+
455
+ private class ErrorObject {
456
+
457
+ private int code ;
458
+ private String message ;
459
+
460
+ public ErrorObject (final int code , final String message ) {
461
+ this .code = code ;
462
+ this .message = message ;
463
+ }
464
+
465
+ public String toJsonString () {
466
+ return new GsonBuilder ().create ().toJson (this );
467
+ }
468
+ }
469
+
470
+ private static WritableMap convertJsonToMap (JSONObject jsonObject ) throws JSONException {
471
+ WritableMap map = new WritableNativeMap ();
472
+ Iterator <String > iterator = jsonObject .keys ();
473
+ while (iterator .hasNext ()) {
474
+ String key = iterator .next ();
475
+ Object value = jsonObject .get (key );
476
+ if (value instanceof JSONObject ) {
477
+ map .putMap (key , convertJsonToMap ((JSONObject ) value ));
478
+ } else if (value instanceof JSONArray ) {
479
+ map .putArray (key , convertJsonToArray ((JSONArray ) value ));
480
+ } else if (value instanceof Boolean ) {
481
+ map .putBoolean (key , (Boolean ) value );
482
+ } else if (value instanceof Integer ) {
483
+ map .putInt (key , (Integer ) value );
484
+ } else if (value instanceof Double ) {
485
+ map .putDouble (key , (Double ) value );
486
+ } else if (value instanceof String ) {
487
+ map .putString (key , (String ) value );
488
+ } else {
489
+ map .putString (key , value .toString ());
490
+ }
491
+ }
492
+ return map ;
493
+ }
494
+
495
+ private static WritableArray convertJsonToArray (JSONArray jsonArray ) throws JSONException {
496
+ WritableArray array = new WritableNativeArray ();
497
+ for (int i = 0 ; i < jsonArray .length (); i ++) {
498
+ Object value = jsonArray .get (i );
499
+ if (value instanceof JSONObject ) {
500
+ array .pushMap (convertJsonToMap ((JSONObject ) value ));
501
+ } else if (value instanceof JSONArray ) {
502
+ array .pushArray (convertJsonToArray ((JSONArray ) value ));
503
+ } else if (value instanceof Boolean ) {
504
+ array .pushBoolean ((Boolean ) value );
505
+ } else if (value instanceof Integer ) {
506
+ array .pushInt ((Integer ) value );
507
+ } else if (value instanceof Double ) {
508
+ array .pushDouble ((Double ) value );
509
+ } else if (value instanceof String ) {
510
+ array .pushString ((String ) value );
511
+ } else {
512
+ array .pushString (value .toString ());
513
+ }
514
+ }
515
+ return array ;
461
516
}
462
517
}
0 commit comments