@@ -40,6 +40,7 @@ public class RNBluetoothManagerModule extends ReactContextBaseJavaModule
40
40
public static final String EVENT_CONNECTION_LOST = "EVENT_CONNECTION_LOST" ;
41
41
public static final String EVENT_UNABLE_CONNECT = "EVENT_UNABLE_CONNECT" ;
42
42
public static final String EVENT_CONNECTED = "EVENT_CONNECTED" ;
43
+ public static final String EVENT_BLUETOOTH_NOT_SUPPORT = "EVENT_BLUETOOTH_NOT_SUPPORT" ;
43
44
44
45
45
46
// Intent request codes
@@ -83,16 +84,6 @@ public RNBluetoothManagerModule(ReactApplicationContext reactContext, BluetoothS
83
84
IntentFilter filter = new IntentFilter (BluetoothDevice .ACTION_FOUND );
84
85
filter .addAction (BluetoothAdapter .ACTION_DISCOVERY_FINISHED );
85
86
this .reactContext .registerReceiver (discoverReceiver , filter );
86
-
87
- // Get local Bluetooth adapter
88
- mBluetoothAdapter = BluetoothAdapter .getDefaultAdapter ();
89
-
90
-
91
- // If the adapter is null, then Bluetooth is not supported
92
- if (mBluetoothAdapter == null ) {
93
- Toast .makeText (this .reactContext , "Bluetooth is not available" ,
94
- Toast .LENGTH_LONG ).show ();
95
- }
96
87
}
97
88
98
89
@ Override
@@ -106,23 +97,41 @@ Map<String, Object> getConstants() {
106
97
constants .put (EVENT_CONNECTION_LOST , EVENT_CONNECTION_LOST );
107
98
constants .put (EVENT_UNABLE_CONNECT , EVENT_UNABLE_CONNECT );
108
99
constants .put (EVENT_CONNECTED , EVENT_CONNECTED );
100
+ constants .put (EVENT_BLUETOOTH_NOT_SUPPORT , EVENT_BLUETOOTH_NOT_SUPPORT );
109
101
constants .put (DEVICE_NAME , DEVICE_NAME );
102
+ constants .put (EVENT_BLUETOOTH_NOT_SUPPORT , EVENT_BLUETOOTH_NOT_SUPPORT );
110
103
return constants ;
111
104
}
112
105
106
+ private BluetoothAdapter getBluetoothAdapter (){
107
+ if (mBluetoothAdapter == null ){
108
+ // Get local Bluetooth adapter
109
+ mBluetoothAdapter = BluetoothAdapter .getDefaultAdapter ();
110
+ }
111
+ // If the adapter is null, then Bluetooth is not supported
112
+ if (mBluetoothAdapter == null ) {
113
+ emitRNEvent (EVENT_BLUETOOTH_NOT_SUPPORT , Arguments .createMap ());
114
+ }
115
+
116
+ return mBluetoothAdapter ;
117
+ }
118
+
113
119
114
120
@ ReactMethod
115
121
public void enableBluetooth (final Promise promise ) {
116
- // If Bluetooth is not on, request that it be enabled.
117
- // setupChat() will then be called during onActivityResult
118
- if (!mBluetoothAdapter .isEnabled ()) {
122
+ BluetoothAdapter adapter = this .getBluetoothAdapter ();
123
+ if (adapter == null ){
124
+ promise .reject (EVENT_BLUETOOTH_NOT_SUPPORT );
125
+ }else if (!adapter .isEnabled ()) {
126
+ // If Bluetooth is not on, request that it be enabled.
127
+ // setupChat() will then be called during onActivityResult
119
128
Intent enableIntent = new Intent (
120
129
BluetoothAdapter .ACTION_REQUEST_ENABLE );
121
130
promiseMap .put (PROMISE_ENABLE_BT , promise );
122
131
this .reactContext .startActivityForResult (enableIntent , REQUEST_ENABLE_BT , Bundle .EMPTY );
123
132
} else {
124
133
WritableArray pairedDeivce =Arguments .createArray ();
125
- Set <BluetoothDevice > boundDevices = mBluetoothAdapter .getBondedDevices ();
134
+ Set <BluetoothDevice > boundDevices = adapter .getBondedDevices ();
126
135
for (BluetoothDevice d : boundDevices ) {
127
136
try {
128
137
JSONObject obj = new JSONObject ();
@@ -139,58 +148,70 @@ public void enableBluetooth(final Promise promise) {
139
148
140
149
@ ReactMethod
141
150
public void disableBluetooth (final Promise promise ) {
142
- if (mService != null && mService .getState () != BluetoothService .STATE_NONE ) {
143
- mService .stop ();
151
+ BluetoothAdapter adapter = this .getBluetoothAdapter ();
152
+ if (adapter == null ){
153
+ promise .resolve (true );
154
+ }else {
155
+ if (mService != null && mService .getState () != BluetoothService .STATE_NONE ) {
156
+ mService .stop ();
157
+ }
158
+ promise .resolve (!adapter .isEnabled () || adapter .disable ());
144
159
}
145
- promise .resolve (!mBluetoothAdapter .isEnabled () || mBluetoothAdapter .disable ());
146
160
}
147
161
148
162
@ ReactMethod
149
163
public void isBluetoothEnabled (final Promise promise ) {
150
- promise .resolve (mBluetoothAdapter !=null && mBluetoothAdapter .isEnabled ());
164
+ BluetoothAdapter adapter = this .getBluetoothAdapter ();
165
+ promise .resolve (adapter !=null && adapter .isEnabled ());
151
166
}
152
167
153
168
@ ReactMethod
154
169
public void scanDevices (final Promise promise ) {
155
- cancelDisCovery ();
156
- int permissionChecked = ContextCompat .checkSelfPermission (reactContext , android .Manifest .permission .ACCESS_COARSE_LOCATION );
157
- if (permissionChecked == PackageManager .PERMISSION_DENIED ) {
158
- // // TODO: 2018/9/21
159
- ActivityCompat .requestPermissions (reactContext .getCurrentActivity (),
160
- new String []{android .Manifest .permission .ACCESS_COARSE_LOCATION },
161
- 1 );
162
- }
170
+ BluetoothAdapter adapter = this .getBluetoothAdapter ();
171
+ if (adapter == null ){
172
+ promise .reject (EVENT_BLUETOOTH_NOT_SUPPORT );
173
+ }else {
174
+ cancelDisCovery ();
175
+ int permissionChecked = ContextCompat .checkSelfPermission (reactContext , android .Manifest .permission .ACCESS_COARSE_LOCATION );
176
+ if (permissionChecked == PackageManager .PERMISSION_DENIED ) {
177
+ // // TODO: 2018/9/21
178
+ ActivityCompat .requestPermissions (reactContext .getCurrentActivity (),
179
+ new String []{android .Manifest .permission .ACCESS_COARSE_LOCATION },
180
+ 1 );
181
+ }
163
182
164
183
165
- pairedDeivce = new JSONArray ();
166
- foundDevice = new JSONArray ();
167
- Set <BluetoothDevice > boundDevices = mBluetoothAdapter .getBondedDevices ();
168
- for (BluetoothDevice d : boundDevices ) {
169
- try {
170
- JSONObject obj = new JSONObject ();
171
- obj .put ("name" , d .getName ());
172
- obj .put ("address" , d .getAddress ());
173
- pairedDeivce .put (obj );
174
- } catch (Exception e ) {
175
- //ignore.
184
+ pairedDeivce = new JSONArray ();
185
+ foundDevice = new JSONArray ();
186
+ Set <BluetoothDevice > boundDevices = adapter .getBondedDevices ();
187
+ for (BluetoothDevice d : boundDevices ) {
188
+ try {
189
+ JSONObject obj = new JSONObject ();
190
+ obj .put ("name" , d .getName ());
191
+ obj .put ("address" , d .getAddress ());
192
+ pairedDeivce .put (obj );
193
+ } catch (Exception e ) {
194
+ //ignore.
195
+ }
176
196
}
177
- }
178
197
179
- WritableMap params = Arguments .createMap ();
180
- params .putString ("devices" , pairedDeivce .toString ());
181
- emitRNEvent (EVENT_DEVICE_ALREADY_PAIRED , params );
182
- if (!mBluetoothAdapter .startDiscovery ()) {
183
- promise .reject ("DISCOVER" , "NOT_STARTED" );
184
- cancelDisCovery ();
185
- } else {
186
- promiseMap .put (PROMISE_SCAN , promise );
198
+ WritableMap params = Arguments .createMap ();
199
+ params .putString ("devices" , pairedDeivce .toString ());
200
+ emitRNEvent (EVENT_DEVICE_ALREADY_PAIRED , params );
201
+ if (!adapter .startDiscovery ()) {
202
+ promise .reject ("DISCOVER" , "NOT_STARTED" );
203
+ cancelDisCovery ();
204
+ } else {
205
+ promiseMap .put (PROMISE_SCAN , promise );
206
+ }
187
207
}
188
208
}
189
209
190
210
@ ReactMethod
191
211
public void connect (String address , final Promise promise ) {
192
- if (mBluetoothAdapter .isEnabled ()) {
193
- BluetoothDevice device = mBluetoothAdapter .getRemoteDevice (address );
212
+ BluetoothAdapter adapter = this .getBluetoothAdapter ();
213
+ if (adapter !=null && adapter .isEnabled ()) {
214
+ BluetoothDevice device = adapter .getRemoteDevice (address );
194
215
promiseMap .put (PROMISE_CONNECT , promise );
195
216
mService .connect (device );
196
217
} else {
@@ -201,8 +222,9 @@ public void connect(String address, final Promise promise) {
201
222
202
223
@ ReactMethod
203
224
public void unpaire (String address ,final Promise promise ){
204
- if (mBluetoothAdapter .isEnabled ()) {
205
- BluetoothDevice device = mBluetoothAdapter .getRemoteDevice (address );
225
+ BluetoothAdapter adapter = this .getBluetoothAdapter ();
226
+ if (adapter !=null && adapter .isEnabled ()) {
227
+ BluetoothDevice device = adapter .getRemoteDevice (address );
206
228
this .unpairDevice (device );
207
229
promise .resolve (address );
208
230
} else {
@@ -223,8 +245,9 @@ private void unpairDevice(BluetoothDevice device) {
223
245
224
246
private void cancelDisCovery () {
225
247
try {
226
- if (mBluetoothAdapter .isDiscovering ()) {
227
- mBluetoothAdapter .cancelDiscovery ();
248
+ BluetoothAdapter adapter = this .getBluetoothAdapter ();
249
+ if (adapter !=null && adapter .isDiscovering ()) {
250
+ adapter .cancelDiscovery ();
228
251
}
229
252
Log .d (TAG , "Discover canceled" );
230
253
} catch (Exception e ) {
@@ -235,6 +258,7 @@ private void cancelDisCovery() {
235
258
236
259
@ Override
237
260
public void onActivityResult (Activity activity , int requestCode , int resultCode , Intent data ) {
261
+ BluetoothAdapter adapter = this .getBluetoothAdapter ();
238
262
Log .d (TAG , "onActivityResult " + resultCode );
239
263
switch (requestCode ) {
240
264
case REQUEST_CONNECT_DEVICE : {
@@ -244,8 +268,8 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
244
268
String address = data .getExtras ().getString (
245
269
EXTRA_DEVICE_ADDRESS );
246
270
// Get the BLuetoothDevice object
247
- if (BluetoothAdapter .checkBluetoothAddress (address )) {
248
- BluetoothDevice device = mBluetoothAdapter
271
+ if (adapter != null && BluetoothAdapter .checkBluetoothAddress (address )) {
272
+ BluetoothDevice device = adapter
249
273
.getRemoteDevice (address );
250
274
// Attempt to connect to the device
251
275
mService .connect (device );
@@ -258,9 +282,9 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
258
282
// When the request to enable Bluetooth returns
259
283
if (resultCode == Activity .RESULT_OK && promise != null ) {
260
284
// Bluetooth is now enabled, so set up a session
261
- if (mBluetoothAdapter !=null ){
285
+ if (adapter !=null ){
262
286
WritableArray pairedDeivce =Arguments .createArray ();
263
- Set <BluetoothDevice > boundDevices = mBluetoothAdapter .getBondedDevices ();
287
+ Set <BluetoothDevice > boundDevices = adapter .getBondedDevices ();
264
288
for (BluetoothDevice d : boundDevices ) {
265
289
try {
266
290
JSONObject obj = new JSONObject ();
0 commit comments