set Pin for blue tooth device - Android android.bluetooth

Android examples for android.bluetooth:Bluetooth

Description

set Pin for blue tooth device

Demo Code

import java.lang.reflect.Method;

import android.bluetooth.BluetoothDevice;

public class Main {

  static public boolean setPin(Class<?> btClass, BluetoothDevice btDevice, String str) throws Exception {
    try {//from   w w w .  j ava  2s .  com
      boolean result = false;
      int count = 0;
      Method removeBondMethod = btClass.getDeclaredMethod("setPin", new Class[] { byte[].class });
      while (!result && count < 3) {
        result = (Boolean) removeBondMethod.invoke(btDevice, str.getBytes());
        count++;
      }

    } catch (SecurityException e) {
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return true;

  }

}

Related Tutorials