2014-04-04 10 views
5

Sto provando a creare un'applicazione come "Bluetooth tethering automatico" nel Play Store. Ho letto sul forum che Android è molto attento alla sicurezza e non abiliterà questa impostazione senza l'interazione dell'utente.Abilita il tethering del bluetooth Android a livello di programmazione

Ho bisogno di alcune spiegazioni su come abilitare il tethering Bluetooth.

Grazie

risposta

1

Qui trovate una domanda simile: Bluetooth question

Basta sostituire "isTetheringOn" con "setBluetoothTethering" nella chiamata riflessione e passare un parametro booleano. Dovrebbe funzionare.

+0

Non funziona per me. L'ho implementato e non succede nulla – Bek

+0

Hai aggiunto tutte le autorizzazioni in manifest? – Lorelorelore

+0

sì già aggiunto – Bek

4

Non so se questo è ancora un problema o meno, ma ho scoperto che funziona con il metodo connect nella chiamata di riflessione. Lavorando fuori del codice che pmont utilizzato dal link in risposta Lorelorelore s':

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(); 
     } 
    } 
} 

Naturalmente, questo presuppone che il Bluetooth sia abilitato, e si dispone di un solo dispositivo abbinato. Ma abilitare il bluetooth è piuttosto semplice usando chiamate standard (non di riflessione), e puoi semplicemente controllare il dispositivo associato a cui desideri connetterti nel ciclo for. Inoltre, non dimenticare la classe BTPanServiceListener dall'altra risposta.

Spero che questo aiuti.

+0

Funziona per dispositivi standard. Tuttavia, sono su un bordo T-mobile S7, e Tappa schifoso rimuove il supporto per il tethering Bluetooth. Anche dopo aver eseguito questo codice, non ho tethering BT. Qualcuno può suggerire un sostituto? Potrebbe essere creare una VPN virtuale o qualcosa su BT? – Taranfx

2

Il seguente codice funziona perfettamente per me

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

La soluzione di cui sopra richiesto alcune modifiche al fine di lavorare per me. Nello specifico, il codice per abilitare il tethering deve essere nel metodo OnServiceConnected(). Anche io ho le seguenti autorizzazioni impostate nel manifesto:

<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" /> 

Ecco la mia soluzione:

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

Funziona su N? Hai un'app pubblicata per i pigri tra noi? :) – w00t