请输入SSID
" placeholder-class="placeholder" :adjust-position="false"
style="height: 100%;" />
</view>
<view class="fzb-wifi-input">
<input v-model="wifiPassword" :placeholder="请输入WIFI密码
" placeholder-class="placeholder"
adjust-position="false" style="height: 100%;" />
</view><view class="tn-flex tn-flex-col-center tn-flex-row-between">
<uv-button customStyle="{width: '230rpx',height:'80rpx',background:'#fff',border: '1rpx solid #353648',borderRadius: '8rpx'}" @click="wifiCancel">取消</uv-button>
<uv-button customStyle="{width: '230rpx',height:'80rpx',background:'#FDB531',borderRadius: '8rpx',color:'#fff'}" disabled="wifiPassword.length<6" @click="wifiConfirm">确定</uv-button> </view>
</view>
</d-popup>
<d-popup :show="Application" bgType="default" pos="bottom-center" width="590rpx" height="260rpx">
<view
class="tn-bg-white tn-width-full tn-height-full tn-radius tn-padding tn-flex tn-flex-direction-column tn-flex-row-around">
<view class="fzb-wifi-header tn-text-center text-hei text-bold5 tn-text-xl ">
提示
</view>
<view class="fzb-wifi-header tn-text-center text-hei text-bold5 tn-text-md tn-margin-top tn-margin-bottom">
未打开 Wi-Fi 开关
</view>
<view class="tn-flex tn-flex-col-center tn-flex-row-between">
<uv-button customStyle="{width: '230rpx',height:'80rpx',background:'#fff',border: '1rpx solid #353648',borderRadius: '8rpx'}" @click="launchApplication()">设置</uv-button>
<uv-button customStyle="{width: '230rpx',height:'80rpx',background:'#FDB531',borderRadius: '8rpx',color:'#fff'}" @click="initWifi()">重试</uv-button>
</view>
</view>
</d-popup>
<d-message ref="toast"></d-message>
</view>
</template>
<script>
import {
judgePermission
} from "@/utlis/permission.js"
import {
asciiToArrayBuffer,
ab2hex,
hexCharCodeToStr
} from "@/utlis/bluetooth.js"
import {
saveUserDeviceMode
} from "@/api/userDveice.js"
import {
mapState
} from "vuex"
export default {
data() {
return {
moduleSSID:"",
resHex: "",
wifiSSID: "",
Application: false,
wifiPassword: "",
wifiShow: false,
wifiIndex: 0,
wifiOptions: {
},
wifiList: [],
connectWifi: "",
obj: {},
msg: {},
wifiLIST: {},
notifyValueChange: "",
wifiObj: {}
}
},
onLoad(e) {
const {
deviceId,
uuid,
characteristicId,
characteristicId0
} = JSON.parse(e.setInit)
console.log("setInit");
console.log(JSON.parse(e.setInit));
this.obj = {
deviceId,
uuid,
characteristicId,
characteristicId0
}
this.initWifi()
},
computed: {
...mapState(['user'])
},
onBackPress(e) {
if (e.from == "backbutton") return true; //APP安卓物理返回键逻辑
},
watch:{
resHex(value){
if(value!=3||value!="3"){
saveUserDeviceMode({
userId: this.user.userId,
deviceCode: 255
}).then(res => {
const {
code
} = res
if (code == 1) {
this.$refs.toast.show({
model: 'success',
black: false,
mask: true,
label: 'WIFI-连接成功'
})
}
this.moduleSSID = this.wifiOptions.SSID
})
}else{
this.$refs.toast.show({
model: 'success',
black: false,
mask: true,
label: '连接失败,请重新连接!'
})
}
}
},
methods: {
initWifi() {
uni.startWifi({
success: res => {
// console.log(res,'res');
this.Application = false
uni.getWifiList({
success: (res) => {
console.log(res, "res");
},
fail: (err) => {
},
complete: (err) => {
uni.onGetWifiList(this.onGetWifiListFun)
this.getWifiListFun()
}
})
},
fail: (err) => {
if (err.errCode == 12005) {
this.Application = true
}
console.log(err);
}
})
},
onGetWifiListFun(info) {
this.wifiList = []
let wifiList = []
const unique = (arr) => {
const res = new Map(); //定义常量 res,值为一个Map对象实例
return arr.filter((arr) => !res.has(arr.SSID) && res.set(arr.SSID, 1))
}
wifiList = unique(info.wifiList).filter(item => item.SSID != '')
// 按信号强度排序
wifiList = wifiList.sort((a, b) => {
return b.signalStrength - a.signalStrength
})
this.wifiList = wifiList
// 获取成功,关闭loading
// console.log('wifiList', wifiList)
console.log('wifiList', this.wifiList)
// console.log(info)
},
getWifiListFun() {
// #ifdef APP-PLUS
uni.getWifiList({
complete: () => {
// 移除监听函数
uni.offGetWifiList(this.onGetWifiListFun)
// 获取当前连接的WiFi信息
}
})
// #endif
},
wifiConfirm() {
// let msg = `${this.wifiOptions.SSID+'|'+this.wifiPassword}`
// let msg = `10001`
let msg = `${this.wifiSSID+'|'+this.wifiPassword}`
const buffer = asciiToArrayBuffer(msg);
uni.writeBLECharacteristicValue({
// 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
deviceId: this.obj.deviceId,
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
serviceId: this.obj.uuid,
// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
characteristicId: this.obj.characteristicId,
// 这里的value是ArrayBuffer类型
// value: buffer,
value: buffer,
writeType: 'writeNoResponse',
success: (res) => {
console.log("发送成功-----", res);
},
fail: (res) => {},
complete: (res) => {
this.wifiObj = res
this.updateNotify(this.obj.deviceId, this.obj.uuid, this.obj.characteristicId0)
this.wifiCancel()
}
});
// let newList = this.wifiList
// newList.forEach((item, itemIndex) => {
// if (this.wifiIndex == itemIndex) {
// newList[itemIndex].is = !newList[itemIndex].is;
// return;
// }
// newList[itemIndex].is = false;
// });
},
wifiCancel() {
this.wifiShow = false
},
/**
* @tips:启用低功耗蓝牙设备特征值变化时的notify功能,订阅特征值。注意:必须设备的特征值支持 notify 或者 indicate 才可以成功调用
*/
async updateNotify(deviceId, serviceId, characteristicId) {
uni.notifyBLECharacteristicValueChange({
deviceId,
serviceId,
characteristicId,
state: true,
success: (res) => {
console.log("notify功能,订阅特征值");
console.log(res);
this.message()
},
fail: (err) => {
},
complete: (res) => {
this.notifyValueChange = res
}
})
},
//监听发送过来的消息
async message() {
// setTimeout(() => {
let show = true
uni.onBLECharacteristicValueChange((res) => {
// console.log("蓝牙返回数据为:"+receiveValue)
let resHex = ab2hex(res.value) // ArrayBuffer转16进制字符串示例
// this.resHex = resHex
// let result = hexCharCodeToStr(resHex) // 将16进制的内容转ascii码成我们看得懂的字符串内容
this.resHex = resHex
})
this.$refs.toast.hide()
// }, 2000)
},
// 跳转到手机系统设置页面
launchApplication() {
if (plus.os.name == "Android") {
plus.runtime.launchApplication({
pname: "com.android.settings",
extra: {
action: "android.settings.SETTINGS"
}
}, function(e) {
console.log("打开系统设置成功");
}, function(e) {
console.log("打开系统设置失败:", e);
});
} else {
plus.runtime.openURL("app-settings:", function(e) {
console.log("打开系统设置成功");
}, function(e) {
console.log("打开系统设置失败:", e);
});
}
},
// 链接wifi
linkedWifi(item, index) {
this.wifiOptions = item
console.log(item,"item");
this.wifiIndex = index
// if (this.wifiList[index].is) return
this.wifiShow = true
this.wifiPassword = ''
}
}
}
</script>
<style lang="scss">
page {
background: #FFF;
}
.fzb_discover {
font-size: 30rpx;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #B5B5B5;
}
.fzb_buetooth_right {
width: 40rpx;
height: 40rpx;
background: #E3E3E3;
}
.fzb_connected {
font-size: 30rpx;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #353648;
background: #EEEEEE;
border-radius: 30rpx;
width: 130rpx;
height: 60rpx;
}
.fzb-wifi-input {
height: 96rpx;
background: #F7F7F7;
border-radius: 8rpx;
padding: 30rpx;
}
.placeholder {
font-size: 24rpx;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #BDBDBD;
}
</style>
1 个回复
DCloud_Android_DQQ
提供一下报错日志