1
1
package com .app .reallygoodpie .ledvisualalizer ;
2
2
3
+ import android .bluetooth .BluetoothAdapter ;
4
+ import android .bluetooth .BluetoothDevice ;
5
+ import android .bluetooth .BluetoothSocket ;
3
6
import android .content .Context ;
4
- import android .support . annotation . ColorInt ;
7
+ import android .content . Intent ;
5
8
import android .support .v4 .content .ContextCompat ;
6
- import android .support .v4 .util .ArrayMap ;
7
9
import android .support .v7 .app .AppCompatActivity ;
8
10
import android .os .Bundle ;
9
11
import android .util .Log ;
18
20
import com .app .reallygoodpie .ledvisualalizer .models .ColorGridModel ;
19
21
import com .pes .androidmaterialcolorpickerdialog .ColorPicker ;
20
22
23
+ import java .io .IOException ;
21
24
import java .util .Map ;
25
+ import java .util .Set ;
26
+ import java .util .UUID ;
22
27
23
28
public class MainActivity extends AppCompatActivity implements View .OnClickListener {
24
29
@@ -38,6 +43,11 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
38
43
private Context mContext ;
39
44
private ColorGridAdapter mAdapter ;
40
45
46
+ private BluetoothAdapter mBluetoothAdapter ;
47
+ private BluetoothDevice mDevice ;
48
+ private ConnectThread mConnectThread ;
49
+
50
+
41
51
protected void onCreate (Bundle savedInstanceState ) {
42
52
super .onCreate (savedInstanceState );
43
53
setContentView (R .layout .activity_main );
@@ -61,7 +71,10 @@ protected void onCreate(Bundle savedInstanceState) {
61
71
fillButton = (Button ) findViewById (R .id .fill_button );
62
72
fillButton .setOnClickListener (this );
63
73
64
- // Set the default color to blue
74
+ saveButton = (Button ) findViewById (R .id .save_button );
75
+ saveButton .setOnClickListener (this );
76
+
77
+ // Set the default color to green
65
78
currentGlobalColor = ContextCompat .getColor (mContext , R .color .md_green_500 );
66
79
67
80
// Initialize the grid
@@ -126,7 +139,108 @@ public void onClick(View v) {
126
139
currentGrid .init (currentGlobalColor );
127
140
mAdapter .notifyDataSetChanged ();
128
141
break ;
142
+ case R .id .save_button :
143
+ Log .i (TAG , "Saving..." );
144
+ connectBluetooth ();
145
+ break ;
146
+ }
147
+ }
148
+
149
+ public void initBluetooth ()
150
+ {
151
+ // Check if bluetooth can be enabled
152
+ mBluetoothAdapter = BluetoothAdapter .getDefaultAdapter ();
153
+ if (mBluetoothAdapter == null )
154
+ {
155
+ // Device is not supported
156
+ Toast .makeText (mContext , "Device does not support bluetooth" , Toast .LENGTH_SHORT ).show ();
157
+ }
158
+
159
+ // Check if bluetooth is enabled
160
+ if (!mBluetoothAdapter .isEnabled ())
161
+ {
162
+ Intent enableBtIntent = new Intent (BluetoothAdapter .ACTION_REQUEST_ENABLE );
163
+ startActivityForResult (enableBtIntent , 1 );
164
+ }
165
+
166
+ // Discover Devices
167
+ // Assume all other devices are disconnected
168
+ Set <BluetoothDevice > pairedDevices = mBluetoothAdapter .getBondedDevices ();
169
+ if (pairedDevices .size () > 0 )
170
+ {
171
+ for (BluetoothDevice device : pairedDevices )
172
+ {
173
+ mDevice = device ;
174
+ }
175
+ }
176
+
177
+
178
+
179
+ }
180
+
181
+ public void connectBluetooth ()
182
+ {
183
+ Toast .makeText (mContext , "Attempting to connect to device..." , Toast .LENGTH_LONG ).show ();
184
+ // Ensure we have a device to connect to
185
+ if (mDevice == null )
186
+ {
187
+ initBluetooth ();
129
188
}
189
+
190
+ Toast .makeText (mContext , "Connected to: " + mDevice .getName (), Toast .LENGTH_LONG ).show ();
191
+ mConnectThread = new ConnectThread (mDevice );
192
+ mConnectThread .start ();
193
+ }
194
+
195
+ private class ConnectThread extends Thread {
196
+
197
+ private static final String TAG = "ConnectThread" ;
198
+
199
+ private final BluetoothSocket mSocket ;
200
+ private final BluetoothDevice mDevice ;
201
+ private final UUID MY_UUID ;
202
+
203
+ public ConnectThread (BluetoothDevice device )
204
+ {
205
+ BluetoothSocket tmp = null ;
206
+ MY_UUID = UUID .randomUUID ();
207
+ mDevice = device ;
208
+
209
+ try {
210
+ tmp = device .createInsecureRfcommSocketToServiceRecord (MY_UUID );
211
+ } catch (IOException e ) {
212
+ Log .e (TAG , "Failed to create BluetoothSocket" );
213
+ }
214
+
215
+ mSocket = tmp ;
216
+ }
217
+
218
+ public void run ()
219
+ {
220
+ mBluetoothAdapter .cancelDiscovery ();
221
+ try {
222
+ mSocket .connect ();
223
+ } catch (IOException connectException ) {
224
+ try {
225
+ mSocket .close ();
226
+ } catch (IOException closeException ) {
227
+ Log .e (TAG , "run: Failed to close socket exception" );
228
+ }
229
+ }
230
+ }
231
+
232
+ public void cancel ()
233
+ {
234
+ try
235
+ {
236
+ mSocket .close ();
237
+ }
238
+ catch (IOException e )
239
+ {
240
+ Log .e (TAG , "cancel: Failed to close socket exception" );
241
+ }
242
+ }
243
+
130
244
}
131
245
132
246
0 commit comments