简单梳理android蓝牙配对通讯、app层实现蓝牙静默配对、ClsUtils类

最近做蓝牙通讯,需要app层静默连接通讯,记录近几天的心酸摸索。

1.搜索蓝牙设备

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
  //不支持蓝牙或者蓝牙不可用
            Toast.makeText(this, "设备不支持蓝牙", Toast.LENGTH_SHORT).show();
        } else {
        if (!bluetoothAdapter.isDiscovering()) bluetoothAdapter.startDiscovery();
                }

蓝牙发现新设备会发出广播:BluetoothDevice.ACTION_FOUND
蓝牙搜索结束会发出广播:BluetoothAdapter.ACTION_DISCOVERY_FINISHED
//自定义蓝牙搜索状态广播接收器,找到设备后更新自己的列表

   private class BlueDeviceReceiver extends BroadcastReceiver {
   
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(BluetoothDevice.ACTION_FOUND)) {
  //发现新设备
                BluetoothDevice btd = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (!deviceList.contains(btd.getName() + "\n" + btd.getAddress())) {
                    deviceList.add(btd.getName() + "\n" + btd.getAddress());
                    deviceAdapter.notifyDataSetChanged();
                }
  } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
                  //搜索结束
                }
            }
        }
    }

注意添加权限:尖括号输入不了,囧。

uses-permission android:name=”android.permission.BLUETOOTH”
uses-permission android:name=”android.permission.BLUETOOTH_ADMIN”

如果是android6.0系统需要额外添加权限,否则无法搜索设备:

uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION”
uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION”

2.发起配对请求

    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
     if (!bluetoothAdapter.checkBluetoothAddress(deviceAddress)) {
  //检查是否是有效的蓝牙地址
     Toast.makeText(LauncherDemoActivity.this, "蓝牙设备地址无效", Toast.LENGTH_SHORT).show();
            return false;
        }
    BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress);
    if (device != null) {
            Boolean returnValue = null;
            try {
                returnValue = ClsUtils.createBond(BluetoothDevice.class, device);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (returnValue) {
  //发起配对成功,并不代表配对成功,因为可能被拒绝
                return true;
            }
        }

3.自定义蓝牙配对状态广播接收器,当有自己想要的设备配对时,就可以进行下一步socket通讯了

class MyActionReceiver extends BroadcastReceiver {
   
        @Override
        public void onReceive(Context context, Intent intent) {
            if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction())) {
                int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
                        BluetoothDevice.ERROR);
                if (bondState == BluetoothDevice.BOND_BONDED) {
  //有新的配对设备
                        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
                        Set<BluetoothDevice> pairedDevices = adapter.getBondedDevices();
                        BluetoothDevice device1 = null;
// If there are paired devices
                        if (pairedDevices.size() > 0) {
                            // Loop through paired devices
                            for (BluetoothDevice device : pairedDevices) {
                      if(device.getAddress().equals("55:44:33:22:11:00")) {
  //如果连接的设备中有特定的设备则发起socket连接请求
                                    device1 = device;
                                break;
                                }
                            }
                        }
                        if(device1!=null) {
  //发起socke
  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值