Skip to content

Commit f1b4957

Browse files
committedApr 17, 2017
Added connection to bluetooth
* Bluetooth should connect to the only bluetooth device paired
1 parent ebb4f6a commit f1b4957

File tree

7 files changed

+143
-8
lines changed

7 files changed

+143
-8
lines changed
 

‎.idea/modules.xml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎app/build.gradle

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
apply plugin: 'com.android.application'
22

33
android {
4+
signingConfigs {
5+
debug {
6+
keyAlias 'AndroidDebugKey'
7+
keyPassword 'password'
8+
storeFile file('C:/Users/brand/.android/debug.keystore2')
9+
storePassword 'password'
10+
}
11+
}
412
compileSdkVersion 25
513
buildToolsVersion "25.0.2"
614
defaultConfig {
@@ -20,12 +28,11 @@ android {
2028
}
2129

2230
dependencies {
23-
compile fileTree(dir: 'libs', include: ['*.jar'])
31+
compile fileTree(include: ['*.jar'], dir: 'libs')
2432
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
2533
exclude group: 'com.android.support', module: 'support-annotations'
2634
})
2735
compile 'com.android.support:appcompat-v7:25.1.1'
2836
compile 'com.pes.materialcolorpicker:library:1.0.2'
29-
3037
testCompile 'junit:junit:4.12'
3138
}

‎app/src/main/AndroidManifest.xml

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.app.reallygoodpie.ledvisualalizer">
44

5+
<uses-permission android:name="android.permission.BLUETOOTH" />
6+
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
7+
58
<application
69
android:allowBackup="true"
710
android:icon="@mipmap/ic_launcher"

‎app/src/main/java/com/app/reallygoodpie/ledvisualalizer/MainActivity.java

+117-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.app.reallygoodpie.ledvisualalizer;
22

3+
import android.bluetooth.BluetoothAdapter;
4+
import android.bluetooth.BluetoothDevice;
5+
import android.bluetooth.BluetoothSocket;
36
import android.content.Context;
4-
import android.support.annotation.ColorInt;
7+
import android.content.Intent;
58
import android.support.v4.content.ContextCompat;
6-
import android.support.v4.util.ArrayMap;
79
import android.support.v7.app.AppCompatActivity;
810
import android.os.Bundle;
911
import android.util.Log;
@@ -18,7 +20,10 @@
1820
import com.app.reallygoodpie.ledvisualalizer.models.ColorGridModel;
1921
import com.pes.androidmaterialcolorpickerdialog.ColorPicker;
2022

23+
import java.io.IOException;
2124
import java.util.Map;
25+
import java.util.Set;
26+
import java.util.UUID;
2227

2328
public 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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.app.reallygoodpie.ledvisualalizer.helpers;
2+
3+
import android.bluetooth.BluetoothDevice;
4+
import android.bluetooth.BluetoothSocket;
5+
import android.util.Log;
6+
7+
import java.io.IOException;
8+
import java.util.UUID;
9+

‎build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.2.3'
8+
classpath 'com.android.tools.build:gradle:2.3.0'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Mon Dec 28 10:00:20 PST 2015
1+
#Mon Apr 17 19:16:57 AWST 2017
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

0 commit comments

Comments
 (0)
Please sign in to comment.