wx.createBLEConnection 蓝牙连接失败
发布于 6 年前 作者 icao 17853 次浏览 来自 问答
  1. 手机(ios 安卓)蓝牙搜索始终搜索不到设备, 只能搜索到电脑蓝牙。

  2. ios连接电脑蓝牙失败,安卓连接电脑蓝牙失败。

//index.js  

//获取应用实例  

var app = getApp();

Page({

data: {

status: “”,

sousuo: “”,

connectedDeviceId: “”, //已连接设备uuid  

services: “”, // 连接设备的服务  

characteristics: “”,   // 连接设备的状态值  

writeServicweId: “”, // 可写服务uuid  

writeCharacteristicsId: “”,//可写特征值uuid  

readServicweId: “”, // 可读服务uuid  

readCharacteristicsId: “”,//可读特征值uuid  

notifyServicweId: “”, //通知服务UUid  

notifyCharacteristicsId: “”, //通知特征值UUID  

inputValue: “”,

characteristics1: “”, // 连接设备的状态值  

},

onLoad: function () {

if (wx.openBluetoothAdapter) {

wx.openBluetoothAdapter()

} else {

// 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示  

wx.showModal({

title: ‘提示’,

content: ‘当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。’

})

}

},

// 初始化蓝牙适配器  

lanya1: function () {

var that = this;

wx.openBluetoothAdapter({

success: function (res) {

that.setData({

msg: “初始化蓝牙适配器成功!” + JSON.stringify(res),

})

//监听蓝牙适配器状态  

wx.onBluetoothAdapterStateChange(function (res) {

that.setData({

sousuo: res.discovering ? “在搜索。” : “未搜索。”,

status: res.available ? “可用。” : “不可用。”,

})

})

}

})

},

// 本机蓝牙适配器状态  

lanya2: function () {

var that = this;

wx.getBluetoothAdapterState({

success: function (res) {

that.setData({

msg: “本机蓝牙适配器状态” + “/” + JSON.stringify(res.errMsg),

sousuo: res.discovering ? “在搜索。” : “未搜索。”,

status: res.available ? “可用。” : “不可用。”,

})

//监听蓝牙适配器状态  

wx.onBluetoothAdapterStateChange(function (res) {

that.setData({

sousuo: res.discovering ? “在搜索。” : “未搜索。”,

status: res.available ? “可用。” : “不可用。”,

})

})

}

})

},

//搜索设备  

lanya3: function () {

var that = this;

wx.startBluetoothDevicesDiscovery({

success: function (res) {

that.setData({

msg: “搜索设备” + JSON.stringify(res),

})

//监听蓝牙适配器状态  

wx.onBluetoothAdapterStateChange(function (res) {

that.setData({

sousuo: res.discovering ? “在搜索。” : “未搜索。”,

status: res.available ? “可用。” : “不可用。”,

})

})

}

})

},

// 获取所有已发现的设备  

lanya4: function () {

var that = this;

wx.getBluetoothDevices({

success: function (res) {

console.log(res);

//    是否有已连接设备  

// wx.getConnectedBluetoothDevices({

//   success: function (res) {

//     console.log(res.devices);

//    console.log(JSON.stringify(res.devices));

//     // that.setData({

//     //   connectedDeviceId: res.deviceId

//     // })

//   }

// })

console.log(JSON.stringify(res.devices));

that.setData({

msg: “搜索设备” + JSON.stringify(res.devices),

devices: res.devices,

})

//监听蓝牙适配器状态  

wx.onBluetoothAdapterStateChange(function (res) {

that.setData({

sousuo: res.discovering ? “在搜索。” : “未搜索。”,

status: res.available ? “可用。” : “不可用。”,

})

})

}

})

},

//停止搜索周边设备  

lanya5: function () {

var that = this;

wx.stopBluetoothDevicesDiscovery({

success: function (res) {

that.setData({

msg: “停止搜索周边设备” + “/” + JSON.stringify(res.errMsg),

sousuo: res.discovering ? “在搜索。” : “未搜索。”,

status: res.available ? “可用。” : “不可用。”,

})

}

})

},

//连接设备  

connectTO: function (e) {

var that = this;

//  e.currentTarget.id 能打印是设备id,但是一直连接失败。场景:ios连接安卓手机,ios连接电脑蓝牙 ,安桌连接电脑蓝牙。

console.log("===="+e.currentTarget.id),

wx.createBLEConnection({

deviceId: e.currentTarget.id,

success: function (res) {

console.log(res);

that.setData({

connectedDeviceId: e.currentTarget.id,

msg: “已连接” + e.currentTarget.id,

msg1: “”,

})

},

fail: function () {

console.log(“调用失败”);

},

complete: function () {

console.log(“调用结束”);

}

})

console.log(that.data.connectedDeviceId);

},

})

回到顶部