安卓蓝牙自动pin码

news/2025/1/18 4:33:59/

开发一蓝牙项目,配对时需要输入密码,实在是太不方便了,想实现隐藏输入密码的过程。

参考: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();}}}};

http://www.ppmy.cn/news/510676.html

相关文章

ble 设置固定的pin码_1.设置非固定长度的PIN码

ble 设置固定的pin码 When compared to its rival Android, iOS has always had a better reputation in security and privacy terms, becoming a highlight in all Apple marketing campaigns. This security is also grounded in the system restrictions, since Apple keep…

java 连接蓝牙 有密码_Android自动配对(绑定)蓝牙(BLE)设备,带有密码

我试图在没有用户交互的情况下配对蓝牙(BLE)设备 - 意味着配对将仅以编程方式完成,用户不会选择蓝牙设备而不会输入密码 . 我正在使用以下代码: //request receiver IntentFilter pairingRequestFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST); pairing…

考研C语言第八章

结构体定义&#xff0c;初始化&#xff0c;结构体数组 结构体对齐 这个东西看着像数据库里面属性的定义&#xff0c;也像java里面的类的定义 #include <stdio.h> #include <string.h> #include <stdlib.h>struct student{int num;char name[20];char sex;i…

Android 蓝牙自动匹配PIN码跳过用户交互

近期项目中需要连接蓝牙设备&#xff0c;起初只是设置蓝牙列表界面让用户点击然后输入默认PIN码&#xff0c;后来改需求了 &#xff0c;要求自动连接指定设备并不需要用户手动输入PIN码&#xff0c;作为Android 小白的我是拒绝的&#xff0c;但是拒绝有什么用~ 首先说一下之后…

笔记本蓝牙显示输入码无效_蓝牙键盘输入码错误无法连接笔记本(win10)

在研究室用三星笔记本写论文&#xff0c;ctrl键敲坏了(现在看来是复制黏贴用的太多) 于是买了个蓝牙键盘&#xff0c;省的跑a/s浪费时间&#xff0c;可是花了一上午才终于搞定了&#xff0c;还不如上午去趟客服分分钟换个ctrl键来的简单...... 那如何解决蓝牙键盘不能连接笔记本…

手机蓝牙连接小票机/打印机,输入PIN码后确定键不亮的解决办法

如果以前配对过&#xff0c;就在手机蓝牙里取消配对&#xff0c;然后把机器关掉&#xff0c;手机蓝牙关掉。 重新配对连接&#xff0c;可以用了。 就没有重启不能解决的问题&#xff0c;如果有&#xff0c;就重启两遍。

3、低功耗蓝牙(BLE)配对和解绑

本文将对以上问题进行论述。 什么是低功耗蓝牙配对&#xff1f;什么又是绑定&#xff1f;配对和绑定有什么区别&#xff1f;配对有什么好处&#xff1f;如何删除绑定信息&#xff1f;如何确定配对的安全等级&#xff1f;just work的配对一定就不安全吗&#xff1f;如何开…

请解释下为什么鹿晗发布恋情的时候,微博系统会崩溃,如何解决?

出题人&#xff1a;阿里巴巴出题专家&#xff1a;江岚&#xff0f;阿里巴巴数据技术高级技术专家 参考答案&#xff1a; A. 获取微博通过 pull 方式还是 push 方式 B. 发布微博的频率要远小于阅读微博 C. 流量明星的发微博&#xff0c;和普通博主要区分对待&#xff0c;比如…