Skip to content

Commit f29250c

Browse files
committed
resolve the problems found in #15 #14
resolve the problems found in #15 #14
1 parent 3762ad3 commit f29250c

File tree

4 files changed

+90
-59
lines changed

4 files changed

+90
-59
lines changed

android/build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ repositories {
5353
}
5454

5555
dependencies {
56-
implementation 'com.facebook.react:react-native:0.55+'
56+
compile fileTree(dir: 'libs', include: ['*.jar'])
57+
implementation 'com.facebook.react:react-native:+' // From node_modules
5758
implementation group: 'com.android.support', name: 'support-v4', version: '27.0.0'
5859
implementation "com.google.zxing:core:3.3.0"
5960
}

android/src/main/java/cn/jystudio/bluetooth/RNBluetoothManagerModule.java

+80-56
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class RNBluetoothManagerModule extends ReactContextBaseJavaModule
4040
public static final String EVENT_CONNECTION_LOST = "EVENT_CONNECTION_LOST";
4141
public static final String EVENT_UNABLE_CONNECT = "EVENT_UNABLE_CONNECT";
4242
public static final String EVENT_CONNECTED = "EVENT_CONNECTED";
43+
public static final String EVENT_BLUETOOTH_NOT_SUPPORT = "EVENT_BLUETOOTH_NOT_SUPPORT";
4344

4445

4546
// Intent request codes
@@ -83,16 +84,6 @@ public RNBluetoothManagerModule(ReactApplicationContext reactContext, BluetoothS
8384
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
8485
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
8586
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-
}
9687
}
9788

9889
@Override
@@ -106,23 +97,41 @@ Map<String, Object> getConstants() {
10697
constants.put(EVENT_CONNECTION_LOST, EVENT_CONNECTION_LOST);
10798
constants.put(EVENT_UNABLE_CONNECT, EVENT_UNABLE_CONNECT);
10899
constants.put(EVENT_CONNECTED, EVENT_CONNECTED);
100+
constants.put(EVENT_BLUETOOTH_NOT_SUPPORT, EVENT_BLUETOOTH_NOT_SUPPORT);
109101
constants.put(DEVICE_NAME, DEVICE_NAME);
102+
constants.put(EVENT_BLUETOOTH_NOT_SUPPORT, EVENT_BLUETOOTH_NOT_SUPPORT);
110103
return constants;
111104
}
112105

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+
113119

114120
@ReactMethod
115121
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
119128
Intent enableIntent = new Intent(
120129
BluetoothAdapter.ACTION_REQUEST_ENABLE);
121130
promiseMap.put(PROMISE_ENABLE_BT, promise);
122131
this.reactContext.startActivityForResult(enableIntent, REQUEST_ENABLE_BT, Bundle.EMPTY);
123132
} else {
124133
WritableArray pairedDeivce =Arguments.createArray();
125-
Set<BluetoothDevice> boundDevices = mBluetoothAdapter.getBondedDevices();
134+
Set<BluetoothDevice> boundDevices = adapter.getBondedDevices();
126135
for (BluetoothDevice d : boundDevices) {
127136
try {
128137
JSONObject obj = new JSONObject();
@@ -139,58 +148,70 @@ public void enableBluetooth(final Promise promise) {
139148

140149
@ReactMethod
141150
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());
144159
}
145-
promise.resolve(!mBluetoothAdapter.isEnabled() || mBluetoothAdapter.disable());
146160
}
147161

148162
@ReactMethod
149163
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());
151166
}
152167

153168
@ReactMethod
154169
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+
}
163182

164183

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+
}
176196
}
177-
}
178197

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+
}
187207
}
188208
}
189209

190210
@ReactMethod
191211
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);
194215
promiseMap.put(PROMISE_CONNECT, promise);
195216
mService.connect(device);
196217
} else {
@@ -201,8 +222,9 @@ public void connect(String address, final Promise promise) {
201222

202223
@ReactMethod
203224
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);
206228
this.unpairDevice(device);
207229
promise.resolve(address);
208230
} else {
@@ -223,8 +245,9 @@ private void unpairDevice(BluetoothDevice device) {
223245

224246
private void cancelDisCovery() {
225247
try {
226-
if (mBluetoothAdapter.isDiscovering()) {
227-
mBluetoothAdapter.cancelDiscovery();
248+
BluetoothAdapter adapter = this.getBluetoothAdapter();
249+
if (adapter!=null && adapter.isDiscovering()) {
250+
adapter.cancelDiscovery();
228251
}
229252
Log.d(TAG, "Discover canceled");
230253
} catch (Exception e) {
@@ -235,6 +258,7 @@ private void cancelDisCovery() {
235258

236259
@Override
237260
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
261+
BluetoothAdapter adapter = this.getBluetoothAdapter();
238262
Log.d(TAG, "onActivityResult " + resultCode);
239263
switch (requestCode) {
240264
case REQUEST_CONNECT_DEVICE: {
@@ -244,8 +268,8 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
244268
String address = data.getExtras().getString(
245269
EXTRA_DEVICE_ADDRESS);
246270
// Get the BLuetoothDevice object
247-
if (BluetoothAdapter.checkBluetoothAddress(address)) {
248-
BluetoothDevice device = mBluetoothAdapter
271+
if (adapter!=null && BluetoothAdapter.checkBluetoothAddress(address)) {
272+
BluetoothDevice device = adapter
249273
.getRemoteDevice(address);
250274
// Attempt to connect to the device
251275
mService.connect(device);
@@ -258,9 +282,9 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
258282
// When the request to enable Bluetooth returns
259283
if (resultCode == Activity.RESULT_OK && promise != null) {
260284
// Bluetooth is now enabled, so set up a session
261-
if(mBluetoothAdapter!=null){
285+
if(adapter!=null){
262286
WritableArray pairedDeivce =Arguments.createArray();
263-
Set<BluetoothDevice> boundDevices = mBluetoothAdapter.getBondedDevices();
287+
Set<BluetoothDevice> boundDevices = adapter.getBondedDevices();
264288
for (BluetoothDevice d : boundDevices) {
265289
try {
266290
JSONObject obj = new JSONObject();

examples/home.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {ActivityIndicator,
1414
NativeEventEmitter,
1515
Switch,
1616
TouchableOpacity,
17-
Dimensions} from 'react-native';
17+
Dimensions,
18+
ToastAndroid} from 'react-native';
1819
import {BluetoothEscposPrinter, BluetoothManager, BluetoothTscPrinter} from "../index";
1920
import EscPos from "./escpos";
2021
import Tsc from "./tsc";
@@ -79,6 +80,11 @@ export default class Home extends Component {
7980
boundAddress: ''
8081
});
8182
}
83+
));
84+
this._listeners.push(DeviceEventEmitter.addListener(
85+
BluetoothManager.EVENT_BLUETOOTH_NOT_SUPPORT, ()=> {
86+
ToastAndroid.show("Device Not Support Bluetooth !", ToastAndroid.LONG);
87+
}
8288
))
8389
}
8490
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-bluetooth-escpos-printer",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "React-Native plugin for the bluetooth ESC/POS printers.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)