. f Android Turn on Bluetooth Tethering | Android enable tethering ~ Android Developers Blog

Saturday, 19 December 2015

Android Turn on Bluetooth Tethering | Android enable tethering

Hello Friends ,
          Today, I am sharing my another blog on Bluetooth Tethering.  In this tutorial
we can programmatically turn on and off  Bluetooth Tethering.

 





















Here the code:

1. BluetoothTethering.java


  1. package com.solutions.developer.android.com.bluetoothtethering;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.bluetooth.BluetoothAdapter;  
  5. import android.bluetooth.BluetoothManager;  
  6. import android.bluetooth.BluetoothProfile;  
  7. import android.content.Context;  
  8. import android.os.Bundle;  
  9. import android.support.v7.app.AppCompatActivity;  
  10. import android.view.Menu;  
  11. import android.view.MenuItem;  
  12. import android.widget.CompoundButton;  
  13. import android.widget.Switch;  
  14.   
  15. import java.lang.reflect.Constructor;  
  16. import java.lang.reflect.Method;  
  17.   
  18.   
  19. public class BluetoothTethering extends AppCompatActivity {  
  20.   
  21.     BluetoothAdapter mBluetoothAdapter = null;  
  22.     Class classBluetoothPan = null;  
  23.     Constructor BTPanCtor = null;  
  24.     Object BTSrvInstance = null;  
  25.     Class noparams[] = {};  
  26.     Method mIsBTTetheringOn;  
  27.     public static Switch toggle ;  
  28.   
  29.     @Override  
  30.     protected void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.activity_bluetooth_tethering);  
  33.   
  34.         toggle = (Switch) findViewById(R.id.wifi_switch);  
  35.         toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {  
  36.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  37.                 try {  
  38.                     mBluetoothAdapter = getBTAdapter();  
  39.                     mBluetoothAdapter.enable();  
  40.                     Thread.sleep(100);  
  41.                     toggleTethering();  
  42.                 } catch (InterruptedException e) {  
  43.                     e.printStackTrace();  
  44.                 }  
  45.             }  
  46.         });  
  47.     }  
  48.   
  49.   
  50.     @Override  
  51.     public boolean onCreateOptionsMenu(Menu menu) {  
  52.         // Inflate the menu; this adds items to the action bar if it is present.  
  53.         getMenuInflater().inflate(R.menu.menu_bluetooth_tethering, menu);  
  54.         return true;  
  55.     }  
  56.   
  57.     @Override  
  58.     public boolean onOptionsItemSelected(MenuItem item) {  
  59.         // Handle action bar item clicks here. The action bar will  
  60.         // automatically handle clicks on the Home/Up button, so long  
  61.         // as you specify a parent activity in AndroidManifest.xml.  
  62.         int id = item.getItemId();  
  63.   
  64.         //noinspection SimplifiableIfStatement  
  65.         if (id == R.id.action_settings) {  
  66.             return true;  
  67.         }  
  68.   
  69.         return super.onOptionsItemSelected(item);  
  70.     }  
  71.   
  72.     public void toggleTethering() {  
  73.         Context MyContext = getApplicationContext();  
  74.         mBluetoothAdapter = getBTAdapter();  
  75.         String sClassName = "android.bluetooth.BluetoothPan";  
  76.         try {  
  77.             classBluetoothPan = Class.forName("android.bluetooth.BluetoothPan");  
  78.             mIsBTTetheringOn = classBluetoothPan.getDeclaredMethod("isTetheringOn", noparams);  
  79.             BTPanCtor = classBluetoothPan.getDeclaredConstructor(Context.class, BluetoothProfile.ServiceListener.class);  
  80.             BTPanCtor.setAccessible(true);  
  81.   
  82.             BTSrvInstance = BTPanCtor.newInstance(MyContext, new BTPanServiceListener(MyContext));  
  83.             Thread.sleep(250);  
  84.   
  85.         } catch (ClassNotFoundException e) {  
  86.             e.printStackTrace();  
  87.         } catch (Exception e) {  
  88.             e.printStackTrace();  
  89.         }  
  90.     }  
  91.   
  92.     @SuppressLint("NewApi")  
  93.     private BluetoothAdapter getBTAdapter() {  
  94.         if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1)  
  95.             return BluetoothAdapter.getDefaultAdapter();  
  96.         else {  
  97.             BluetoothManager bm = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);  
  98.             return bm.getAdapter();  
  99.         }  
  100.     }  
  101.   
  102.     public  static void changeToggleState(boolean state) {  
  103.         try{  
  104.             if(state){  
  105.                 toggle.setChecked(BTPanServiceListener.state);  
  106.             }else {  
  107.                 toggle.setChecked(BTPanServiceListener.state);  
  108.             }  
  109.         }catch (Exception e){  
  110.             e.printStackTrace();  
  111.         }  
  112.     }  
  113. }  
  114.    

2. BTPanServiceListener.java




  1. package com.solutions.developer.android.com.bluetoothtethering;  
  2.   
  3. import java.lang.reflect.InvocationTargetException;  
  4.   
  5. import android.bluetooth.BluetoothProfile;  
  6. import android.content.Context;  
  7. import android.util.Log;  
  8. import android.widget.Toast;  
  9.   
  10. public class BTPanServiceListener implements BluetoothProfile.ServiceListener {  
  11.     private final Context context;  
  12.     public static boolean state = false;  
  13.   
  14.     public BTPanServiceListener(final Context context) {  
  15.         this.context = context;  
  16.     }  
  17.   
  18.     @Override  
  19.     public void onServiceConnected(final int profile,  
  20.                                    final BluetoothProfile proxy) {  
  21.         //Some code must be here or the compiler will optimize away this callback.  
  22.         Log.i("MyApp""BTPan proxy connected");  
  23.         try {  
  24.             boolean nowVal = ((Boolean) proxy.getClass().getMethod("isTetheringOn"new Class[0]).invoke(proxy, new Object[0])).booleanValue();  
  25.             if (nowVal) {  
  26.                 proxy.getClass().getMethod("setBluetoothTethering"new Class[]{Boolean.TYPE}).invoke(proxy, new Object[]{Boolean.valueOf(false)});  
  27.                 Toast.makeText(context, "Turning bluetooth tethering off", Toast.LENGTH_SHORT).show();  
  28.                 state = false;  
  29.             } else {  
  30.                 proxy.getClass().getMethod("setBluetoothTethering"new Class[]{Boolean.TYPE}).invoke(proxy, new Object[]{Boolean.valueOf(true)});  
  31.                 Toast.makeText(context, "Turning bluetooth tethering on", Toast.LENGTH_SHORT).show();  
  32.                 state = true;  
  33.             }  
  34.             BluetoothTethering.changeToggleState(state);  
  35.         } catch (IllegalAccessException e) {  
  36.             // TODO Auto-generated catch block  
  37.             e.printStackTrace();  
  38.         } catch (IllegalArgumentException e) {  
  39.             // TODO Auto-generated catch block  
  40.             e.printStackTrace();  
  41.         } catch (InvocationTargetException e) {  
  42.             // TODO Auto-generated catch block  
  43.             e.printStackTrace();  
  44.         } catch (NoSuchMethodException e) {  
  45.             // TODO Auto-generated catch block  
  46.             e.printStackTrace();  
  47.         }  
  48.     }  
  49.   
  50.     @Override  
  51.     public void onServiceDisconnected(final int profile) {  
  52.     }  
  53. }   



Download Complete Code : BluetoothTethering

Hope this will help some one.
Enjoy coding....... :)

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developers Blog.