2
2
3
3
import android .app .Activity ;
4
4
import android .app .ProgressDialog ;
5
- import android .bluetooth .BluetoothA2dp ;
6
5
import android .bluetooth .BluetoothDevice ;
7
6
import android .content .Context ;
8
7
import android .content .Intent ;
9
- import android .os .Message ;
10
- import android .support .design .widget .Snackbar ;
11
8
12
9
import com .devpaul .bluetoothutillib .broadcasts .BluetoothBroadcastReceiver ;
13
10
import com .devpaul .bluetoothutillib .broadcasts .BluetoothPairingReceiver ;
14
11
import com .devpaul .bluetoothutillib .broadcasts .BluetoothStateReceiver ;
15
12
import com .devpaul .bluetoothutillib .broadcasts .FoundDeviceReceiver ;
16
13
import com .devpaul .bluetoothutillib .dialogs .DeviceDialog ;
17
14
import com .devpaul .bluetoothutillib .handlers .BluetoothHandler ;
15
+ import com .devpaul .bluetoothutillib .handlers .DefaultBluetoothHandler ;
18
16
import com .devpaul .bluetoothutillib .utils .BluetoothUtility ;
19
17
import com .devpaul .bluetoothutillib .utils .SimpleBluetoothListener ;
20
18
@@ -102,11 +100,6 @@ public void onDeviceUnpaired(BluetoothDevice device) {
102
100
*/
103
101
private Context mContext ;
104
102
105
- /**
106
- * Reference to the calling activity.
107
- */
108
- private Activity mActivity ;
109
-
110
103
/**
111
104
* {@code BluetoothUtility used by SimpleBluetooth}
112
105
*/
@@ -157,20 +150,29 @@ public void onDeviceUnpaired(BluetoothDevice device) {
157
150
*/
158
151
private boolean connectWithService = false ;
159
152
153
+ /**
154
+ * Boolean for showing or hiding snackbars.
155
+ */
160
156
private boolean shouldShowSnackbars = false ;
161
157
158
+ /**
159
+ * Default handler for Simple bluetooth.
160
+ */
161
+ private BluetoothHandler mHandler ;
162
+
162
163
/**
163
164
* Constructor for {@code SimpleBluetooth}
164
165
* Allows for easy handling for setting up connections and bluetooth servers to connect to.
165
166
* @param context context from the calling activity
166
- * @param refActivity reference to the calling activity. Context and activity should match.
167
167
*/
168
- public SimpleBluetooth (Context context , Activity refActivity ) {
168
+ public SimpleBluetooth (Context context , SimpleBluetoothListener listener ) {
169
169
//initialize fields.
170
170
this .progressDialog = new ProgressDialog (context );
171
171
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 );
174
176
//register the state change receiver.
175
177
this .curType = InputStreamType .NORMAL ;
176
178
this .bluetoothStateReceiver = BluetoothStateReceiver
@@ -186,20 +188,18 @@ public SimpleBluetooth(Context context, Activity refActivity) {
186
188
* Constructor for {@code SimpleBluetooth} Use this constructor to provide your own custom bluetooth
187
189
* handler.
188
190
* @param context the context of the calling activity.
189
- * @param refActivity reference to the calling activity. Context and activity should match.
190
191
* @param handler custom {@code BluetoothHandler} for bluetooth event call backs.
191
192
*/
192
- public SimpleBluetooth (Context context , Activity refActivity , BluetoothHandler handler ) {
193
+ public SimpleBluetooth (Context context , BluetoothHandler handler ) {
193
194
//initialize fields.
194
195
this .progressDialog = new ProgressDialog (context );
195
196
this .mContext = context ;
196
- this .mActivity = refActivity ;
197
197
this .customHandler = handler ;
198
198
this .curType = InputStreamType .NORMAL ;
199
199
//check the handler.
200
200
if (customHandler == null ) throw
201
201
new NullPointerException ("Custom BluetoothHandler cannot be null!" );
202
- this .bluetoothUtility = new BluetoothUtility (mContext , mActivity , customHandler );
202
+ this .bluetoothUtility = new BluetoothUtility (mContext , customHandler );
203
203
//register the state change receiver.
204
204
this .bluetoothStateReceiver = BluetoothStateReceiver
205
205
.register (mContext , stateRecieverCallback );
@@ -232,51 +232,9 @@ public void setInputStreamType(InputStreamType type) {
232
232
*/
233
233
public void setShouldShowSnackbars (boolean show ) {
234
234
shouldShowSnackbars = show ;
235
+ mHandler .setShowSnackbars (show );
235
236
bluetoothUtility .setShouldShowSnackbars (show );
236
237
}
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
- };
280
238
281
239
/**
282
240
* Method that must be called to set everything up for this class.
@@ -336,8 +294,10 @@ public void sendData(byte[] data) {
336
294
* OnActivityResult.
337
295
*/
338
296
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
+ }
341
301
}
342
302
343
303
/**
@@ -418,12 +378,11 @@ public void connectToBluetoothServer(String macAddress) {
418
378
public void connectToA2DPDevice (String deviceName ) {
419
379
if (!isInitialized ) {
420
380
throw new IllegalStateException ("Must initialize before using any other method in class" +
421
- "SimpleBluetooth! Call initializeSimpleBluetooth()" );
381
+ "SimpleBluetooth. Call initializeSimpleBluetooth()" );
422
382
} else {
423
383
a2dpDevice = bluetoothUtility .findDeviceByName (deviceName );
424
384
bluetoothUtility .setUpA2DPConnection ();
425
385
}
426
-
427
386
}
428
387
429
388
/**
0 commit comments