如何实现android蓝牙开发 自动配对连接,并不弹出提示框

如何实现android蓝牙开发 自动配对连接,并不弹出提示框


之前做一个android版的蓝牙 与血压计通讯的项目,遇到最大的难题就是自动配对.

上网查资料说是用反射createBond()和setPin(),但测试时进行配对还是会出现提示,但配对是成功了

我就开始查找怎么关闭这个蓝牙配对提示框,后面还是伟大的android源码帮助了我。

在源码 BluetoothDevice 类中还有两个隐藏方法

cancelBondProcess()和cancelPairingUserInput()

这两个方法一个是取消配对进程一个是取消用户输入

下面是自动配对的代码

Mainfest,xml注册

 

1 <receiver android:name=".BluetoothConnectActivityReceiver" >
2     <intent-filter>
3         <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
4     </intent-filter>
5 </receiver>

自己在收到广播时处理并将预先输入的密码设置进去

 

01 public class BluetoothConnectActivityReceiver extends BroadcastReceiver
02 {
03  
04     String strPsw = "0";
05  
06     @Override
07     public void onReceive(Context context, Intent intent)
08     {
09         // TODO Auto-generated method stub
10         if (intent.getAction().equals(
11                 "android.bluetooth.device.action.PAIRING_REQUEST"))
12         {
13             BluetoothDevice btDevice = intent
14                     .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
15  
16             // byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234");
17             // device.setPin(pinBytes);
18             Log.i("tag11111""ddd");
19             try
20             {
21                 ClsUtils.setPin(btDevice.getClass(), btDevice, strPsw); // 手机和蓝牙采集器配对
22                 ClsUtils.createBond(btDevice.getClass(), btDevice);
23                 ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice);
24             }
25             catch (Exception e)
26             {
27                 // TODO Auto-generated catch block
28                 e.printStackTrace();
29             }
30         }
31  
32  
33     }
34 }

 

001 <b>/************************************ 蓝牙配对函数 * **************/
002 import java.lang.reflect.Field;
003 import java.lang.reflect.Method;
004  
005 import android.bluetooth.BluetoothDevice;
006 import android.util.Log;
007 public class ClsUtils
008 {
009  
010     /**
011      * 与设备配对 参考源码:platform/packages/apps/Settings.git
012      * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
013      */
014     static public boolean createBond(Class btClass, BluetoothDevice btDevice)
015             throws Exception
016     {
017         Method createBondMethod = btClass.getMethod("createBond");
018         Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
019         return returnValue.booleanValue();
020     }
021  
022     /**
023      * 与设备解除配对 参考源码:platform/packages/apps/Settings.git
024      * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
025      */
026     static public boolean removeBond(Class btClass, BluetoothDevice btDevice)
027             throws Exception
028     {
029         Method removeBondMethod = btClass.getMethod("removeBond");
030         Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice);
031         return returnValue.booleanValue();
032     }
033  
034     static public boolean setPin(Class btClass, BluetoothDevice btDevice,
035             String str) throws Exception
036     {
037         try
038         {
039             Method removeBondMethod = btClass.getDeclaredMethod("setPin",
040                     new Class[]
041                     {byte[].class});
042             Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice,
043                     new Object[]
044                     {str.getBytes()});
045             Log.e("returnValue""" + returnValue);
046         }
047         catch (SecurityException e)
048         {
049             // throw new RuntimeException(e.getMessage());
050             e.printStackTrace();
051         }
052         catch (IllegalArgumentException e)
053         {
054             // throw new RuntimeException(e.getMessage());
055             e.printStackTrace();
056         }
057         catch (Exception e)
058         {
059             // TODO Auto-generated catch block
060             e.printStackTrace();
061         }
062         return true;
063  
064     }
065  
066     // 取消用户输入
067     static public boolean cancelPairingUserInput(Class btClass,
068             BluetoothDevice device)
069  
070     throws Exception
071     {
072         Method createBondMethod = btClass.getMethod("cancelPairingUserInput");
073         // cancelBondProcess()
074         Boolean returnValue = (Boolean) createBondMethod.invoke(device);
075         return returnValue.booleanValue();
076     }
077  
078     // 取消配对
079     static public boolean cancelBondProcess(Class btClass,
080             BluetoothDevice device)
081  
082     throws Exception
083     {
084         Method createBondMethod = btClass.getMethod("cancelBondProcess");
085         Boolean returnValue = (Boolean) createBondMethod.invoke(device);
086         return returnValue.booleanValue();
087     }
088  
089     /**
090      *
091      * @param clsShow
092      */
093     static public void printAllInform(Class clsShow)
094     {
095         try
096         {
097             // 取得所有方法
098             Method[] hideMethod = clsShow.getMethods();
099             int i = 0;
100             for (; i < hideMethod.length; i++)
101             {
102                 Log.e("method name", hideMethod[i].getName() + ";and the i is:"
103                         + i);
104             }
105             // 取得所有常量
106             Field[] allFields = clsShow.getFields();
107             for (i = 0; i < allFields.length; i++)
108             {
109                 Log.e("Field name", allFields[i].getName());
110             }
111         }
112         catch (SecurityException e)
113         {
114             // throw new RuntimeException(e.getMessage());
115             e.printStackTrace();
116         }
117         catch (IllegalArgumentException e)
118         {
119             // throw new RuntimeException(e.getMessage());
120             e.printStackTrace();
121         }
122         catch (Exception e)
123         {
124             // TODO Auto-generated catch block
125             e.printStackTrace();
126         }
127     }
128 }</b>

 执行时直接使用:

 

01 <b>public static boolean pair(String strAddr, String strPsw)
02     {
03         boolean result = false;
04         BluetoothAdapter bluetoothAdapter = BluetoothAdapter
05                 .getDefaultAdapter();
06  
07         bluetoothAdapter.cancelDiscovery();
08  
09         if (!bluetoothAdapter.isEnabled())
10         {
11             bluetoothAdapter.enable();
12         }
13  
14         if (!BluetoothAdapter.checkBluetoothAddress(strAddr))
15         // 检查蓝牙地址是否有效
16  
17             Log.d("mylog""devAdd un effient!");
18         }
19  
20         BluetoothDevice device = bluetoothAdapter.getRemoteDevice(strAddr);
21  
22         if (device.getBondState() != BluetoothDevice.BOND_BONDED)
23         {
24             try
25             {
26                 Log.d("mylog""NOT BOND_BONDED");
27                 ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对
28                 ClsUtils.createBond(device.getClass(), device);
29                 remoteDevice = device; // 配对完毕就把这个设备对象传给全局的remoteDevice
30                 result = true;
31             }
32             catch (Exception e)
33             {
34                 // TODO Auto-generated catch block
35  
36                 Log.d("mylog""setPiN failed!");
37                 e.printStackTrace();
38             //
39  
40         }
41         else
42         {
43             Log.d("mylog""HAS BOND_BONDED");
44             try
45             {
46                 ClsUtils.createBond(device.getClass(), device);
47                 ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对
48                 ClsUtils.createBond(device.getClass(), device);
49                 remoteDevice = device; // 如果绑定成功,就直接把这个设备对象传给全局的remoteDevice
50                 result = true;
51             }
52             catch (Exception e)
53             {
54                 // TODO Auto-generated catch block
55                 Log.d("mylog""setPiN failed!");
56                 e.printStackTrace();
57             }
58         }
59         return result;
60     }</b>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 蓝牙配对指的是在Android设备中,当我们打开蓝牙连接功能时,会弹一个配对框,用于配对连接其他蓝牙设备。 在Android中,我们可以通过以下步骤实现蓝牙配对: 1. 打开蓝牙:在应用中的相应位置,通过调用蓝牙适配器(BluetoothAdapter)的enable()方法,可以打开设备的蓝牙功能。 2. 搜索设备:调用蓝牙适配器的startDiscovery()方法,会开始搜索附近的蓝牙设备。搜索过程可以通过广播接收器(BroadcastReceiver)监听蓝牙设备的变化。 3. 弹配对框:当检测到附近有新的蓝牙设备时,Android系统会自动在设备上弹配对框,显示可用的蓝牙设备列表。 4. 选择要配对的设备:在配对框中,用户可以选择要配对的设备。通常会显示蓝牙设备的名称和MAC地址等信息。 5. 开始配对:用户选择要配对的设备后,点击确认按钮,系统会发起配对请求。在这个过程中,系统会尝试与被选择的设备建立安全连接。 6. 配对结果:配对过程完成后,系统会返回配对结果。如果配对成功,我们可以获取到连接到的蓝牙设备对象,可以通过该对象与蓝牙设备进行数据传输。 需要注意的是,不同的Android版本或设备可能会有一些差异,因此在开发过程中需要根据不同情况进行适配。同时,还需要处理一些配对失败或取消配对的情况,以提高用户体验。 总之,Android蓝牙配对是通过系统级的弹框选择和处理蓝牙设备的配对过程,让用户在使用蓝牙功能时能够方便地与其他设备进行连接和交互。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值