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

(2013-01-05 15:27:36)

转载▼ 标签:

我就开始查找怎么关闭这个蓝牙配对提示框,后面还是伟大的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 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 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 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 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 }

执行时直接使用:

01 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 }

android 删除蓝牙绑定,android 蓝牙程序控制绑定与删除绑定相关推荐

  1. android ble配对绑定,Android蓝牙(一)搜索配对和绑定

    蓝牙技术在智能硬件方面有很多用武之地,今天我就为大家分享一下蓝牙在Android系统下的使用方法技巧,并实现一下两个终端间数据的传输. 蓝牙(Bluetooth)是一种短距离的无线通信技术标准,蓝牙协 ...

  2. 【Android】蓝牙开发——经典蓝牙:配对与解除配对 实现配对或连接时不弹出配对框

    目录 一.配对方法 二.解除配对方法 三.配对/解除配对结果 四.justwork配对模式下,不弹出配对框 五.pincode配对模式下,不弹出配对框 六.小结 在之前的文章[Android]蓝牙开发 ...

  3. Android深入浅出系列之Bluetooth—蓝牙操作(一)

    一:什么是蓝牙 1:Bluetooth是目前使用最广泛的无线通讯协议,近距离无线通讯的标准.传说瑞典有个国王特别爱吃蓝莓导致自己的牙齿天天都是蓝色的,在他执政期间这位国王非常善于交际,能说会到,和邻国 ...

  4. Android蓝牙开发—经典蓝牙和BLE(低功耗)蓝牙的区别

    找到一篇介紹BT与BLE使用差别的文章, 写的很清晰,看完基本明白了 ----------------------------------------------------------------- ...

  5. Android蓝牙开发 — 经典蓝牙BLE蓝牙

    一,前期基础知识储备 1)蓝牙是一种支持设备之间短距离通信的无线电技术(其他还包括红外,WIFI): 支持移动电话.笔记本电脑.无线耳机等设备之间进行信息的交换: Android支持的蓝牙协议栈:Bl ...

  6. Android 移动开发 近场通信 蓝牙通信

    Android 近场通信 蓝牙通信 一.近场通信介绍 1.三种主要近场通信技术的特点 2.比较 3.近场通信技术的未来发展 二.蓝牙通信 1.核心 2.布局 3.功能 其他配置 最终显示 一.近场通信 ...

  7. android 蓝牙 编程,Android编程之蓝牙测试实例

    本文实例讲述了Android编程之蓝牙测试.分享给大家供大家参考.具体分析如下: 一.软件平台: win7 + eclipse + sdk 二.设计思路: 配合倒计时定时器实现蓝牙打开,可见,扫描三个 ...

  8. Android蓝牙开发—经典蓝牙详细开发流程

    文章目录 开发流程 权限 核心API BlueToothAdapter getDefaultAdapter():获取BluetoothAdapter对象 判断设备是否支持蓝牙 判断蓝牙是否开启 get ...

  9. 蓝牙HID——Android手机注册HID时出现 Could not bind to Bluetooth (HID Device) Service with Intent * 的问题分析

    异常描述 在蓝牙HID的开发过程中,使用红米K30手机 MIUI12.5(Android 11) 系统,打算将手机打造成蓝牙外设(键盘.触摸板.游戏手柄等).首先调用下面的方式与系统蓝牙HID服务绑定 ...

  10. Android 手机蓝牙 (普通蓝牙篇)

    Android 手机蓝牙一 (普通蓝牙) hello,大家好有很久没有更新博客了.想写的东西很多,提起笔又觉得无从下笔.可能这就是菜鸟吧!好了废话不多说,进入正题. 本篇讲的是Android手机蓝牙分 ...

最新文章

  1. IOS 从系统图库中获取 图片 并设置为头像
  2. cbow 和skip-gram比较
  3. 本地环境的搭配及安装配置
  4. Linux安装Oracle 10g
  5. 房贷利率上浮30%利息太高吃不消,如何让利率变成下浮10%?
  6. 用gensim doc2vec计算文本相似度,Python可以跑通的代码
  7. Python程序设计语言基础03:基本数据类型
  8. Spark Hive 云原生改造在智领云的应用
  9. 特斯拉被曝储存大量未加密个人数据,你的隐私正在“裸奔”!
  10. JavaScript 误区
  11. everything 全盘文件查找工具及正则表达式的使用
  12. 阶段5 3.微服务项目【学成在线】_day04 页面静态化_14-页面静态化-数据模型-远程请求接口...
  13. java中rtsp转m3u8_直播源格式转换教程——rtmp/rtsp/http/m3u8!!
  14. wowza流媒体服务器最详细教程-wowza安装配置及优化
  15. log2 3用计算机怎么按,如何使用计算器计算对数log以2为底3的对数,由于计算器2ndf又叫shift,不同计算器不同,请根据图来,因为有一些别...
  16. 视频教程-Dreamweaver初级从入门到精通,掌握网页制作-Dreamweaver
  17. 什么是云计算架构和组件
  18. 攻防世界 Web高手进阶区 mfw
  19. 关爱码农成长:关于写代码二三事
  20. Java程序员必备技能

热门文章

  1. Python 之 os 模块常用操作
  2. 高速网络芯片 入驻移动基站
  3. 如何画出一张合格的技术架构图? 1
  4. ResNet详解——通俗易懂版
  5. 重要调谐质量阻尼器TMD模拟错误纠正
  6. 最新研究:宇航员血液样本中个个都带基因突变
  7. 操作系统(8) 磁盘的结构
  8. 飞行堡垒安装Linux黑屏,华硕飞行堡垒_安装kali后N卡问题
  9. 数据结构---克鲁斯卡尔(Kruskal)算法
  10. 机油压力传感器的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告