summaryrefslogtreecommitdiff
path: root/app/src/main/java/com/javispedro/autobluetether/BtTetherEnabler.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/com/javispedro/autobluetether/BtTetherEnabler.java')
-rw-r--r--app/src/main/java/com/javispedro/autobluetether/BtTetherEnabler.java53
1 files changed, 53 insertions, 0 deletions
diff --git a/app/src/main/java/com/javispedro/autobluetether/BtTetherEnabler.java b/app/src/main/java/com/javispedro/autobluetether/BtTetherEnabler.java
new file mode 100644
index 0000000..bae42c8
--- /dev/null
+++ b/app/src/main/java/com/javispedro/autobluetether/BtTetherEnabler.java
@@ -0,0 +1,53 @@
+package com.javispedro.autobluetether;
+
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothProfile;
+import android.content.Context;
+import android.content.ContextWrapper;
+import android.util.Log;
+
+public class BtTetherEnabler extends ContextWrapper {
+ private final static String TAG = "BtTetherEnabler";
+
+ public BtTetherEnabler(Context context) {
+ super(context);
+ }
+
+ public static class EnableException extends Exception {
+ public EnableException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public EnableException(String message) {
+ super(message);
+ }
+ }
+
+ public void startTethering() throws EnableException {
+ final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
+ if (adapter == null) {
+ Log.e(TAG, "no BT adapter");
+ throw new EnableException("No Bluetooth adapter");
+ }
+
+ adapter.getProfileProxy(getBaseContext(), new BluetoothProfile.ServiceListener() {
+ public void onServiceConnected(int profile, BluetoothProfile proxy) {
+ Log.d(TAG, "onServiceConnected");
+ final BtPanWrapper wrapper = new BtPanWrapper(proxy);
+ boolean is_on = wrapper.isTetheringOn();
+ Log.d(TAG, "isTetheringOn: " + is_on);
+ if (!is_on) {
+ Log.d(TAG, "trying to turn on");
+ wrapper.setBluetoothTethering(true);
+ }
+ adapter.closeProfileProxy(profile, proxy);
+ }
+
+ public void onServiceDisconnected(int profile) {
+ Log.d(TAG, "onServiceDisconnected");
+ }
+ }, BtPanWrapper.PROFILE_ID);
+ }
+
+
+}