Android Open Source - BluetoothSppPro Bluetooth Ctrl






From Project

Back to project page BluetoothSppPro.

License

The source code is released under:

Apache License

If you think the Android project BluetoothSppPro listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package mobi.dzs.android.bluetooth;
/*from www.  j  a  va2s .com*/

import java.lang.reflect.Method;

import android.bluetooth.BluetoothDevice;
import android.util.Log;
/**
 * ???????API?????????
 * */
public class BluetoothCtrl{
  /**???:???????????????????*/
  static public final String PAIRING_REQUEST = "android.bluetooth.device.action.PAIRING_REQUEST";
    /** 
     * ???????????
     * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java 
     */  
    static public boolean createBond(BluetoothDevice btDevice)  
      throws Exception
    {  
      Class<? extends BluetoothDevice> btClass = btDevice.getClass();
        Method createBondMethod = btClass.getMethod("createBond");  
        Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);  
        return returnValue.booleanValue();  
    }  
  
    /** 
     * ???????????
     * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java 
     */  
    static public boolean removeBond(BluetoothDevice btDevice)  
      throws Exception
    {  
      Class<? extends BluetoothDevice> btClass = btDevice.getClass();
        Method removeBondMethod = btClass.getMethod("removeBond");  
        Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice);  
        return returnValue.booleanValue();  
    }  
  
    /** 
     * ????????
     * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java 
     */ 
    static public boolean setPin(BluetoothDevice btDevice, String str) 
      throws Exception
    {
      Boolean returnValue = false;
        try{   
          Class<? extends BluetoothDevice> btClass = btDevice.getClass();
            Method removeBondMethod = btClass.getDeclaredMethod("setPin", new Class[] { byte[].class });
//          byte[] ar = new byte[]{0x31,0x32,0x33,0x34};
//          Method removeBondMethod = 
//            btClass.getMethod("setPin",new Class[]{Array.newInstance(byte.class,4).getClass()}); 
//            Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, ar);
            returnValue = (Boolean) removeBondMethod.invoke(btDevice, new Object[] { str.getBytes() });  
            Log.d("returnValue", ">>setPin:" + returnValue.toString());  
        }catch (SecurityException e){  
            // throw new RuntimeException(e.getMessage());
          Log.e("returnValue", ">>setPin:" + e.getMessage());  
            e.printStackTrace();  
        }catch (IllegalArgumentException e){  
            // throw new RuntimeException(e.getMessage());
          Log.e("returnValue", ">>setPin:" + e.getMessage());
            e.printStackTrace();  
        }catch (Exception e){  
          Log.e("returnValue", ">>setPin:" + e.getMessage());  
            e.printStackTrace();  
        }  
        return returnValue.booleanValue();  
    }
    
    /**
     * cancel Pairing User Input
     * @see android.permission.WRITE_SECURE_SETTINGS<br/>
     *      Permission is only granted to system apps
     * */
  static public boolean cancelPairingUserInput(BluetoothDevice btDevice)
    throws Exception
  {
    Class<? extends BluetoothDevice> btClass = btDevice.getClass();
      Method createBondMethod = btClass.getMethod("cancelPairingUserInput");
      // cancelBondProcess()
      Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
      return returnValue.booleanValue();
  }

  /**
   * cancel Bond Process
   * @see android.permission.WRITE_SETTINGS
   * */
  static public boolean cancelBondProcess(BluetoothDevice btDevice)
    throws Exception
  {
    Boolean returnValue = false;
        try
        {  
      Class<? extends BluetoothDevice> btClass = btDevice.getClass();
        Method createBondMethod = btClass.getMethod("cancelBondProcess");
        returnValue = (Boolean) createBondMethod.invoke(btDevice);
        }
        catch (SecurityException e)
        {  
            // throw new RuntimeException(e.getMessage());
          Log.e("returnValue", ">>cancelBondProcess:" + e.getMessage());  
            e.printStackTrace();  
        }
        catch (IllegalArgumentException e)
        {  
            // throw new RuntimeException(e.getMessage());
          Log.e("returnValue", ">>cancelBondProcess:" + e.getMessage());
            e.printStackTrace();  
        }
        catch (Exception e)
        {  
          Log.e("returnValue", ">>cancelBondProcess:" + e.getMessage());  
            e.printStackTrace();  
        }
        return returnValue.booleanValue();
  }
}
/*Example:
//??????:????????????
private BroadcastReceiver _mPairingRequest = new BroadcastReceiver()
{
  @Override
  public void onReceive(Context context, Intent intent)
  {
    BluetoothDevice device = null;
    String msPairPWD = mtvPairPWD.getText().toString();
    if (intent.getAction().equals(BluetoothCtrl.PAIRING_REQUEST))
    {  //?????????????
      device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
      try
      {
        BluetoothCtrl.setPin(device, "1234"); //????????
      }
      catch (Exception e)
      {
        Log.d(getString(R.string.app_name), ">>_mPairingRequest err!");
      }
    }
  }
}
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(strAddr);  
if (device.getBondState() != BluetoothDevice.BOND_BONDED)
{
    try
    {
      BluetoothCtrl.createBond(device); //?????
      remoteDevice = device; //??????????????????remoteDevice
      result=true;
   }
   catch (Exception e)
   {
      Log.d("mylog","setPiN failed!");
      e.printStackTrace();
   }
//}
*/




Java Source Code List

mobi.dzs.android.BLE_SPP_PRO.BaseActivity.java
mobi.dzs.android.BLE_SPP_PRO.BaseCommActivity.java
mobi.dzs.android.BLE_SPP_PRO.actAbout.java
mobi.dzs.android.BLE_SPP_PRO.actByteStream.java
mobi.dzs.android.BLE_SPP_PRO.actCmdLine.java
mobi.dzs.android.BLE_SPP_PRO.actDiscovery.java
mobi.dzs.android.BLE_SPP_PRO.actKeyBoard.java
mobi.dzs.android.BLE_SPP_PRO.actMain.java
mobi.dzs.android.BLE_SPP_PRO.globalPool.java
mobi.dzs.android.bluetooth.BTSerialComm.java
mobi.dzs.android.bluetooth.BluetoothCtrl.java
mobi.dzs.android.bluetooth.BluetoothSppClient.java
mobi.dzs.android.bluetooth.CResourcePV.java
mobi.dzs.android.control.button.ButtonPassListener.java
mobi.dzs.android.control.button.RepeatingButton.java
mobi.dzs.android.storage.CJsonStorage.java
mobi.dzs.android.storage.CKVStorage.java
mobi.dzs.android.storage.CSharedPreferences.java
mobi.dzs.android.util.CHexConver.java
mobi.dzs.android.util.LocalIOTools.java