summaryrefslogtreecommitdiff
path: root/app/src/main/java/com/javispedro/autobluetether/BtPanWrapper.java
blob: 40cd84a68b998944ba9e743e2b8e7115fd3e25f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.javispedro.autobluetether;

import android.bluetooth.BluetoothProfile;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class BtPanWrapper {
    private static final String CLASS_NAME = "android.bluetooth.BluetoothPan";
    public static final int PROFILE_ID = 5 /*BluetoothProfile.PAN*/;
    private final BluetoothProfile p;

    public BtPanWrapper(BluetoothProfile pan) {
        p = pan;
    }

    private Object invoke(String methodName, Class[] parameterTypes, Object[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException {
        Class c = Class.forName(CLASS_NAME);
        Method m = c.getDeclaredMethod(methodName, parameterTypes);
        return m.invoke(this.p, args);
    }

    public boolean isTetheringOn() {
        try {
            return (boolean) invoke("isTetheringOn", null, null);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public void setBluetoothTethering(boolean on) {
        try {
            invoke("setBluetoothTethering", new Class[] {Boolean.TYPE}, new Object[] {on});
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}