{
data: {
status: "" ,
sousuo: "" ,
devices:[]
},
onLoad: function () {
var that= this ;
if (wx.openBluetoothAdapter) {
wx.openBluetoothAdapter({
success: function (res) {
console.log( '1.初始化蓝牙适配器:' ,res);
that.setData({ status: "1.初始化成功!" });
wx.onBluetoothDeviceFound( function (devices) {
var temp = that.data.devices;
temp.push(devices)
that.setData({
devices: temp
})
console.log( '0.1发现新蓝牙设备' , devices)
console.log( '0.2设备id' + devices.deviceId)
console.log( '0.3设备name' + devices.name)
console.log( '0.4.详情:' ,devices)
});
wx.onBluetoothAdapterStateChange( function (res) {
console.log( '2.1.状态发生变化:' , res);
that.setData({
sousuo: res.discovering ? "搜索中.." : "搜索完成" ,
status: res.available ? "可用" : "不可用" ,
});
});
wx.getBluetoothAdapterState({
success: function (res){
console.log( '2.获取蓝牙状态:' , res);
that.setData({
sousuo: res.discovering ? "搜索中.." : "未搜索" ,
status: res.available ? "可用" : "不可用" ,
});
if (!res.discovering){
wx.stopBluetoothDevicesDiscovery({success: function (){}});//--页面返回要先干掉上一个搜索
wx.startBluetoothDevicesDiscovery({
success: function (res) {
console.log( '3.搜索成功返回适配器状态:' , res);
},
fail: function (res) {
console.log( '-3.搜索失败:' , res);
that.setData({ status: "-3.搜索失败!" });
},
});
}
}
});
wx.getBluetoothDevices({
success: function (res) {
console.log( '3.1.成功获取设备:' , res);
that.setData({
status: "3.1.成功获取设备列表" ,
devices: res.devices
});
},
fail: function (res) {
console.log( '-3.1.设备获取失败:' , res);
that.setData({ status: "-3.1.设备获取失败!" });
}
});
},
fail: function (res){
console.log( '-1.蓝牙初始化失败:' ,res)
that.setData({ status: "-1.初始化失败!" })
}
});
} else {
wx.showModal({
title: '提示' ,
content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
});
}
},
|