0


uni-app蓝牙操作

1、打开蓝牙适配器

            openBluetoothAdapter() {
                uni.openBluetoothAdapter({
                    success: res => {
                        console.log('openBluetoothAdapter success', res);
                        this.startBluetoothDevicesDiscovery();
                    },
                    fail: res => {
                        if (res.errCode === 10001) {
                            uni.showToast({
                                title: '请打开蓝牙',
                                icon: 'none'
                            });
                            uni.onBluetoothAdapterStateChange(res => {
                                console.log('onBluetoothAdapterStateChange', res);
                                if (res.available) {
                                    this.startBluetoothDevicesDiscovery();
                                }
                            });
                        }
                    }
                });
            },

2、开启蓝牙搜索

            startBluetoothDevicesDiscovery() {
                if (this.discoveryStarted) {
                    return;
                }
                this.discoveryStarted = true;
                uni.startBluetoothDevicesDiscovery({
                    allowDuplicatesKey: true,
                    success: res => {
                        console.log('startBluetoothDevicesDiscovery success', res);
                        this.onBluetoothDeviceFound();
                    }
                });
            },

3、监听蓝牙设备列表

            onBluetoothDeviceFound() {
                uni.onBluetoothDeviceFound(res => {
                    res.devices.forEach(device => {
                        if (device.name.indexOf('蓝牙名称') == -1) {
                            return;
                        }
                        this.createBLEConnection(device);
                    });
                });
            },

4、建立连接

            createBLEConnection(device) {
                const deviceId = device.deviceId;
                uni.createBLEConnection({
                    deviceId,
                    success: res => {
                        //隐藏加载框
                        this.connect = true;
                        this.restart();
                        this.deviceId = deviceId;
                        this.getBLEDeviceServices(deviceId);
                    }
                });
                this.stopBluetoothDevicesDiscovery();
            },

5、关闭蓝牙连接

            closeBLEConnection() {
                uni.closeBLEConnection({
                    deviceId: this.deviceId
                });
                this.connect = false;
            },

6、停止蓝牙搜搜

            stopBluetoothDevicesDiscovery() {
                uni.stopBluetoothDevicesDiscovery();
                this.discoveryStarted = false;
            },

7、获取蓝牙设备服务

            getBLEDeviceServices(deviceId) {
                uni.getBLEDeviceServices({
                    deviceId,
                    success: res => {
                        console.log(res);
                        for (let i = 0; i < res.services.length; i++) {
                            if (res.services[i].isPrimary) {
                                this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid);
                                return;
                            }
                        }
                    }
                });

            },

8、获取蓝牙特征值

            getBLEDeviceCharacteristics(deviceId, serviceId) {

                uni.getBLEDeviceCharacteristics({
                    deviceId,
                    serviceId,
                    success: res => {
                        console.log('getBLEDeviceCharacteristics success', res.characteristics);
                        for (let i = 0; i < res.characteristics.length; i++) {
                            let item = res.characteristics[i];
                            console.log(item.properties)
                            if (item.properties.read) {
                                uni.readBLECharacteristicValue({
                                    deviceId,
                                    serviceId,
                                    characteristicId: item.uuid
                                });
                            }
                            if (item.properties.write) {
                                this.deviceId = deviceId;
                                this.serviceId = serviceId;
                                this.characteristicId = item.uuid;
                            }
                            if (item.properties.notify || item.properties.indicate) {
                                uni.notifyBLECharacteristicValueChange({
                                    deviceId,
                                    serviceId,
                                    characteristicId: item.uuid,
                                    state: true
                                });
                            }
                        }
                    },
                    fail(res) {
                        console.error('getBLEDeviceCharacteristics', res);
                    }
                });
                uni.onBLECharacteristicValueChange(characteristic => {

                    if(!this.connect){
                        return
                    }
                    //自己的操作
                    let value = util.ab2hex(characteristic.value);

                });
                uni.onBLEConnectionStateChange(res => {
                    // 该方法回调中可以用于处理连接意外断开等异常情况
                    console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`);
                    this.connect = res.connected;
                    if (!this.connect) {
                        this.discoveryStarted = false;
                        //this.finishStatus = true;
                        clearInterval(this.interval);
                        this.openBluetoothAdapter();
                    }

                });
            },
标签: uni-app 前端 vue.js

本文转载自: https://blog.csdn.net/qq_35323539/article/details/126667663
版权归原作者 金刚镜 所有, 如有侵权,请联系我们删除。

“uni-app蓝牙操作”的评论:

还没有评论