首页 > 编程知识 正文

蓝牙连接配对码,打印机蓝牙连接拒绝配对

时间:2023-05-03 13:22:05 阅读:196397 作者:363

前篇写的蓝牙数据传输,因为当时没要求就没写li连接配对,直接调用的已配对数据。

然后不出所料,测试版给出之后马上反馈回来,说要做连接配对功能,不然去设置里面弄太麻烦了....

行吧,谁叫我想偷懒叻。然后就做咯....这里重点表示一下:因为业务原因,我们使用的是4.4的android 版本,也就是说,蓝牙的配对不能直接用 createBond 方法实现,而需要使用反射的方法。具体下面代码都会记录下来。 

那么现在开始撸代码吧。。

首先,初始化蓝牙适配器:

//初始化蓝牙 BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

通过这个BluetoothAdapter ,我们可以获得与蓝牙这个实例。从中可以得到一些基本的东西,如: 启动设备发现(startDiscovery), 获取已配对设备(getBoundedDevices), 通过mac蓝牙地址获取蓝牙设备(getRemoteDevice), 从其它设备创建一个监听连接(listenUsingRfcommWithServiceRecord); 如果 mBluetoothAdapter == null ,则表示该手机不支持蓝牙。

我们需要去配置一个蓝牙权限,和注册广播:

<!--蓝牙权限--> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

广播的话,可以动态注册和静态注册。我这里用的是动态注册:

IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); //监听搜索完毕 filter.addAction(BluetoothDevice.ACTION_FOUND); //监听是否配对 filter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST); //监听配对的状态 registerReceiver(receiver, filter); private BroadcastReceiver receiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); switch (action) { case BluetoothDevice.ACTION_DISCOVERY_FINISHED: break; case BluetoothDevice.ACTION_FOUND: break; case BluetoothDevice.ACTION_PAIRING_REQUEST: break; } } };

额,这样蓝牙准备好了,广播注册可以了。那么就再做一个列表,然后将搜索到的设备添加进集合里,将是否已配对的状态展示出来,判断为配对的进行配对。

这里,首先是打开蓝牙搜索:

mBluetoothAdapter.enable();

同时将自身设备暴露出来:

//开启显示,让本机可以被搜索到 if (mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); startActivity(discoverableIntent); }

然后通过广播获取到搜索的设备:

//action.equals(BluetoothDevice.ACTION_FOUND) 的情况 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getBondState() == BluetoothDevice.BOND_NONE) { //未配对 }else if(device.getBondState() == BluetoothDevice.BOND_BONDED){ //已配对}

在搜索完毕之后就会发出广播:BluetoothAdapter.ACTION_DISCOVERY_FINISHED 提示搜索完毕。

然后就是最重要的选择未配对而需要用的设备进行配对了(注意在配对前,记得先停止正在搜索的广播):

// 判断当前是否还是正在搜索周边设备,如果是则暂停搜索 if (mBluetoothAdapter.isDiscovering()) { mBluetoothAdapter.cancelDiscovery(); }

前面已经说了,因为android 版本问题不能直接配对要通过反射:

public boolean createBond(Class btClass, BluetoothDevice btDevice) throws Exception { Method createBondMethod = btClass.getMethod("createBond"); Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice); return returnValue.booleanValue();}

方法内,class 传入:BluetoothDevice.class ,调用系统类的方法。device 就是你要去匹配的设备。然后会通过注册的广播BluetoothDevice.ACTION_PAIRING_REQUEST,发送配对状态:

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); switch (device.getBondState()) { case BluetoothDevice.BOND_BONDED: //配对成功 break; case BluetoothDevice.BOND_BONDING://配对中 break; case BluetoothDevice.BOND_NONE://配对失败 break; }

这样,蓝牙的连接到配对就完成了。接着就是刷新数据列表更新状态的了。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。