2014-04-04 13 views
5

Ich versuche, eine Anwendung wie "Bluetooth Auto Tethering" auf Play Store zu machen. Ich habe im Forum gelesen, dass Android sehr sicherheitsbewusst ist und diese Einstellung nicht ohne Benutzerinteraktion aktivieren wird.Aktivieren Sie Bluetooth Tethering Android programmgesteuert

Ich brauche einige Erklärungen darüber, wie Bluetooth Tethering aktivieren.

Danke

Antwort

1

Hier finden Sie eine ähnliche Frage finden: Bluetooth question

Ersetzen Sie einfach "isTetheringOn" mit "setBluetoothTethering" im Reflexions Aufruf und in einem boolean-Parameter übergeben. Es sollte funktionieren.

+0

Es ist nicht für mich arbeiten. Ich habe es implementiert und nichts passiert – Bek

+0

Haben Sie alle Berechtigungen in Manifest hinzugefügt? – Lorelorelore

+0

ja bereits hinzugefügt – Bek

4

Ich weiß nicht, ob das immer noch ein Problem ist oder nicht, aber ich habe festgestellt, dass die Verwendung der connect Methode im Reflection Call funktioniert. Abarbeiten des Codes, der pmont aus dem Link verwendet in Lorelorelore ‚s Antwort:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
Class<?> classBluetoothPan = null; 
Constructor<?> BTPanCtor = null; 
Object BTSrvInstance = null; 
Method mBTPanConnect; 

try { 
    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(myContext, new BTPanServiceListener(myContext)); 
} catch (ClassNotFoundException e) { 
    e.printStackTrace(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
// If there are paired devices 
if (pairedDevices.size() > 0) { 
    // Loop through paired devices 
    for (BluetoothDevice device : pairedDevices) { 
     try{ 
      mBTPanConnect.invoke(BTSrvInstance, device); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

Natürlich setzt dies voraus, dass die Bluetooth aktiviert ist und Sie nur ein gekoppeltes Gerät haben. Das Aktivieren von Bluetooth ist jedoch ziemlich einfach, wenn Sie Standardanrufe (keine Spiegelung) verwenden, und Sie können einfach in der Schleife for nach dem gekoppelten Gerät suchen, mit dem Sie eine Verbindung herstellen möchten. Vergessen Sie auch nicht die BTPanServiceListener Klasse von der anderen Antwort.

Hoffe, das hilft.

+0

Dies funktioniert für Standardgeräte. Allerdings bin ich auf einer T-Mobile S7-Kante, und das beschissene Tmobile entfernt die Unterstützung für Bluetooth-Tethering. Auch nach dem Ausführen dieses Codes habe ich kein BT-Tethering. Kann jemand eine Alternative vorschlagen? Kann ein virtuelles VPN oder etwas über BT erstellt werden? – Taranfx

2

Der Code unten funktioniert für mich perfekt

String sClassName = "android.bluetooth.BluetoothPan"; 

try { 

    Class<?> classBluetoothPan = Class.forName(sClassName); 

    Constructor<?> ctor = classBluetoothPan.getDeclaredConstructor(Context.class, BluetoothProfile.ServiceListener.class); 
    ctor.setAccessible(true); 
    Object instance = ctor.newInstance(getApplicationContext(), new BTPanServiceListener(getApplicationContext()));     
    // Set Tethering ON 
    Class[] paramSet = new Class[1]; 
    paramSet[0] = boolean.class; 

    Method setTetheringOn = classBluetoothPan.getDeclaredMethod("setBluetoothTethering", paramSet); 

    setTetheringOn.invoke(instance,true); 

} catch (ClassNotFoundException e) { 
    e.printStackTrace(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

public class BTPanServiceListener implements BluetoothProfile.ServiceListener { 

    private final Context context; 

    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.e("MyApp", "BTPan proxy connected"); 

    } 

    @Override 
    public void onServiceDisconnected(final int profile) { 
    } 
} 
2

Die Lösung oben erforderlich einige Änderungen, um für mich zu arbeiten. Insbesondere muss der Code zum Aktivieren von Tethering in der OnServiceConnected() -Methode enthalten sein. Auch habe ich die folgenden Berechtigungen in dem Manifest:

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> 
<uses-permission android:name="android.permission.BLUETOOTH" /> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> 

Hier ist meine Lösung:

public class BluetoothTethering extends ActionBarActivity { 

    Object instance = null; 
    Method setTetheringOn = null; 
    Method isTetheringOn = null; 
    Object mutex = new Object(); 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_bluetooth_tethering); 
     String sClassName = "android.bluetooth.BluetoothPan"; 

     try { 

      Class<?> 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) { 
       setTetheringOn = classBluetoothPan.getDeclaredMethod("setBluetoothTethering", paramSet); 
       isTetheringOn = classBluetoothPan.getDeclaredMethod("isTetheringOn", null); 
       instance = ctor.newInstance(getApplicationContext(), new BTPanServiceListener(getApplicationContext())); 
      } 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public class BTPanServiceListener implements BluetoothProfile.ServiceListener { 

     private final Context context; 

     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. 

      try { 
       synchronized (mutex) { 
        setTetheringOn.invoke(instance, true); 
        if ((Boolean)isTetheringOn.invoke(instance, null)) { 
         Toast.makeText(getApplicationContext(), "BT Tethering is on", Toast.LENGTH_LONG).show(); 
        } 
        else { 
         Toast.makeText(getApplicationContext(), "BT Tethering is off", Toast.LENGTH_LONG).show(); 
        } 
       } 
      } 
      catch (InvocationTargetException e) { 
       e.printStackTrace(); 
      } 
      catch (IllegalAccessException e) { 
       e.printStackTrace(); 
      } 
     } 

     @Override 
     public void onServiceDisconnected(final int profile) { 
     } 
    } 
} 
+1

Funktioniert das auf N? Hast du eine veröffentlichte App für die Faulen unter uns? :) – w00t