From 92ca6d6cb479fd866e772303ba4d7df70239536e Mon Sep 17 00:00:00 2001 From: jens Date: Sat, 13 Nov 2021 14:48:56 +0100 Subject: [PATCH 1/3] BT tethering --- .../main/java/com/jens/automation2/Action.java | 15 ++++++++++++++- .../java/com/jens/automation2/Actions.java | 18 +++++++++--------- .../jens/automation2/ActivityManageRule.java | 7 +++++++ .../jens/automation2/ActivityPermissions.java | 6 ++++++ .../com/jens/automation2/XmlFileInterface.java | 1 - app/src/main/res/values/strings.xml | 3 +++ 6 files changed, 39 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/jens/automation2/Action.java b/app/src/main/java/com/jens/automation2/Action.java index 215a085d..57a92efb 100644 --- a/app/src/main/java/com/jens/automation2/Action.java +++ b/app/src/main/java/com/jens/automation2/Action.java @@ -24,6 +24,7 @@ public class Action setBluetooth, setUsbTethering, setWifiTethering, + setBluetoothTethering, setDisplayRotation, turnWifiOn,turnWifiOff, turnBluetoothOn,turnBluetoothOff, @@ -31,7 +32,7 @@ public class Action changeSoundProfile, turnUsbTetheringOn,turnUsbTetheringOff, turnWifiTetheringOn,turnWifiTetheringOff, - enableScreenRotation, disableScreenRotation, + enableScreenRotation,disableScreenRotation, startOtherActivity, waitBeforeNextAction, wakeupDevice, @@ -54,6 +55,8 @@ public class Action return context.getResources().getString(R.string.actionSetBluetooth); case setWifiTethering: return context.getResources().getString(R.string.actionSetWifiTethering); + case setBluetoothTethering: + return context.getResources().getString(R.string.actionSetBluetoothTethering); case setUsbTethering: return context.getResources().getString(R.string.actionSetUsbTethering); case setDisplayRotation: @@ -178,6 +181,13 @@ public class Action else returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionTurnWifiTetheringOff)); } + else if(this.getAction().equals(Action_Enum.setBluetoothTethering)) + { + if(this.getParameter1()) + returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionTurnBluetoothTetheringOn)); + else + returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionTurnBluetoothTetheringOff)); + } else if(this.getAction().equals(Action_Enum.setDisplayRotation)) { if(this.getParameter1()) @@ -382,6 +392,9 @@ public class Action case setWifiTethering: Actions.setWifiTethering(context, getParameter1(), toggleActionIfPossible); break; + case setBluetoothTethering: + Actions.BluetoothTetheringClass.setBluetoothTethering(context, getParameter1(), toggleActionIfPossible); + break; case setDisplayRotation: Actions.setDisplayRotation(context, getParameter1(), toggleActionIfPossible); break; diff --git a/app/src/main/java/com/jens/automation2/Actions.java b/app/src/main/java/com/jens/automation2/Actions.java index f25a1f61..09150bbf 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -321,16 +321,16 @@ public class Actions { Miscellaneous.logEvent("i", "Bluetooth Tethering", "Changing Bluetooth Tethering to " + String.valueOf(desiredState), 4); - boolean state = Actions.isWifiApEnabled(context); +// boolean state = isTetheringOn(context); - if (toggleActionIfPossible) - { - Miscellaneous.logEvent("i", "Bluetooth Tethering", context.getResources().getString(R.string.toggling), 2); - desiredState = !state; - } +// if (toggleActionIfPossible) +// { +// Miscellaneous.logEvent("i", "Bluetooth Tethering", context.getResources().getString(R.string.toggling), 2); +// desiredState = !state; +// } - if (((state && !desiredState) || (!state && desiredState))) - { +// if (((state && !desiredState) || (!state && desiredState))) +// { BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); Class classBluetoothPan = null; Constructor BTPanCtor = null; @@ -372,7 +372,7 @@ public class Actions } } } - } +// } return true; } diff --git a/app/src/main/java/com/jens/automation2/ActivityManageRule.java b/app/src/main/java/com/jens/automation2/ActivityManageRule.java index c69d9e8c..0f6d0d88 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageRule.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageRule.java @@ -1361,6 +1361,8 @@ public class ActivityManageRule extends Activity items.add(new Item(typesLong[i].toString(), R.drawable.router)); else if(types[i].toString().equals(Action_Enum.setWifiTethering.toString())) items.add(new Item(typesLong[i].toString(), R.drawable.router)); + else if(types[i].toString().equals(Action_Enum.setBluetoothTethering.toString())) + items.add(new Item(typesLong[i].toString(), R.drawable.router)); else if(types[i].toString().equals(Action_Enum.setDisplayRotation.toString())) items.add(new Item(typesLong[i].toString(), R.drawable.displayrotation)); else if(types[i].toString().equals(Action_Enum.waitBeforeNextAction.toString())) @@ -1461,6 +1463,11 @@ public class ActivityManageRule extends Activity newAction.setAction(Action_Enum.setWifiTethering); getActionParameter1Dialog(ActivityManageRule.this).show(); } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.setBluetoothTethering.toString())) + { + newAction.setAction(Action_Enum.setBluetoothTethering); + getActionParameter1Dialog(ActivityManageRule.this).show(); + } else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.setDisplayRotation.toString())) { newAction.setAction(Action_Enum.setDisplayRotation); diff --git a/app/src/main/java/com/jens/automation2/ActivityPermissions.java b/app/src/main/java/com/jens/automation2/ActivityPermissions.java index 76738cbe..90a529fb 100644 --- a/app/src/main/java/com/jens/automation2/ActivityPermissions.java +++ b/app/src/main/java/com/jens/automation2/ActivityPermissions.java @@ -531,6 +531,12 @@ public class ActivityPermissions extends Activity // https://stackoverflow.com/questions/32185628/connectivitymanager-requestnetwork-in-android-6-0 // addToArrayListUnique(Manifest.permission.CHANGE_NETWORK_STATE, requiredPermissions); break; + case setBluetoothTethering: + //addToArrayListUnique(Manifest.permission.CHANGE_NETWORK_STATE, requiredPermissions); + addToArrayListUnique(Manifest.permission.BLUETOOTH, requiredPermissions); + addToArrayListUnique(Manifest.permission.BLUETOOTH_ADMIN, requiredPermissions); + addToArrayListUnique(Manifest.permission.WRITE_SETTINGS, requiredPermissions); + break; case setWifi: addToArrayListUnique(Manifest.permission.WRITE_SETTINGS, requiredPermissions); // https://stackoverflow.com/questions/32185628/connectivitymanager-requestnetwork-in-android-6-0 diff --git a/app/src/main/java/com/jens/automation2/XmlFileInterface.java b/app/src/main/java/com/jens/automation2/XmlFileInterface.java index 5c469307..b015e301 100644 --- a/app/src/main/java/com/jens/automation2/XmlFileInterface.java +++ b/app/src/main/java/com/jens/automation2/XmlFileInterface.java @@ -830,7 +830,6 @@ public class XmlFileInterface return (triggerCollection); } - private static Trigger readTrigger(XmlPullParser parser) throws IOException, XmlPullParserException { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6d9f0d31..e412a854 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -158,6 +158,7 @@ Bluetooth USB Tethering Wifi Tethering + Bluetooth Tethering Display rotation turn Wifi on turn Wifi off @@ -169,6 +170,8 @@ turn USB Tethering off turn Wifi Tethering on turn Wifi Tethering off + turn Bluetooth Tethering on + turn Bluetooth Tethering off turn airplane mode on turn airplane mode off enable screen rotation From 17109b12d42da6b800bac53e269f17abf94aa005 Mon Sep 17 00:00:00 2001 From: jens Date: Mon, 15 Nov 2021 20:28:38 +0100 Subject: [PATCH 2/3] BT tethering --- .../java/com/jens/automation2/Rule.java | 2 + .../java/com/jens/automation2/Actions.java | 64 +++++++++++++------ 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/app/src/apkFlavor/java/com/jens/automation2/Rule.java b/app/src/apkFlavor/java/com/jens/automation2/Rule.java index 23bba173..249a5370 100644 --- a/app/src/apkFlavor/java/com/jens/automation2/Rule.java +++ b/app/src/apkFlavor/java/com/jens/automation2/Rule.java @@ -349,6 +349,8 @@ public class Rule implements Comparable return true; case setWifiTethering: return true; + case setBluetoothTethering: + return true; default: break; } diff --git a/app/src/main/java/com/jens/automation2/Actions.java b/app/src/main/java/com/jens/automation2/Actions.java index 09150bbf..69f199cc 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -331,29 +331,37 @@ public class Actions // if (((state && !desiredState) || (!state && desiredState))) // { - BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); - Class classBluetoothPan = null; - Constructor BTPanCtor = null; - Object BTSrvInstance = null; - Method mBTPanConnect = null; + BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + Class classBluetoothPan = null; + Constructor BTPanCtor = null; + Object BTSrvInstance = null; + Method mBTPanConnect = null; - try + String sClassName = "android.bluetooth.BluetoothPan"; + try + { + classBluetoothPan = Class.forName(sClassName); + Constructor ctor = classBluetoothPan.getDeclaredConstructor(Context.class, BluetoothProfile.ServiceListener.class); + + ctor.setAccessible(true); + // Set Tethering ON + + Class[] paramSet = new Class[1]; + paramSet[0] = boolean.class; + + synchronized (mutex) { - classBluetoothPan = Class.forName("android.bluetooth.BluetoothPan"); - mBTPanConnect = classBluetoothPan.getDeclaredMethod("connect", BluetoothDevice.class); - BTPanCtor = classBluetoothPan.getDeclaredConstructor(Context.class, BluetoothProfile.ServiceListener.class); - BTPanCtor.setAccessible(true); - BTSrvInstance = BTPanCtor.newInstance(context, new BTPanServiceListener(context)); - } - catch (ClassNotFoundException e) - { - Miscellaneous.logEvent("e", "Bluetooth Tethering", Log.getStackTraceString(e), 1); - } - catch (Exception e) - { - Miscellaneous.logEvent("e", "Bluetooth Tethering", Log.getStackTraceString(e), 1); + setTetheringOn = classBluetoothPan.getDeclaredMethod("setBluetoothTethering", paramSet); + isTetheringOn = classBluetoothPan.getDeclaredMethod("isTetheringOn", null); + instance = ctor.newInstance(context, new BTPanServiceListener(context)); } + classBluetoothPan = Class.forName("android.bluetooth.BluetoothPan"); + mBTPanConnect = classBluetoothPan.getDeclaredMethod("connect", BluetoothDevice.class); + BTPanCtor = classBluetoothPan.getDeclaredConstructor(Context.class, BluetoothProfile.ServiceListener.class); + BTPanCtor.setAccessible(true); + BTSrvInstance = BTPanCtor.newInstance(context, new BTPanServiceListener(context)); + Set pairedDevices = mBluetoothAdapter.getBondedDevices(); // If there are paired devices @@ -372,8 +380,22 @@ public class Actions } } } -// } - return true; + return true; + } + catch (NoSuchMethodException e) + { + Miscellaneous.logEvent("e", "Bluetooth Tethering", Log.getStackTraceString(e), 1); + } + catch (ClassNotFoundException e) + { + Miscellaneous.logEvent("e", "Bluetooth Tethering", Log.getStackTraceString(e), 1); + } + catch (Exception e) + { + Miscellaneous.logEvent("e", "Bluetooth Tethering", Log.getStackTraceString(e), 1); + } + + return false; } public static class BTPanServiceListener implements BluetoothProfile.ServiceListener From fb87d5e42db344be6d64aae506a12d3256f4d83b Mon Sep 17 00:00:00 2001 From: jens Date: Wed, 17 Nov 2021 21:46:56 +0100 Subject: [PATCH 3/3] BT tethering --- .idea/deploymentTargetDropDown.xml | 4 ++-- app/src/main/java/com/jens/automation2/Actions.java | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index c23aaba9..3ec016f8 100644 --- a/.idea/deploymentTargetDropDown.xml +++ b/.idea/deploymentTargetDropDown.xml @@ -7,11 +7,11 @@ - + - + \ No newline at end of file diff --git a/app/src/main/java/com/jens/automation2/Actions.java b/app/src/main/java/com/jens/automation2/Actions.java index 69f199cc..2437d707 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -390,6 +390,15 @@ public class Actions { Miscellaneous.logEvent("e", "Bluetooth Tethering", Log.getStackTraceString(e), 1); } + catch(InvocationTargetException e) + { + /* + Exact error message: "Bluetooth binder is null" + This means this device doesn't have bluetooth. + */ + Miscellaneous.logEvent("e", "Bluetooth Tethering", "Device probably doesn't have bluetooth. " + Log.getStackTraceString(e), 1); + Toast.makeText(context, context.getResources().getString(R.string.deviceDoesNotHaveBluetooth), Toast.LENGTH_SHORT).show(); + } catch (Exception e) { Miscellaneous.logEvent("e", "Bluetooth Tethering", Log.getStackTraceString(e), 1);