Skip to content

Commit

Permalink
Updated SimpleBluetoothListener to an abstract class, added ability t…
Browse files Browse the repository at this point in the history
…o pair device through the library. Need to add a way to create insecure connections with unpaired devices.
  • Loading branch information
DeveloperPaul123 committed Oct 8, 2015
1 parent dbac6ca commit ffb136c
Show file tree
Hide file tree
Showing 20 changed files with 457 additions and 88 deletions.
15 changes: 15 additions & 0 deletions .idea/libraries/appcompat_v7_23_0_1.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/core_0_8_1_0.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/libraries/recyclerview_v7_23_0_1.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/libraries/support_annotations_23_0_1.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/libraries/support_v4_23_0_1.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 25 additions & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions BluetoothUtilityDemo.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<component name="FacetManager">
<facet type="java-gradle" name="Java-Gradle">
<configuration>
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
<option name="BUILDABLE" value="false" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
13 changes: 2 additions & 11 deletions app/app.iml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<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">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
Expand Down Expand Up @@ -71,11 +71,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.afollestad.material-dialogs/core/0.8.1.0/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.0.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/recyclerview-v7/23.0.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/23.0.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.github.DeveloperPaul123/SimpleBluetoothLibrary/ca1f0785cd/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jacoco" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javaResources" />
Expand All @@ -93,11 +88,7 @@
</content>
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="recyclerview-v7-23.0.1" level="project" />
<orderEntry type="library" exported="" name="SimpleBluetoothLibrary-ca1f0785cd" level="project" />
<orderEntry type="library" exported="" name="core-0.8.1.0" level="project" />
<orderEntry type="library" exported="" name="support-v4-23.0.1" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-23.0.1" level="project" />
<orderEntry type="library" exported="" name="support-annotations-23.0.1" level="project" />
<orderEntry type="module" module-name="btutillib" exported="" />
</component>
</module>
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ repositories {

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
// compile project(':btutillib')
compile 'com.github.DeveloperPaul123:SimpleBluetoothLibrary:ca1f0785cd'
compile project(':btutillib')
// compile 'com.github.DeveloperPaul123:SimpleBluetoothLibrary:ca1f0785cd'
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {

curMacAddress = data.getStringExtra(DeviceDialog.DEVICE_DIALOG_DEVICE_ADDRESS_EXTRA);
boolean paired = simpleBluetooth.getBluetoothUtility()
.checkIfPaired(simpleBluetooth.getBluetoothUtility()
.findDeviceByMacAddress(curMacAddress));
String message = paired ? "is paired" : "is not paired";
Log.i("ActivityResult", "Device " + message);
if(requestCode == SCAN_REQUEST) {
simpleBluetooth.connectToBluetoothDevice(curMacAddress);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,53 @@
import android.widget.Toast;

import com.devpaul.bluetoothutillib.abstracts.BaseBluetoothActivity;
import com.devpaul.bluetoothutillib.utils.SimpleBluetoothListener;

/**
* Created by Pauly D on 3/18/2015.
*/
public class TestActivity extends BaseBluetoothActivity {

@Override
public SimpleBluetoothListener getListener() {
return new SimpleBluetoothListener() {
@Override
public void onBluetoothDataReceived(byte[] bytes, String data) {
super.onBluetoothDataReceived(bytes, data);
}

@Override
public void onDeviceConnected(BluetoothDevice device) {
super.onDeviceConnected(device);
}

@Override
public void onDeviceDisconnected(BluetoothDevice device) {
super.onDeviceDisconnected(device);
}

@Override
public void onDiscoveryStarted() {
super.onDiscoveryStarted();
}

@Override
public void onDiscoveryFinished() {
super.onDiscoveryFinished();
}

@Override
public void onDevicePaired(BluetoothDevice device) {
super.onDevicePaired(device);
}

@Override
public void onDeviceUnpaired(BluetoothDevice device) {
super.onDeviceUnpaired(device);
}
};
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -28,28 +69,4 @@ public void onDeviceSelected(String macAddress) {
Toast.makeText(this, "Device " + macAddress, Toast.LENGTH_SHORT).show();
}

@Override
public void onBluetoothDataReceived(byte[] bytes, String data) {

}

@Override
public void onDeviceConnected(BluetoothDevice device) {

}

@Override
public void onDeviceDisconnected(BluetoothDevice device) {

}

@Override
public void onDiscoveryStarted() {

}

@Override
public void onDiscoveryFinished() {

}
}
2 changes: 1 addition & 1 deletion btutillib/btutillib.iml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<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">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.widget.Toast;

import com.devpaul.bluetoothutillib.broadcasts.BluetoothBroadcastReceiver;
import com.devpaul.bluetoothutillib.broadcasts.BluetoothPairingReceiver;
import com.devpaul.bluetoothutillib.broadcasts.BluetoothStateReceiver;
import com.devpaul.bluetoothutillib.broadcasts.FoundDeviceReceiver;
import com.devpaul.bluetoothutillib.dialogs.DeviceDialog;
Expand Down Expand Up @@ -74,6 +75,22 @@ public void onDiscoveryStarted() {
}
};

private final BluetoothPairingReceiver.Callback bluetoothPairingReciever = new BluetoothPairingReceiver.Callback() {
@Override
public void onDevicePaired(BluetoothDevice device) {
if(mListener != null) {
mListener.onDevicePaired(device);
}
}

@Override
public void onDeviceUnpaired(BluetoothDevice device) {
if(mListener != null) {
mListener.onDeviceUnpaired(device);
}
}
};

/**
* {@link com.devpaul.bluetoothutillib.utils.SimpleBluetoothListener} for SimpleBluetooth
*/
Expand Down Expand Up @@ -104,6 +121,11 @@ public void onDiscoveryStarted() {
*/
private BluetoothStateReceiver bluetoothStateReceiver;

/**
* {@code BluetoothPairingReceiver} that receives pair/unpair intents.
*/
private BluetoothPairingReceiver bluetoothPairingReceiver;

/**
* State boolean
*/
Expand Down Expand Up @@ -148,13 +170,17 @@ public SimpleBluetooth(Context context, Activity refActivity) {
this.bluetoothUtility = new BluetoothUtility(mContext, mActivity, mHandler);
//register the state change receiver.
this.curType = InputStreamType.NORMAL;

/*
Trying onActivityResult instead of this for now.
*/
// this.bluetoothBroadcastReceiver = BluetoothBroadcastReceiver
// .register(mContext, bluetoothBroadcastRecieverCallback);
this.bluetoothStateReceiver = BluetoothStateReceiver
.register(mContext, stateRecieverCallback);

this.bluetoothPairingReceiver = BluetoothPairingReceiver
.register(mContext, bluetoothPairingReciever);
//state boolean
this.isInitialized = false;
}
Expand Down Expand Up @@ -186,6 +212,8 @@ public SimpleBluetooth(Context context, Activity refActivity, BluetoothHandler h

this.bluetoothStateReceiver = BluetoothStateReceiver
.register(mContext, stateRecieverCallback);
this.bluetoothPairingReceiver = BluetoothPairingReceiver
.register(mContext, bluetoothPairingReciever);
//state boolean
this.isInitialized = false;
}
Expand Down Expand Up @@ -359,6 +387,7 @@ public void connectToBluetoothServer(String macAddress) {

}


/**
* Connects to an A2DP device.
* @param deviceName the name of the device to connect to.
Expand Down Expand Up @@ -387,8 +416,9 @@ public void makeDiscoverable(int duration) {
* Ends all connections and unregister the receiver.
*/
public void endSimpleBluetooth() {
BluetoothBroadcastReceiver.safeUnregister(mContext, bluetoothBroadcastReceiver);
// BluetoothBroadcastReceiver.safeUnregister(mContext, bluetoothBroadcastReceiver);
BluetoothStateReceiver.safeUnregister(mContext, bluetoothStateReceiver);
BluetoothPairingReceiver.safeUnregister(mContext, bluetoothPairingReceiver);
bluetoothUtility.closeConnections();
}

Expand Down
Loading

0 comments on commit ffb136c

Please sign in to comment.