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

本文介绍了如何在Android中实现蓝牙设备的自动配对和连接,避免出现配对提示框。通过反射调用BluetoothDevice类的隐藏方法,如createBond()、setPin()、cancelBondProcess()和cancelPairingUserInput(),实现在后台完成蓝牙设备的配对和连接过程。
摘要由CSDN通过智能技术生成

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

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

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

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

cancelBondProcess()和cancelPairingUserInput()

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

下面是自动配对的代码

Mainfest,xml注册

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

public class BluetoothConnectActivityReceiver extends BroadcastReceiver

{

String strPsw = "0";

@Override

public void onReceive(Context context, Intent intent)

{

// TODO Auto-generated method stub

if (intent.getAction().equals(

"android.bluetooth.device.action.PAIRING_REQUEST"))

{

BluetoothDevice btDevice = intent

.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

// byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234");

// device.setPin(pinBytes);

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 block

e.printStackTrace();

}

}

}

}

/*******************

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值