Skip to content

Commit ffb136c

Browse files
Updated SimpleBluetoothListener to an abstract class, added ability to pair device through the library. Need to add a way to create insecure connections with unpaired devices.
1 parent dbac6ca commit ffb136c

20 files changed

+457
-88
lines changed

.idea/libraries/appcompat_v7_23_0_1.xml

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

.idea/libraries/core_0_8_1_0.xml

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

.idea/libraries/recyclerview_v7_23_0_1.xml

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

.idea/libraries/support_annotations_23_0_1.xml

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

.idea/libraries/support_v4_23_0_1.xml

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

.idea/misc.xml

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

.idea/modules.xml

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

BluetoothUtilityDemo.iml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module external.linked.project.id="BluetoothUtilityDemo" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="com.github.developerpaul123.simplebluetoothlibrary" external.system.module.version="1.4.1" type="JAVA_MODULE" version="4">
3+
<component name="FacetManager">
4+
<facet type="java-gradle" name="Java-Gradle">
5+
<configuration>
6+
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
7+
<option name="BUILDABLE" value="false" />
8+
</configuration>
9+
</facet>
10+
</component>
11+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true">
12+
<exclude-output />
13+
<content url="file://$MODULE_DIR$">
14+
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
15+
</content>
16+
<orderEntry type="inheritedJdk" />
17+
<orderEntry type="sourceFolder" forTests="false" />
18+
</component>
19+
</module>

app/app.iml

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<module external.linked.project.id=":app" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.github.developerpaul123.simplebluetoothlibrary" external.system.module.version="1.4" type="JAVA_MODULE" version="4">
2+
<module external.linked.project.id=":app" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.github.developerpaul123.simplebluetoothlibrary" external.system.module.version="1.4.1" type="JAVA_MODULE" version="4">
33
<component name="FacetManager">
44
<facet type="android-gradle" name="Android-Gradle">
55
<configuration>
@@ -71,11 +71,6 @@
7171
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
7272
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex" />
7373
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex-cache" />
74-
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.afollestad.material-dialogs/core/0.8.1.0/jars" />
75-
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.0.1/jars" />
76-
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/recyclerview-v7/23.0.1/jars" />
77-
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/23.0.1/jars" />
78-
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.github.DeveloperPaul123/SimpleBluetoothLibrary/ca1f0785cd/jars" />
7974
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
8075
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jacoco" />
8176
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javaResources" />
@@ -93,11 +88,7 @@
9388
</content>
9489
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
9590
<orderEntry type="sourceFolder" forTests="false" />
96-
<orderEntry type="library" exported="" name="recyclerview-v7-23.0.1" level="project" />
97-
<orderEntry type="library" exported="" name="SimpleBluetoothLibrary-ca1f0785cd" level="project" />
98-
<orderEntry type="library" exported="" name="core-0.8.1.0" level="project" />
99-
<orderEntry type="library" exported="" name="support-v4-23.0.1" level="project" />
100-
<orderEntry type="library" exported="" name="appcompat-v7-23.0.1" level="project" />
10191
<orderEntry type="library" exported="" name="support-annotations-23.0.1" level="project" />
92+
<orderEntry type="module" module-name="btutillib" exported="" />
10293
</component>
10394
</module>

app/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ repositories {
2626

2727
dependencies {
2828
compile fileTree(include: ['*.jar'], dir: 'libs')
29-
// compile project(':btutillib')
30-
compile 'com.github.DeveloperPaul123:SimpleBluetoothLibrary:ca1f0785cd'
29+
compile project(':btutillib')
30+
// compile 'com.github.DeveloperPaul123:SimpleBluetoothLibrary:ca1f0785cd'
3131
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
165165
if(resultCode == RESULT_OK) {
166166

167167
curMacAddress = data.getStringExtra(DeviceDialog.DEVICE_DIALOG_DEVICE_ADDRESS_EXTRA);
168+
boolean paired = simpleBluetooth.getBluetoothUtility()
169+
.checkIfPaired(simpleBluetooth.getBluetoothUtility()
170+
.findDeviceByMacAddress(curMacAddress));
171+
String message = paired ? "is paired" : "is not paired";
172+
Log.i("ActivityResult", "Device " + message);
168173
if(requestCode == SCAN_REQUEST) {
169174
simpleBluetooth.connectToBluetoothDevice(curMacAddress);
170175
} else {

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

+41-24
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,53 @@
55
import android.widget.Toast;
66

77
import com.devpaul.bluetoothutillib.abstracts.BaseBluetoothActivity;
8+
import com.devpaul.bluetoothutillib.utils.SimpleBluetoothListener;
89

910
/**
1011
* Created by Pauly D on 3/18/2015.
1112
*/
1213
public class TestActivity extends BaseBluetoothActivity {
1314

15+
@Override
16+
public SimpleBluetoothListener getListener() {
17+
return new SimpleBluetoothListener() {
18+
@Override
19+
public void onBluetoothDataReceived(byte[] bytes, String data) {
20+
super.onBluetoothDataReceived(bytes, data);
21+
}
22+
23+
@Override
24+
public void onDeviceConnected(BluetoothDevice device) {
25+
super.onDeviceConnected(device);
26+
}
27+
28+
@Override
29+
public void onDeviceDisconnected(BluetoothDevice device) {
30+
super.onDeviceDisconnected(device);
31+
}
32+
33+
@Override
34+
public void onDiscoveryStarted() {
35+
super.onDiscoveryStarted();
36+
}
37+
38+
@Override
39+
public void onDiscoveryFinished() {
40+
super.onDiscoveryFinished();
41+
}
42+
43+
@Override
44+
public void onDevicePaired(BluetoothDevice device) {
45+
super.onDevicePaired(device);
46+
}
47+
48+
@Override
49+
public void onDeviceUnpaired(BluetoothDevice device) {
50+
super.onDeviceUnpaired(device);
51+
}
52+
};
53+
}
54+
1455
@Override
1556
protected void onCreate(Bundle savedInstanceState) {
1657
super.onCreate(savedInstanceState);
@@ -28,28 +69,4 @@ public void onDeviceSelected(String macAddress) {
2869
Toast.makeText(this, "Device " + macAddress, Toast.LENGTH_SHORT).show();
2970
}
3071

31-
@Override
32-
public void onBluetoothDataReceived(byte[] bytes, String data) {
33-
34-
}
35-
36-
@Override
37-
public void onDeviceConnected(BluetoothDevice device) {
38-
39-
}
40-
41-
@Override
42-
public void onDeviceDisconnected(BluetoothDevice device) {
43-
44-
}
45-
46-
@Override
47-
public void onDiscoveryStarted() {
48-
49-
}
50-
51-
@Override
52-
public void onDiscoveryFinished() {
53-
54-
}
5572
}

btutillib/btutillib.iml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<module external.linked.project.id=":btutillib" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.github.DeveloperPaul123" external.system.module.version="1.4" type="JAVA_MODULE" version="4">
2+
<module external.linked.project.id=":btutillib" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.github.DeveloperPaul123" external.system.module.version="1.4.1" type="JAVA_MODULE" version="4">
33
<component name="FacetManager">
44
<facet type="android-gradle" name="Android-Gradle">
55
<configuration>

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

+31-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.widget.Toast;
1111

1212
import com.devpaul.bluetoothutillib.broadcasts.BluetoothBroadcastReceiver;
13+
import com.devpaul.bluetoothutillib.broadcasts.BluetoothPairingReceiver;
1314
import com.devpaul.bluetoothutillib.broadcasts.BluetoothStateReceiver;
1415
import com.devpaul.bluetoothutillib.broadcasts.FoundDeviceReceiver;
1516
import com.devpaul.bluetoothutillib.dialogs.DeviceDialog;
@@ -74,6 +75,22 @@ public void onDiscoveryStarted() {
7475
}
7576
};
7677

78+
private final BluetoothPairingReceiver.Callback bluetoothPairingReciever = new BluetoothPairingReceiver.Callback() {
79+
@Override
80+
public void onDevicePaired(BluetoothDevice device) {
81+
if(mListener != null) {
82+
mListener.onDevicePaired(device);
83+
}
84+
}
85+
86+
@Override
87+
public void onDeviceUnpaired(BluetoothDevice device) {
88+
if(mListener != null) {
89+
mListener.onDeviceUnpaired(device);
90+
}
91+
}
92+
};
93+
7794
/**
7895
* {@link com.devpaul.bluetoothutillib.utils.SimpleBluetoothListener} for SimpleBluetooth
7996
*/
@@ -104,6 +121,11 @@ public void onDiscoveryStarted() {
104121
*/
105122
private BluetoothStateReceiver bluetoothStateReceiver;
106123

124+
/**
125+
* {@code BluetoothPairingReceiver} that receives pair/unpair intents.
126+
*/
127+
private BluetoothPairingReceiver bluetoothPairingReceiver;
128+
107129
/**
108130
* State boolean
109131
*/
@@ -148,13 +170,17 @@ public SimpleBluetooth(Context context, Activity refActivity) {
148170
this.bluetoothUtility = new BluetoothUtility(mContext, mActivity, mHandler);
149171
//register the state change receiver.
150172
this.curType = InputStreamType.NORMAL;
173+
151174
/*
152175
Trying onActivityResult instead of this for now.
153176
*/
154177
// this.bluetoothBroadcastReceiver = BluetoothBroadcastReceiver
155178
// .register(mContext, bluetoothBroadcastRecieverCallback);
156179
this.bluetoothStateReceiver = BluetoothStateReceiver
157180
.register(mContext, stateRecieverCallback);
181+
182+
this.bluetoothPairingReceiver = BluetoothPairingReceiver
183+
.register(mContext, bluetoothPairingReciever);
158184
//state boolean
159185
this.isInitialized = false;
160186
}
@@ -186,6 +212,8 @@ public SimpleBluetooth(Context context, Activity refActivity, BluetoothHandler h
186212

187213
this.bluetoothStateReceiver = BluetoothStateReceiver
188214
.register(mContext, stateRecieverCallback);
215+
this.bluetoothPairingReceiver = BluetoothPairingReceiver
216+
.register(mContext, bluetoothPairingReciever);
189217
//state boolean
190218
this.isInitialized = false;
191219
}
@@ -359,6 +387,7 @@ public void connectToBluetoothServer(String macAddress) {
359387

360388
}
361389

390+
362391
/**
363392
* Connects to an A2DP device.
364393
* @param deviceName the name of the device to connect to.
@@ -387,8 +416,9 @@ public void makeDiscoverable(int duration) {
387416
* Ends all connections and unregister the receiver.
388417
*/
389418
public void endSimpleBluetooth() {
390-
BluetoothBroadcastReceiver.safeUnregister(mContext, bluetoothBroadcastReceiver);
419+
// BluetoothBroadcastReceiver.safeUnregister(mContext, bluetoothBroadcastReceiver);
391420
BluetoothStateReceiver.safeUnregister(mContext, bluetoothStateReceiver);
421+
BluetoothPairingReceiver.safeUnregister(mContext, bluetoothPairingReceiver);
392422
bluetoothUtility.closeConnections();
393423
}
394424

0 commit comments

Comments
 (0)