Skip to content

Commit 6d070a5

Browse files
Attempting to fix #23. Also did some overall code cleanup to avoid context leaks.
1 parent b3bc016 commit 6d070a5

File tree

8 files changed

+192
-151
lines changed

8 files changed

+192
-151
lines changed

app/src/main/java/com/devpaul/bluetoothutilitydemo/MainActivity.java

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -44,48 +44,36 @@ protected void onResume() {
4444
//object on the client side gets lost. Thus when send is called, nothing happens because it's
4545
//a different object.
4646
if(simpleBluetooth == null) {
47-
simpleBluetooth = new SimpleBluetooth(this, this);
47+
simpleBluetooth = new SimpleBluetooth(this, new SimpleBluetoothListener() {
48+
@Override
49+
public void onBluetoothDataReceived(byte[] bytes, String data) {
50+
//read the data coming in.
51+
Toast.makeText(MainActivity.this, "Data: " + data, Toast.LENGTH_SHORT).show();
52+
connectionState.setText("Data: " + data);
53+
isConnected = false;
54+
Log.w("SIMPLEBT", "Data received");
55+
}
56+
57+
@Override
58+
public void onDeviceConnected(BluetoothDevice device) {
59+
//a device is connected so you can now send stuff to it
60+
Toast.makeText(MainActivity.this, "Connected!", Toast.LENGTH_SHORT).show();
61+
connectionState.setText("Connected");
62+
isConnected = true;
63+
Log.w("SIMPLEBT", "Device connected");
64+
}
65+
66+
@Override
67+
public void onDeviceDisconnected(BluetoothDevice device) {
68+
// device was disconnected so connect it again?
69+
Toast.makeText(MainActivity.this, "Disconnected!", Toast.LENGTH_SHORT).show();
70+
connectionState.setText("Disconnected");
71+
Log.w("SIMPLEBT", "Device disconnected");
72+
}
73+
});
4874
}
4975
simpleBluetooth.initializeSimpleBluetooth();
5076
simpleBluetooth.setInputStreamType(BluetoothUtility.InputStreamType.BUFFERED);
51-
simpleBluetooth.setSimpleBluetoothListener(new SimpleBluetoothListener() {
52-
53-
@Override
54-
public void onBluetoothDataReceived(byte[] bytes, String data) {
55-
//read the data coming in.
56-
Toast.makeText(MainActivity.this, "Data: " + data, Toast.LENGTH_SHORT).show();
57-
connectionState.setText("Data: " + data);
58-
isConnected = false;
59-
Log.w("SIMPLEBT", "Data received");
60-
}
61-
62-
@Override
63-
public void onDeviceConnected(BluetoothDevice device) {
64-
//a device is connected so you can now send stuff to it
65-
Toast.makeText(MainActivity.this, "Connected!", Toast.LENGTH_SHORT).show();
66-
connectionState.setText("Connected");
67-
isConnected = true;
68-
Log.w("SIMPLEBT", "Device connected");
69-
}
70-
71-
@Override
72-
public void onDeviceDisconnected(BluetoothDevice device) {
73-
// device was disconnected so connect it again?
74-
Toast.makeText(MainActivity.this, "Disconnected!", Toast.LENGTH_SHORT).show();
75-
connectionState.setText("Disconnected");
76-
Log.w("SIMPLEBT", "Device disconnected");
77-
}
78-
79-
@Override
80-
public void onDiscoveryStarted() {
81-
82-
}
83-
84-
@Override
85-
public void onDiscoveryFinished() {
86-
87-
}
88-
});
8977
}
9078

9179
@Override

btutillib/src/main/java/com/devpaul/bluetoothutillib/SimpleBluetooth.java

Lines changed: 22 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22

33
import android.app.Activity;
44
import android.app.ProgressDialog;
5-
import android.bluetooth.BluetoothA2dp;
65
import android.bluetooth.BluetoothDevice;
76
import android.content.Context;
87
import android.content.Intent;
9-
import android.os.Message;
10-
import android.support.design.widget.Snackbar;
118

129
import com.devpaul.bluetoothutillib.broadcasts.BluetoothBroadcastReceiver;
1310
import com.devpaul.bluetoothutillib.broadcasts.BluetoothPairingReceiver;
1411
import com.devpaul.bluetoothutillib.broadcasts.BluetoothStateReceiver;
1512
import com.devpaul.bluetoothutillib.broadcasts.FoundDeviceReceiver;
1613
import com.devpaul.bluetoothutillib.dialogs.DeviceDialog;
1714
import com.devpaul.bluetoothutillib.handlers.BluetoothHandler;
15+
import com.devpaul.bluetoothutillib.handlers.DefaultBluetoothHandler;
1816
import com.devpaul.bluetoothutillib.utils.BluetoothUtility;
1917
import com.devpaul.bluetoothutillib.utils.SimpleBluetoothListener;
2018

@@ -102,11 +100,6 @@ public void onDeviceUnpaired(BluetoothDevice device) {
102100
*/
103101
private Context mContext;
104102

105-
/**
106-
* Reference to the calling activity.
107-
*/
108-
private Activity mActivity;
109-
110103
/**
111104
* {@code BluetoothUtility used by SimpleBluetooth}
112105
*/
@@ -157,20 +150,29 @@ public void onDeviceUnpaired(BluetoothDevice device) {
157150
*/
158151
private boolean connectWithService = false;
159152

153+
/**
154+
* Boolean for showing or hiding snackbars.
155+
*/
160156
private boolean shouldShowSnackbars = false;
161157

158+
/**
159+
* Default handler for Simple bluetooth.
160+
*/
161+
private BluetoothHandler mHandler;
162+
162163
/**
163164
* Constructor for {@code SimpleBluetooth}
164165
* Allows for easy handling for setting up connections and bluetooth servers to connect to.
165166
* @param context context from the calling activity
166-
* @param refActivity reference to the calling activity. Context and activity should match.
167167
*/
168-
public SimpleBluetooth(Context context, Activity refActivity) {
168+
public SimpleBluetooth(Context context, SimpleBluetoothListener listener) {
169169
//initialize fields.
170170
this.progressDialog = new ProgressDialog(context);
171171
this.mContext = context;
172-
this.mActivity = refActivity;
173-
this.bluetoothUtility = new BluetoothUtility(mContext, mActivity, mHandler);
172+
this.mListener = listener;
173+
this.mHandler = new DefaultBluetoothHandler(listener, context);
174+
this.mHandler.setShowSnackbars(shouldShowSnackbars);
175+
this.bluetoothUtility = new BluetoothUtility(mContext, mHandler);
174176
//register the state change receiver.
175177
this.curType = InputStreamType.NORMAL;
176178
this.bluetoothStateReceiver = BluetoothStateReceiver
@@ -186,20 +188,18 @@ public SimpleBluetooth(Context context, Activity refActivity) {
186188
* Constructor for {@code SimpleBluetooth} Use this constructor to provide your own custom bluetooth
187189
* handler.
188190
* @param context the context of the calling activity.
189-
* @param refActivity reference to the calling activity. Context and activity should match.
190191
* @param handler custom {@code BluetoothHandler} for bluetooth event call backs.
191192
*/
192-
public SimpleBluetooth(Context context, Activity refActivity, BluetoothHandler handler) {
193+
public SimpleBluetooth(Context context, BluetoothHandler handler) {
193194
//initialize fields.
194195
this.progressDialog = new ProgressDialog(context);
195196
this.mContext = context;
196-
this.mActivity = refActivity;
197197
this.customHandler = handler;
198198
this.curType = InputStreamType.NORMAL;
199199
//check the handler.
200200
if(customHandler == null) throw
201201
new NullPointerException("Custom BluetoothHandler cannot be null!");
202-
this.bluetoothUtility = new BluetoothUtility(mContext, mActivity, customHandler);
202+
this.bluetoothUtility = new BluetoothUtility(mContext, customHandler);
203203
//register the state change receiver.
204204
this.bluetoothStateReceiver = BluetoothStateReceiver
205205
.register(mContext, stateRecieverCallback);
@@ -232,51 +232,9 @@ public void setInputStreamType(InputStreamType type) {
232232
*/
233233
public void setShouldShowSnackbars(boolean show) {
234234
shouldShowSnackbars = show;
235+
mHandler.setShowSnackbars(show);
235236
bluetoothUtility.setShouldShowSnackbars(show);
236237
}
237-
/**
238-
* Default handler for Simple bluetooth.
239-
*/
240-
private BluetoothHandler mHandler = new BluetoothHandler() {
241-
@Override
242-
public void handleMessage(Message message) {
243-
switch (message.what) {
244-
case MESSAGE_READ:
245-
byte[] readBuf = (byte[]) message.obj;
246-
String readMessage = new String(readBuf);
247-
if(readBuf != null && readBuf.length > 0) {
248-
if(mListener != null)
249-
mListener.onBluetoothDataReceived(readBuf, readMessage);
250-
}
251-
break;
252-
case MESSAGE_WAIT_FOR_CONNECTION:
253-
if(progressDialog != null) {
254-
progressDialog.setTitle("");
255-
progressDialog.setMessage("Waiting...");
256-
progressDialog.show();
257-
}
258-
break;
259-
case MESSAGE_CONNECTION_MADE:
260-
if(progressDialog != null) {
261-
if(progressDialog.isShowing()) {
262-
progressDialog.dismiss();
263-
if(shouldShowSnackbars) {
264-
Snackbar.make(mActivity.findViewById(android.R.id.content), "Device connected.",
265-
Snackbar.LENGTH_SHORT).show();
266-
}
267-
}
268-
}
269-
break;
270-
case MESSAGE_A2DP_PROXY_RECEIVED:
271-
BluetoothA2dp bluetoothA2dp = (BluetoothA2dp) message.obj;
272-
bluetoothUtility.connectA2DPProxy(bluetoothA2dp, a2dpDevice);
273-
break;
274-
default:
275-
break;
276-
}
277-
278-
}
279-
};
280238

281239
/**
282240
* Method that must be called to set everything up for this class.
@@ -336,8 +294,10 @@ public void sendData(byte[] data) {
336294
* OnActivityResult.
337295
*/
338296
public void scan(int requestCode) {
339-
Intent deviceDialog = new Intent(mActivity, DeviceDialog.class);
340-
mActivity.startActivityForResult(deviceDialog, requestCode);
297+
Intent deviceDialog = new Intent(mContext, DeviceDialog.class);
298+
if(mContext instanceof Activity) {
299+
((Activity)mContext).startActivityForResult(deviceDialog, requestCode);
300+
}
341301
}
342302

343303
/**
@@ -418,12 +378,11 @@ public void connectToBluetoothServer(String macAddress) {
418378
public void connectToA2DPDevice(String deviceName) {
419379
if(!isInitialized) {
420380
throw new IllegalStateException("Must initialize before using any other method in class" +
421-
"SimpleBluetooth! Call initializeSimpleBluetooth()");
381+
"SimpleBluetooth. Call initializeSimpleBluetooth()");
422382
} else {
423383
a2dpDevice = bluetoothUtility.findDeviceByName(deviceName);
424384
bluetoothUtility.setUpA2DPConnection();
425385
}
426-
427386
}
428387

429388
/**

btutillib/src/main/java/com/devpaul/bluetoothutillib/abstracts/BaseBluetoothActivity.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
* - Connecting to a device.
1919
* - Receiving data from the device.
2020
* - Sending data to the device.
21-
*
2221
*/
2322
public abstract class BaseBluetoothActivity extends Activity {
2423

@@ -35,11 +34,10 @@ public abstract class BaseBluetoothActivity extends Activity {
3534

3635
@Override
3736
protected void onCreate(Bundle savedInstanceState) {
38-
simpleBluetooth = new SimpleBluetooth(this, this);
37+
simpleBluetooth = new SimpleBluetooth(this, getSimpleBluetoothListener());
3938
if(simpleBluetooth.initializeSimpleBluetooth()) {
4039
onBluetoothEnabled();
4140
}
42-
simpleBluetooth.setSimpleBluetoothListener(getSimpleBluetoothListener());
4341
super.onCreate(savedInstanceState);
4442
}
4543

@@ -98,5 +96,4 @@ public SimpleBluetooth getSimpleBluetooth() {
9896
return this.simpleBluetooth;
9997
}
10098

101-
10299
}

btutillib/src/main/java/com/devpaul/bluetoothutillib/abstracts/SupportBaseBluetoothActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ public abstract class SupportBaseBluetoothActivity extends AppCompatActivity {
3333

3434
@Override
3535
protected void onCreate(Bundle savedInstanceState) {
36-
simpleBluetooth = new SimpleBluetooth(this, this);
36+
simpleBluetooth = new SimpleBluetooth(this, getSimpleBluetoothListener());
3737
if(simpleBluetooth.initializeSimpleBluetooth()) {
3838
onBluetoothEnabled();
3939
}
40-
simpleBluetooth.setSimpleBluetoothListener(getSimpleBluetoothListener());
4140
super.onCreate(savedInstanceState);
4241
}
4342

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
11
package com.devpaul.bluetoothutillib.handlers;
22

3+
import android.app.Activity;
4+
import android.app.ProgressDialog;
5+
import android.content.Context;
36
import android.os.Handler;
4-
import android.os.Message;
7+
8+
import com.devpaul.bluetoothutillib.utils.SimpleBluetoothListener;
59

610
/**
711
* Created by Paul Tsouchlos
812
* A handler for receiving messages from the bluetooth device.
913
*/
10-
public abstract class BluetoothHandler extends Handler {
14+
public class BluetoothHandler extends Handler {
1115
public static final int MESSAGE_READ = 121;
1216
public static final int MESSAGE_WAIT_FOR_CONNECTION = 143;
1317
public static final int MESSAGE_CONNECTION_MADE = 155;
1418
public static final int MESSAGE_A2DP_PROXY_RECEIVED = 157;
15-
public abstract void handleMessage(Message message);
19+
public SimpleBluetoothListener mListener;
20+
public Activity mActivity;
21+
public ProgressDialog dialog;
22+
public boolean shouldShowSnackbars;
23+
24+
/**
25+
* Bluetooth Handler class for handling messages from the bluetooth device.
26+
* @param listener listener for simple bluetooth.
27+
* @param context reference context.
28+
*/
29+
public BluetoothHandler(SimpleBluetoothListener listener, Context context) {
30+
this.mListener = listener;
31+
if(context instanceof Activity) {
32+
this.mActivity = (Activity) context;
33+
}
34+
dialog = new ProgressDialog(context);
35+
}
36+
37+
public void setShowSnackbars(boolean show) {
38+
this.shouldShowSnackbars = show;
39+
}
1640
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.devpaul.bluetoothutillib.handlers;
2+
3+
import android.bluetooth.BluetoothA2dp;
4+
import android.content.Context;
5+
import android.os.Message;
6+
import android.support.design.widget.Snackbar;
7+
8+
import com.devpaul.bluetoothutillib.utils.SimpleBluetoothListener;
9+
10+
/**
11+
* Created by Paul on 3/21/2016.
12+
*
13+
* Default handler for SimpleBluetooth.
14+
*/
15+
public class DefaultBluetoothHandler extends BluetoothHandler {
16+
17+
/**
18+
* Default bluetooth handler for SimpleBluetooth. This should work fine for most cases.
19+
* Override if you need more message.what options.
20+
* @param listener the simple bluetooth listener.
21+
* @param context reference context.
22+
*/
23+
public DefaultBluetoothHandler(SimpleBluetoothListener listener, Context context) {
24+
super(listener, context);
25+
}
26+
27+
@Override
28+
public void handleMessage(Message message) {
29+
switch (message.what) {
30+
case MESSAGE_READ:
31+
byte[] readBuf = (byte[]) message.obj;
32+
String readMessage = new String(readBuf);
33+
if(readBuf.length > 0) {
34+
if(mListener != null)
35+
mListener.onBluetoothDataReceived(readBuf, readMessage);
36+
}
37+
break;
38+
case MESSAGE_WAIT_FOR_CONNECTION:
39+
if(dialog != null) {
40+
dialog.setTitle("");
41+
dialog.setMessage("Waiting...");
42+
dialog.show();
43+
}
44+
break;
45+
case MESSAGE_CONNECTION_MADE:
46+
if(dialog != null) {
47+
if(dialog.isShowing()) {
48+
dialog.dismiss();
49+
if(shouldShowSnackbars && mActivity != null) {
50+
Snackbar.make(mActivity.findViewById(android.R.id.content), "Device connected.",
51+
Snackbar.LENGTH_SHORT).show();
52+
}
53+
}
54+
}
55+
break;
56+
case MESSAGE_A2DP_PROXY_RECEIVED:
57+
BluetoothA2dp device = (BluetoothA2dp) message.obj;
58+
if(device != null && mListener != null) {
59+
mListener.onBluetoothA2DPRequested(device);
60+
}
61+
break;
62+
default:
63+
break;
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)