2014-04-04 21 views

Odpowiedz

1

Tutaj możesz znaleźć podobne pytanie: Bluetooth question

Wystarczy zastąpić „isTetheringOn” z „setBluetoothTethering” w wywołaniu refleksji i przechodzą w logiczną parametru. Powinno działać.

+0

To nie działa dla mnie. Zaimplementowałem to i nic się nie dzieje – Bek

+0

Czy dodałeś wszystkie uprawnienia do manifestu? – Lorelorelore

+0

yes already added – Bek

4

Nie wiem, czy to nadal jest problem, czy nie, ale znalazłem, że przy użyciu metody connect w odbiciu połączenia działa. Praca off z kodem, który pmont używane z linku w odpowiedzi 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(); 
     } 
    } 
} 

Oczywiście, to zakłada, że ​​Bluetooth jest włączony, a masz tylko jedno powiązane urządzenie. Ale włączenie bluetooth jest dość proste przy użyciu standardowych (nie refleksyjnych) wywołań, a możesz po prostu sprawdzić sparowane urządzenie, z którym chcesz się połączyć w pętli for. Nie zapomnij także o klasie BTPanServiceListener z drugiej odpowiedzi.

Mam nadzieję, że to pomoże.

+0

Działa to dla standardowych urządzeń. Jednak jestem na krawędzi S7 mobilnej T-Mobile, a brzydki Tmobile usuwa wsparcie dla tetheringu Bluetooth. Nawet po uruchomieniu tego kodu nie mam tetheringu BT. Czy ktoś może zaproponować zastępcę? Czy można stworzyć wirtualny VPN lub coś ponad BT? – Taranfx

2

Poniższy kod działa idealnie dla mnie

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

Rozwiązanie powyżej wymaganego pewnej modyfikacji, aby pracować dla mnie. W szczególności kod umożliwiający tethering musi znajdować się w metodzie OnServiceConnected(). Mam także następujące uprawnienia określone w manifeście:

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

Oto moje rozwiązanie:

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

Czy to działa na N? Czy masz opublikowaną aplikację dla leniwych wśród nas? :) – w00t

Powiązane problemy