problem description
the environmental background of the problems and what methods you have tried
related codes
/ / Please paste the code text below (do not replace the code with pictures)
/ / index.js
/ / get an application example
const app = getApp ()
Page ({
data: {
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse("button.open-type.getUserInfo"),
devices: [],
deviceId: "",
services: [],
serviceId: "",
characteristicId: "",
characteristicIdNotify: "",
characteristicIdRead: "",
},
onLoad () {
// wx.showModal({
// title: "",
// content: "",
// success: function (res) {
// that.setData({
// isbluetoothready: false,
// searchingstatus: false
// })
// }
// })
},
/ *
- @ description turn on Bluetooth Adapter
* /
openBlue () {
let _this = this
wx.openBluetoothAdapter({
success(res) {
console.log(":", res)
//
_this.discoverDevices()
}
})
},
closeBlue () {
wx.closeBluetoothAdapter({
success(res) {
console.log(": ", res)
}
})
},
/ *
- @ description search for Bluetooth devices
* /
discoverDevices () {
let _this = this
wx.startBluetoothDevicesDiscovery({
success(res) {
console.log("", res)
setTimeout(() => {
_this.getDevices()
setTimeout(() => {
_this.foundDevices()
}, 500)
}, 500)
}
})
},
/ *
- @ description search for new Bluetooth devices
* /
foundDevices () {
let _this = this
wx.onBluetoothDeviceFound(function (devices) {
console.log(":", devices)
//
_this.data.devices.push(devices.devices[0])
_this.setData({
devices: _this.data.devices
})
})
},
/ *
- @ description get connectable Bluetooth devices
* /
getDevices () {
let _this = this
wx.getBluetoothDevices({
success: function (res) {
if (res.devices[0]) {
console.log(":", res)
_this.setData({
devices: res.devices
})
}
}
})
},
chooseDevice (e) {
let _this = this
// deviceId
_this.data.deviceId = e.currentTarget.dataset.deviceid
//
_this.createConnect(_this.data.deviceId)
// console.log("_this.data.deviceId: ",_this.data.deviceId)
},
createConnect (deviceId) {
let _this = this
wx.createBLEConnection({
// deviceId createBLEConnection
deviceId,
success(res) {
//
console.log(":", res)
// serviceId
_this.getServices(deviceId)
//
_this.onConnectionStatus()
//
_this.stopDiscoverDevices()
},
fail(err) {
console.warn("err :", err);
}
})
},
onConnectionStatus () {
wx.onBLEConnectionStateChange(function (res) {
//
console.log(` ${res.deviceId} , : ${res.connected}`)
})
},
/ *
- @ description get connected Bluetooth devices
* /
getConnectDevices (services) {
wx.getConnectedBluetoothDevices({
services,
success(res) {
console.log(":" + JSON.stringify(res))
},
fail(err) {
console.log("err: ", err.errMsg)
}
})
},
/ *
- @ description stop searching for Bluetooth devices
* /
stopDiscoverDevices () {
wx.stopBluetoothDevicesDiscovery({
success(res) {
console.log("")
}
})
},
/ *
- @ description get services & serviceId
* /
getServices (deviceId) {
let _this = this
wx.getBLEDeviceServices({
deviceId,
success(res) {
console.log("device services:", res.services)
_this.getConnectDevices([res.services[2].uuid])
// serviceId
_this.setData({
serviceId: res.services[2].uuid
})
console.log("_this.serviceId :", _this.data.serviceId);
_this.getCharacteristicsId(_this.data.deviceId, _this.data.serviceId)
}
})
},
/ *
- @ description get CharacteristicsId
* /
getCharacteristicsId (deviceId, serviceId) {
let _this = this
wx.getBLEDeviceCharacteristics({
deviceId,
serviceId,
success(res) {
console.log("Characteristics:", res.characteristics)
// characteristicIdcharacteristicIdNotify
_this.setData({
characteristicIdNotify: res.characteristics[0].uuid,
characteristicId: res.characteristics[0].uuid,
})
_this.openNotify(deviceId, serviceId, _this.data.characteristicIdNotify)
console.log("CharacteristicsId:", res.characteristics[0].uuid)
}
})
},
/ *
- @ description Open notify
* /
openNotify (deviceId, serviceId, characteristicId) {
let _this = this
wx.notifyBLECharacteristicValueChange({
state: true,
deviceId,
serviceId,
characteristicId,
success(res) {
console.log("notify ", res.errMsg)
setTimeout(() => {
_this.writeDevice(deviceId, serviceId, _this.data.characteristicId)
}, 2000)
},
fail(err) {
console.log("openNotify err: ", err)
}
})
},
/ *
- @ description writes data to the device
* /
writeDevice (deviceId, serviceId, characteristicId) {
let _this = this
// 0x0016
let senddata = "test"
let buffer = new ArrayBuffer(senddata.length)
let dataView = new DataView(buffer)
for (var i = 0; i < senddata.length; iPP) {
dataView.setUint8(i, senddata.charAt(i).charCodeAt())
}
// let buffer = new ArrayBuffer(2)
// let dataView = new DataView(buffer)
// dataView.setUint16(0, 3)
wx.writeBLECharacteristicValue({
deviceId,
serviceId,
characteristicId,
value: buffer,
success(res) {
console.log("", res.errMsg)
_this.readDevice(deviceId, serviceId, _this.data.characteristicId)
},
fail(err) {
console.log("err", err)
},
complete() {
_this.closeConnection(deviceId)
_this.closeBlue()
}
})
},
readDevice (deviceId, serviceId, characteristicId) {
let _this = this
//
wx.onBLECharacteristicValueChange(function (characteristic) {
console.log("characteristic value comed:", characteristic)
})
wx.readBLECharacteristicValue({
// deviceId createBLEConnection
deviceId,
// serviceId getBLEDeviceServices
serviceId,
// characteristicId getBLEDeviceCharacteristics
characteristicId,
success(res) {
console.log("readBLECharacteristicValue:", res.errCode)
}
})
},
closeConnection (deviceId) {
wx.closeBLEConnection({
deviceId,
success(res) {
console.log(":", res)
}
})
}
})