Hallo zusammen
ich steh wieder mal vor einem Problem.
Ich wollte mir eine einfache App basteln die bei meinem Handy (Version 6.0) die Einstellungen/Tethering/Bluetooth-Tethering aktiviert/deaktiviert.
Ich habe mir dazu im Internet den Code runter geladen.
Allderings verstehen tu ich ihn nicht so richtig.
Mein grösseres Problem ist aber, daß er
nach dem Start immer bei Zeile 44 (PTPanServuceListerner > proxy.getClass......)
abbricht bzw. catch ... aufruft.
Kann mir da jemand helfen ?
Gruß herrm_no
Code
proxy.getClass().getMethod("setBluetoothTethering", new Class[]{Boolean.TYPE}).invoke(proxy, new Object[]{Boolean.valueOf(true)});
BluetootTethering
Java
package com.herrm_no.bluetoothtethering;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.Switch;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class BluetoothTethering extends AppCompatActivity {
BluetoothAdapter mBluetoothAdapter = null;
Class<?> classBluetoothPan = null;
Constructor<?> BTPanCtor = null;
Object BTSrvInstance = null;
Class<?> noparams[] = {};
Method mIsBTTetheringOn;
public static Switch toggle ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth_tethering);
toggle = findViewById(R.id.wifi_switch);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
try {
mBluetoothAdapter = getBTAdapter();
mBluetoothAdapter.enable();
Thread.sleep(100);
toggleTethering();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_bluetooth_tethering, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void toggleTethering() {
Context MyContext = getApplicationContext();
mBluetoothAdapter = getBTAdapter();
String sClassName = "android.bluetooth.BluetoothPan";
try {
classBluetoothPan = Class.forName(sClassName);
mIsBTTetheringOn = classBluetoothPan.getDeclaredMethod("isTetheringOn", noparams);
BTPanCtor = classBluetoothPan.getDeclaredConstructor(Context.class, BluetoothProfile.ServiceListener.class);
BTPanCtor.setAccessible(true);
BTSrvInstance = BTPanCtor.newInstance(MyContext, new BTPanServiceListener(MyContext));
Thread.sleep(250);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressLint("NewApi")
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();
}
}
public static void changeToggleState(boolean state) {
try{
if(state){
toggle.setChecked(BTPanServiceListener.state);
}else {
toggle.setChecked(BTPanServiceListener.state);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
Alles anzeigen
BTPanServiceListener
Java
package com.herrm_no.bluetoothtethering;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class BTPanServiceListener implements BluetoothProfile.ServiceListener {
private final Context context;
public static boolean state = false;
BluetoothAdapter mBluetoothAdapter = null;
Class<?> classBluetoothPan = null;
//public Boolean BOL = true;
public BTPanServiceListener(final Context context) {
this.context = context;
}
@Override
public void onServiceConnected(final int profile,
final BluetoothProfile proxy) {
//Some code must be here or the compiler will optimize away this callback.
Log.i("MyApp", "BTPan proxy connected");
try {
//boolean nowVal = ((Boolean) proxy.getClass().getMethod("isTetheringOn", new Class[0]).invoke(proxy, new Object[0])).booleanValue();
boolean nowVal = ((Boolean) proxy.getClass().getMethod("isTetheringOn", new Class[0]).invoke(proxy));
if (nowVal) {
proxy.getClass().getMethod("setBluetoothTethering", new Class[]{Boolean.TYPE}).invoke(proxy, new Object[]{Boolean.valueOf(false)});
// proxy.getClass().getMethod("setBluetoothTethering", new Class[]{Boolean.TYPE}).invoke(proxy, true); //enable);
Toast.makeText(context, "Turning bluetooth tethering off", Toast.LENGTH_SHORT).show();
state = false;
} else {
proxy.getClass().getMethod("setBluetoothTethering", new Class[]{Boolean.TYPE}).invoke(proxy, new Object[]{Boolean.valueOf(true)});
// proxy.getClass().getMethod("setBluetoothTethering", new Class[]{Boolean.TYPE}).invoke(proxy, false); //enable);
Toast.makeText(context, "Turning bluetooth tethering on", Toast.LENGTH_SHORT).show();
state = true;
}
BluetoothTethering.changeToggleState(state);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(final int profile) {
}
}
Alles anzeigen
Manifest
Code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.herrm_no.bluetoothtethering">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".BluetoothTethering">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Alles anzeigen