开发低功耗蓝牙4.0血压计连接与收发数据

本文记录了开发鱼跃YE680A蓝牙血压计的过程,包括如何连接设备,使用UUID收发数据。主要分为三个类:连接服务类、UUID配置类和工具类。连接过程涉及蓝牙功能检测、设备扫描和连接。数据通信关键在于发送命令和通过onCharacteristicChanged监听回传数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在开发鱼跃YE680A蓝牙血压计,获取测量到的血压和心率数据,之前也做有一个爱奥乐蓝牙血压计的,连接流程是一样的,在收发数据时有一些区别,下面做一些笔记:

一、整体的可定义三个类,一个Bluetooth服务的,用于连接,一个包含各类UUID的,用于收发数据,最后一个封装工具类,给调用者调用。
1. 先说连接吧,判断是不是支持蓝牙功能,是否打开蓝牙和请求打开蓝牙涉及到的,代码详细可能没贴出来了,网上一搜大把的。

    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothLeScanner mBluetoothLeScanner;
    private boolean isScanning; // 是否正在搜索

    private void initBluetooth() {
        BluetoothManager bluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();
        if(mBluetoothAdapter == null) {
            app.showToast(R.string.msg_bluetooth_no_support);
            return;
        } else {
            if(!mBluetoothAdapter.isEnabled()) {
                requestOpenBluetooth(); // 请求打开蓝牙
            }
            mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
        }
    }


    /**
     * 开始搜索设备
     * @param scan
     */
    public void scanLeDevice(boolean scan) {
        if(mBluetoothAdapter != null && !mBluetoothAdapter.isEnabled()) {
            requestOpenBluetooth(); // 请求打开蓝牙
            return;
        } else if(mBluetoothAdapter == null || mBluetoothLeScanner == null) {
            MyLg.e("scanLeDevice", "mBluetoothLeScanner == null");
            return;
        }
        if(scan) {
            if(!isScanning) {
                mBluetoothLeScanner.startScan(mScanCallback); // 开始搜索设备
            }
            isScanning = true;
        } else {
            mBluetoothLeScanner.stopScan(mScanCallback);
            isScanning = false;
        }
    }

下面是扫描设备时的回调:

private ScanCallback mScanCallback = new ScanCallback() {
        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            super.onBatchScanResults(results);
        }

        @Override
        public void onScanFailed(
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值