开发一蓝牙项目,配对时需要输入密码,实在是太不方便了,想实现隐藏输入密码的过程。
参考:android蓝牙配对 自动联接,如何实现android蓝牙开发 自动配对连接,并不弹出提示框
代码如下:
先新建一个名为 ClsUtils.java 的类,内容为:
这个类的功能非常的多,其中,也有去除某个已配对的蓝牙的功能 。
package com.example.android.bluetoothchat;// hui 参考: https://blog.csdn.net/weixin_28630185/article/details/117277259?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167387420816800211545191%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=167387420816800211545191&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~pc_rank_34-10-117277259-null-null.142^v71^pc_new_rank,201^v4^add_ask&utm_term=%E5%AE%89%E5%8D%93%E8%93%9D%E7%89%99%E8%87%AA%E5%8A%A8pin%E7%A0%81&spm=1018.2226.3001.4187/************************************ 蓝牙配对函数 * **************/import java.lang.reflect.Field;
import java.lang.reflect.Method;
import android.bluetooth.BluetoothDevice;
import android.util.Log;
public class ClsUtils
{/*** 与设备配对 参考源码:platform/packages/apps/Settings.git* /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java*/static public boolean createBond(Class btClass, BluetoothDevice btDevice)throws Exception{Method createBondMethod = btClass.getMethod("createBond");Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);return returnValue.booleanValue();}/*** 与设备解除配对 参考源码:platform/packages/apps/Settings.git* /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java*/static public boolean removeBond(Class btClass, BluetoothDevice btDevice)throws Exception{Method removeBondMethod = btClass.getMethod("removeBond");Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice);return returnValue.booleanValue();}static public boolean setPin(Class btClass, BluetoothDevice btDevice,String str) throws Exception{try{Method removeBondMethod = btClass.getDeclaredMethod("setPin", new Class[]{byte[].class});Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, new Object[]{str.getBytes()});Log.e("returnValue", "" + returnValue);}catch (SecurityException e){// throw new RuntimeException(e.getMessage());e.printStackTrace();}catch (IllegalArgumentException e){// throw new RuntimeException(e.getMessage());e.printStackTrace();}catch (Exception e){// TODO Auto-generated catch blocke.printStackTrace();}return true;}// 取消用户输入static public boolean cancelPairingUserInput(Class btClass, BluetoothDevice device) throws Exception{Method createBondMethod = btClass.getMethod("cancelPairingUserInput");// cancelBondProcess()Boolean returnValue = (Boolean) createBondMethod.invoke(device);return returnValue.booleanValue();}// 取消配对static public boolean cancelBondProcess(Class btClass, BluetoothDevice device) throws Exception{Method createBondMethod = btClass.getMethod("cancelBondProcess");Boolean returnValue = (Boolean) createBondMethod.invoke(device);return returnValue.booleanValue();}/**** @param clsShow*/static public void printAllInform(Class clsShow){try{// 取得所有方法Method[] hideMethod = clsShow.getMethods();int i = 0;for (; i < hideMethod.length; i++){Log.e("method name", hideMethod[i].getName() + ";and the i is:" + i);}// 取得所有常量Field[] allFields = clsShow.getFields();for (i = 0; i < allFields.length; i++){Log.e("Field name", allFields[i].getName());}}catch (SecurityException e){// throw new RuntimeException(e.getMessage());e.printStackTrace();}catch (IllegalArgumentException e){// throw new RuntimeException(e.getMessage());e.printStackTrace();}catch (Exception e){// TODO Auto-generated catch blocke.printStackTrace();}}}/*//执行时直接使用:public static boolean pair(String strAddr, String strPsw){boolean result = false;BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();bluetoothAdapter.cancelDiscovery();if (!bluetoothAdapter.isEnabled()){bluetoothAdapter.enable();}if (!BluetoothAdapter.checkBluetoothAddress(strAddr)){ // 检查蓝牙地址是否有效Log.d("mylog", "devAdd un effient!");}BluetoothDevice device = bluetoothAdapter.getRemoteDevice(strAddr);if (device.getBondState() != BluetoothDevice.BOND_BONDED){try{Log.d("mylog", "NOT BOND_BONDED");ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对ClsUtils.createBond(device.getClass(), device);remoteDevice = device; // 配对完毕就把这个设备对象传给全局的remoteDeviceresult = true;}catch (Exception e){// TODO Auto-generated catch blockLog.d("mylog", "setPiN failed!");e.printStackTrace();} //}else{Log.d("mylog", "HAS BOND_BONDED");try{ClsUtils.createBond(device.getClass(), device);ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对ClsUtils.createBond(device.getClass(), device);remoteDevice = device; // 如果绑定成功,就直接把这个设备对象传给全局的remoteDeviceresult = true;}catch (Exception e){// TODO Auto-generated catch blockLog.d("mylog", "setPiN failed!");e.printStackTrace();}}return result;}*/
第2步,在蓝牙工程里搜索关键字 “BroadcastReceiver”,从而定位到广播处理函数,在这个函数加入配对时的处理代码,如下:
/*** The BroadcastReceiver that listens for discovered devices and changes the title when* discovery is finished*/private final BroadcastReceiver mReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();// When discovery finds a deviceif (BluetoothDevice.ACTION_FOUND.equals(action)) {//.............} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {//..............} else if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")){ // hui add 跳过输入pin码对话框。BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);// byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234");// device.setPin(pinBytes);String strPsw = "1234";Log.i("tag11111", "ddd");try{ClsUtils.setPin(btDevice.getClass(), btDevice, strPsw); // 手机和蓝牙采集器配对ClsUtils.createBond(btDevice.getClass(), btDevice);ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice);}catch (Exception e){// TODO Auto-generated catch blocke.printStackTrace();}}}};