有大佬知道吗?
- 发布:2022-07-07 10:59
- 更新:2023-05-24 16:01
- 阅读:2039
这边测试未复现该问题,测试代码:
<template>
<view>
<button type="primary" @click="getStorage">getStorage</button>
</view>
</template>
<script>
export default {
onLoad() {
uni.getStorage({
key:'test-storage-key',
success(res) {
console.log('onLoad res',res.data)
uni.showToast({
title:`onLoad res:${res.data}`
})
},
fail(){
console.log('onLoad getStorage failed');
uni.showToast({
title:`onLoad fail`
})
uni.setStorage({
key: 'test-storage-key',
data: 'test-storage-value',
});
}
})
},
methods: {
getStorage(){
uni.getStorage({
key:'test-storage-key',
success(res) {
console.log('onShow res',res.data)
uni.showToast({
title:`onShow res:${res.data}`
})
}
})
}
}
}
</script>
如依然有问题,请提供可复现demo
z***@163.com (作者)
<template>
<view>
<uni-fab :popMenu="true" horizontal="right" :content="content" @trigger="trigger"></uni-fab>
<view>
<!-- 主界面设备列表 -->
<uni-list>
<uni-list-item v-for="(device,index) in lockList" :key="index" :title="device.dname" showArrow>
</uni-list-item>
</uni-list>
<!-- 普通弹窗 -->
<uni-popup ref="popup" background-color="#fff">
<uni-section title="设备列表" type="line" padding>
<uni-list>
<uni-list-item v-for="(device,index) in devicesList" :key="index" :title="device.localName"
showArrow :rightText="device.RSSI" clickable @click="onClick(device,index)">
</uni-list-item>
</uni-list>
</uni-section>
</uni-popup>
</view>
</view>
</template>
<script>
var _self;
export default {
created() {
_self = this
//读取设备
},
onLaunch() {
this.getStorage();
},
data() {
return {
content: [{
iconPath: '/static/blueadd.png',
text: '添加设备',
active: false
},
{
iconPath: '/static/blueadd.png',
text: '授权开锁',
active: false
}
],
devicesList: [], //搜索蓝牙设备
lockList: [] //界面设备列表
};
},
methods: {
trigger(e) {
console.log(e)
this.$refs.popup.open();
this.openBluetoothAdapter();
},
onClick(device, index) {
console.log('执行click事件', device)
this.connetBlue(0, index);
},
//缓存数据
setStorage() {
//这是一个同步接口。建议使用这个方法
//第一个参数本地缓存中的指定的 key
//第二个参数需要存储的内容
uni.setStorage({
key: 'locks',
data: this.lockList,
});
},
getStorage() {
//这是一个同步接口。建议使用这个方法
//参数本地缓存中的指定的 key通过赋值给一个变量获取
uni.getStorage({
key: 'locks',
success(res) {
console.log('onShow res', res.data)
this.lockList = res.data
}
})
},
removeStorage() {
//这是一个同步接口。建议使用这个方法
//参数本地缓存中的指定的 key通过赋值给一个变量获取
const value = uni.removeStorageSync("locks");
console.log("移除成功");
},
///蓝牙相关方法
openBluetoothAdapter() {
uni.openBluetoothAdapter({
success: e => {
console.log('初始化蓝牙成功:' + e.errMsg);
console.log(JSON.stringify(e));
this.startBluetoothDevicesDiscovery(true);
}
});
},
//搜索蓝牙设备
startBluetoothDevicesDiscovery(needlocation) {
discovering = true; // wxf 190907 增加
if (this.platform == "android") {
} else { // for ios reopen blue adapter
uni.openBluetoothAdapter();
}
this.loading = true;
uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: false,
success: e => {
console.log('开始搜索蓝牙设备:' + e.errMsg);
this.getBlueList();
}
});
},
getBlueList() {
uni.getBluetoothDevices({
success(res) {
let data = res.devices
let tempList = [];
data.map(device => {
if (!!device.localName) {
tempList.push(device)
}
});
this.devicesList = tempList;
_self.listenBluetooth();
}
})
},
//寻找到新设备
listenBluetooth() {
let tempList = this.devicesList;
uni.onBluetoothDeviceFound(res => {
//剔除重复设备,兼容不同设备API的不同返回值
let flag = false;
res.devices.forEach(device => {
if (!!device.localName) {
tempList.push(device)
flag = true;
console.log(device.localName);
//console.log(device.r);
}
})
if (flag) {
this.devicesList = tempList;
console.log('没有新设备时候的数组:' + this.devicesList.length);
}
});
},
connetBlue(type, index) {
let deviceIndex = index;
let deviceInfo = this.devicesList[deviceIndex];
if (this.prevConnected && type == 1) {
deviceInfo = this.pDeviceInfo;
}
let dId = deviceInfo.deviceId;
uni.showLoading({
title: '正在连接...', //提示的内容,
mask: true
});
//连接蓝牙
uni.createBLEConnection({
deviceId: dId, //设备id
success: res => {
uni.hideLoading();
if (res.errCode == 0) {
this.connected = true;
this.connectedName = deviceInfo.name;
uni.setStorageSync('deviceInfo', deviceInfo);
this.deviceId = dId;
uni.showToast({
title: '连接成功',
icon: 'success',
duration: 1000
});
}
uni.stopBluetoothDevicesDiscovery({
success: res => {
console.log('连接蓝牙成功之后关闭蓝牙搜索');
_self.$refs.popup.close();
_self.devicesList = [];
}
})
//连接蓝牙成功后的操作-----
//1、添加设备
let data = {
dname: deviceInfo.localName,
name: ""
}
this.lockList.push(data);
this.setStorage();
},
fail: err => {
uni.showToast({
title: '连接失败!',
icon: 'none',
duration: 2000
});
}
})
},
getBLEDeviceServices() {
//获取服务
uni.showLoading({
title: '正在打印...', //提示的内容,
mask: true
});
let deviceId = this.deviceId;
uni.getBLEDeviceServices({
deviceId,
success: (res) => {
for (let i = 0; i < res.services.length; i++) {
if (res.services[i].isPrimary) {
this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid);
}
}
},
fail: (res) => {
uni.hideLoading();
console.log("获取蓝牙服务失败:" + JSON.stringify(res))
}
})
},
//获取单个服务的特征值(characteristic)
getBLEDeviceCharacteristics(deviceId, serviceId) {
if (!!this.characteristics && !!this.serviceId) {
this.PrintStr();
return;
}
uni.getBLEDeviceCharacteristics({
deviceId,
serviceId,
success: (res) => {
uni.hideLoading();
for (let i = 0; i < res.characteristics.length; i++) {
let item = res.characteristics[i];
if (item.properties.write && !this.serviceId) {
this.serviceId = serviceId;
this.characteristics = item.uuid;
this.PrintStr();
}
}
},
fail(res) {
uni.hideLoading();
console.error('获取特征值失败:', res)
}
})
},
PrintStr() {
const buf = Buffer.from("this is test content!");
var bufferstr = buf.buffer;
uni.writeBLECharacteristicValue({
deviceId: this.deviceId,
serviceId: this.serviceId,
characteristicId: this.characteristics,
value: bufferstr,
success: function(res) {
uni.hideLoading();
},
fail: function(res) {
console.log("数据发送失败:" + JSON.stringify(res))
},
complete: function(res) {
},
})
},
closeBluetoothAdapter() {
uni.closeBluetoothAdapter({
success: res => {
console.log('关闭蓝牙适配器');
}
});
},
}
}
</script>
<style lang="scss">
</style>
z***@163.com (作者)
你真机测试一下看看 我这边是 小程序没杀死 数据是有的 杀死了的话数据就没了
z***@163.com (作者)
大佬 你这是每次读取失败都会主动再存一次啊 你改成存一次 下次直接启动小程序 直接读取试试
2022-07-08 08:50
DCloud_UNI_WZF
回复 z***@163.com: 除了第一次,以后不会触发 fail
2022-07-08 10:06