android 蓝牙隐藏对话框 后台配对
2013-04-17 13:58
239 查看
最近在做一个项目用到蓝牙,但是所有蓝牙的操作必须要后台进行,于是网上搜了下找到了方法。。首先需要下面3个方法,通过反射去操作
static public boolean createBond(Class btClass, BluetoothDevice btDevice) throws Exception { Method createBondMethod = btClass.getMethod("createBond"); Log.i("life", "createBondMethod = " + createBondMethod.getName()); Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice); return returnValue.booleanValue(); } static public boolean setPin(Class btClass, BluetoothDevice btDevice, String str) throws Exception { Boolean returnValue = null; try { Method removeBondMethod = btClass.getDeclaredMethod("setPin", new Class[] { byte[].class }); returnValue = (Boolean) removeBondMethod.invoke(btDevice, new Object[] { str.getBytes() }); Log.i("life", "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 returnValue; } // 取消用户输入 static public boolean cancelPairingUserInput(Class btClass, BluetoothDevice device) throws Exception { Method createBondMethod = btClass.getMethod("cancelPairingUserInput"); // cancelBondProcess() Boolean returnValue = (Boolean) createBondMethod.invoke(device); Log.i("life", "cancelPairingUserInputreturnValue = " + returnValue); return returnValue.booleanValue(); }
然后监听蓝牙配对的广播 匹配“android.bluetooth.device.action.PAIRING_REQUEST”这个action
然后分辨调用上面的setPin(mDevice.getClass(), mDevice, "1234"); // 手机和蓝牙采集器配对 createBond(mDevice.getClass(), mDevice); cancelPairingUserInput(mDevice.getClass(), mDevice);
其中上面的mDevice是你要去连接的那个蓝牙的对象 , 1234为配对的pin码
相关文章推荐
- 在Android 8.0(API第26级)和更高版本的设备上,您可以在试图通过蓝牙、BLE和wi - fi对其他设备进行配对时定制配对请求对话框
- android蓝牙主动发起配对实例
- Android提高第十三篇之探秘蓝牙隐藏API
- Android蓝牙自动配对和Pin码设置
- Android 蓝牙开发——获取已配对蓝牙并显示
- android开发之蓝牙初步 扫描已配对蓝牙、更改蓝牙可见性、搜索外部蓝牙设备
- Android蓝牙自动配对
- Android中如何实现蓝牙的配对与连接
- Android手机蓝牙的开发——(一)蓝牙的开启关闭配对等基本操作
- Android蓝牙开发——查询手机上已配对的蓝牙设备
- Android提高之蓝牙隐藏API探秘
- Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备
- Android手机靠近蓝牙模块自动连接蓝牙模块,断后自动重连。也可以手机靠近手机自动连接。连接成功率百分之百,可后台运行
- Android 蓝牙开发之搜索、配对、连接、通信大全
- Android 蓝牙开发之搜索、配对、连接、通信大全
- Android按下back键非退出隐藏到后台
- Android代码清除蓝牙已配对信息(转载未验证)
- Android蓝牙配对
- android 接收到蓝牙配对请求时如何点亮屏幕
- Android:在AlertDialog点击按钮隐藏对话框