11package com .app .reallygoodpie .ledvisualalizer ;
22
3+ import android .bluetooth .BluetoothAdapter ;
4+ import android .bluetooth .BluetoothDevice ;
5+ import android .bluetooth .BluetoothSocket ;
36import android .content .Context ;
4- import android .support . annotation . ColorInt ;
7+ import android .content . Intent ;
58import android .support .v4 .content .ContextCompat ;
6- import android .support .v4 .util .ArrayMap ;
79import android .support .v7 .app .AppCompatActivity ;
810import android .os .Bundle ;
911import android .util .Log ;
1820import com .app .reallygoodpie .ledvisualalizer .models .ColorGridModel ;
1921import com .pes .androidmaterialcolorpickerdialog .ColorPicker ;
2022
23+ import java .io .IOException ;
2124import java .util .Map ;
25+ import java .util .Set ;
26+ import java .util .UUID ;
2227
2328public class MainActivity extends AppCompatActivity implements View .OnClickListener {
2429
@@ -38,6 +43,11 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
3843 private Context mContext ;
3944 private ColorGridAdapter mAdapter ;
4045
46+ private BluetoothAdapter mBluetoothAdapter ;
47+ private BluetoothDevice mDevice ;
48+ private ConnectThread mConnectThread ;
49+
50+
4151 protected void onCreate (Bundle savedInstanceState ) {
4252 super .onCreate (savedInstanceState );
4353 setContentView (R .layout .activity_main );
@@ -61,7 +71,10 @@ protected void onCreate(Bundle savedInstanceState) {
6171 fillButton = (Button ) findViewById (R .id .fill_button );
6272 fillButton .setOnClickListener (this );
6373
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
6578 currentGlobalColor = ContextCompat .getColor (mContext , R .color .md_green_500 );
6679
6780 // Initialize the grid
@@ -126,7 +139,108 @@ public void onClick(View v) {
126139 currentGrid .init (currentGlobalColor );
127140 mAdapter .notifyDataSetChanged ();
128141 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 ();
129188 }
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+
130244 }
131245
132246
0 commit comments