0

I am trying to enable bluetooth tethering on a android nexus 5 4.4.4 from an app.

Does anyone have a working code for this? I have seen some examples using the "android.bluetooth.BluetoothPan" Class for this, but i am unable to get it working.

With the expamples i get the following error in logcat:

java.lang.reflect.InvocationTargetException Caused by: java.lang.NullPointerException at android.bluetooth.BluetoothPan.isTetheringOn(BluetoothPan.java:346)

Thanks in advise.

UPDATE: Here are some more details. I try to get the current tethering state.

onCreate Method:

private boolean btEnabled = false;

BluetoothAdapter mBluetoothAdapter = null;
Class<?> classBluetoothPan = null;
Constructor<?> BTPanCtor = null;
Object BTSrvInstance = null;
Class<?> noparams[] = {};
Method mIsBTTetheringOn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    try {
        RetrieveWeather();
    } catch (IOException e) {
        e.printStackTrace();
    }
    CheckBluetoothState();


    Context MyContext = getApplicationContext();
    mBluetoothAdapter = getBTAdapter();
    try {
        classBluetoothPan = Class.forName("android.bluetooth.BluetoothPan");
        mIsBTTetheringOn = classBluetoothPan.getDeclaredMethod("isTetheringOn", noparams);
        BTPanCtor = classBluetoothPan.getDeclaredConstructor(Context.class, BluetoothProfile.ServiceListener.class);
        BTPanCtor.setAccessible(true);
        BTSrvInstance = BTPanCtor.newInstance(MyContext, new BTPanServiceListener(MyContext));
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }


    boolean test = IsBluetoothTetherEnabled();
    if (test){
        Log.d("Tethering", "Bluetooth Tethering is On");
    } else {
        Log.d("Tethering", "Bluetooth Tethering is Off");
    }
}



private BluetoothAdapter getBTAdapter() {
    if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1)
        return BluetoothAdapter.getDefaultAdapter();
    else {
        BluetoothManager bm = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
        return bm.getAdapter();
    }
}


// Check whether Bluetooth tethering is enabled.
private boolean IsBluetoothTetherEnabled() {
    try {
        if(mBluetoothAdapter != null) {

            return (Boolean) mIsBTTetheringOn.invoke(BTSrvInstance, (Object []) noparams);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

The error occours in the following line: return (Boolean) mIsBTTetheringOn.invoke(BTSrvInstance, (Object []) noparams);

Best regards

GMSonic
  • 1
  • 1

0 Answers0