收藏
回答

ios小程序低功耗蓝牙与设备建立连接后弹出配对请求?

// 搜索附近设备

    onBluetoothDeviceFound() {

let that = this;

this.isFound = false;

console.log('开始搜索附近设备');

        uni.onBluetoothDeviceFound(res => {

            for (let i = 0; i < res.devices.length; i++) {

                let devicesDic = res.devices[i]

                let advertisData = CryptoTool.ab2hex(res.devices[i].advertisData).slice(4, 16);

                console.log(advertisData,that.imei,333333);

                if (advertisData && (advertisData == that.imei || devicesDic.deviceId == that.imei) && devicesDic.advertisServiceUUIDs) {

that.deviceId = devicesDic.deviceId

that.isFound = true;

clearTimeout(timeIdThree)

that.stopBluetoothDevicesDiscovery();

timeIdFour = setTimeout(function() {

that.createBLEConnection()

}, 1000);

                    

                    break

                }

            }

        })

    },

getConnectedBluetoothDevices(){

uni.getConnectedBluetoothDevices({

  services:[bleServerUUID, omAServerUUID, omCServerUUID], 

  success: (res) => {

     console.log('已连接状态的设备:',res)

  },

})

},

    // 创建蓝牙连接

    createBLEConnection() {

        let that = this

        console.log('创建蓝牙连接',that.deviceId);

        uni.createBLEConnection({

            deviceId: that.deviceId,

            success: res => {

that.isFound = true;

that.isReset = false;

that.createNum = 1

                    console.log('创建蓝牙连接成功');

                    // 蓝牙连接成功,上报设备信息

                    bleControl.connectStateListen({

                        ...BLUE_STATE.CONNECTSUCCESS,

                        deviceInfo: { ...that.deviceId }

                    })

that.getBLEDeviceServices()

that.onBLEConnectionStateChange()

                },

            fail: (err) => {

that.onBLEConnectionStateChange()

uni.hideLoading()

console.log('创建蓝牙连接失败',err);

if(err.errCode == 10000){

that.openBluetoothAdapter()

}

                    bleControl.connectStateListen(BLUE_STATE.CONNECTFAILED)

            },

        })

    },

回答关注问题邀请回答
收藏
登录 后发表内容