首先 网上通用工具类
public class ClsUtils { /** * /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(); } /** * /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<? extends BluetoothDevice> 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 block e.printStackTrace(); } return true; } // static public boolean cancelPairingUserInput(Class<?> btClass, BluetoothDevice device) throws Exception { Method createBondMethod = btClass.getMethod("cancelPairingUserInput"); // cancelBondProcess(btClass, device); 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(); } // static public void setPairingConfirmation(Class<?> btClass, BluetoothDevice device, boolean isConfirm) throws Exception { Method setPairingConfirmation = btClass.getDeclaredMethod("setPairingConfirmation", boolean.class); setPairingConfirmation.invoke(device, isConfirm); } /** * @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 block e.printStackTrace(); } } }网上直接找就ok 这是我用的一个
搜索完设备 根据蓝牙地址获取mBluetoothDevice
执行 boolean bond = ClsUtils.createBond(mBluetoothDevice.getClass(), mBluetoothDevice);最后监听 配对广播就ok啦
这是我用的
public class BluetoothReceiver extends BroadcastReceiver { private static final String TAG = "BluetoothReceiver"; String pin = "1234"; //此处为你要连接的蓝牙设备的初始密钥,一般为1234或0000 public BluetoothReceiver() { Log.e(TAG, "BluetoothReceiver: 配对弹框广播" ); } //广播接收器,当远程蓝牙设备被发现时,回调函数onReceiver()会被执行 @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); //得到action Log.e("action1=", action); BluetoothDevice btDevice = null; //创建一个蓝牙device对象 // 从Intent中获取设备对象 btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //得到的action,会等于PAIRING_REQUEST // BluetoothDevice.ACTION_PAIRING_REQUEST if (action.equals("android.bluetooth.device.action.PAIRING_REQUEST")) { Log.e("发现设备2:", "" + btDevice.getName() + ":" + btDevice.getAddress()); try { //1.确认配对 ClsUtils.setPairingConfirmation(btDevice.getClass(), btDevice, true); //2.终止有序广播 Log.e("order...", "isOrderedBroadcast:" + isOrderedBroadcast() + ",isInitialStickyBroadcast:" + isInitialStickyBroadcast()); abortBroadcast();//如果没有将广播终止,则会出现一个一闪而过的配对框。 //3.调用setPin方法进行配对... boolean ret = ClsUtils.setPin(btDevice.getClass(), btDevice, pin); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Log.e(TAG, "onReceive: 配对出错啦!" ); } } }}
亲测ok的!
本人小菜鸟 一枚 如有不对敬请指教...