收藏
回答

链接,写入打印机成功,打印机没有相应

小程序连接和写入打印机的过程都ok,可是打印机就是没有相应!求解

代码贴出:

// 初始化蓝牙适配器  

initializeBluetooth: function () {

var that = this;

if (!wx.openBluetoothAdapter) {

console.log('蓝牙适配器打开失败,请检查微信版本或手机是否支持小程序蓝牙模块!')

} else {

wx.openBluetoothAdapter({

success: function (res) {

that.setData({

msg: "初始化蓝牙适配器成功!"

})

wx.getBluetoothAdapterState({//获取本机蓝牙适配器状态

success: function (res) {

console.log('本机蓝牙适配器状态:')

console.log(res)

}

})

}

})

}

},


//搜索获取已发现设备  

searchBluetooth: function () {

var that = this;

wx.startBluetoothDevicesDiscovery({//开始搜寻附近的蓝牙外围设备

success: function (res) {

console.log('开始搜索周边蓝牙设备')

console.log(res)

wx.getBluetoothDevices({//sucess返回uuid 对应的的已连接设备列表,Array类型

success: function (res) {

//是否有已连接设备

wx.getConnectedBluetoothDevices({////根据 uuid 获取处于已连接状态的设备

success: function (res) {

console.log('已连接的蓝牙设备:')

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

that.setData({

connectedDeviceId: res.deviceId

})

}

})

that.setData({

devices: res.devices,

})

}

})

}

})

},


//连接设备  

connectTO: function (e) {

var that = this;

wx.stopBluetoothDevicesDiscovery({ //先停止搜索周边设备

success: function (res) {

console.log('连接设备前,先停止搜索周边设备:')

console.log(res)

}

})

wx.showLoading({

title: '连接蓝牙设备中...',

})

wx.createBLEConnection({//若小程序在之前已有搜索过某个蓝牙设备,并成功建立链接,可直接传入之前搜索获取的deviceId直接尝试连接该设备,无需进行搜索操作。

deviceId: e.currentTarget.id,

success: function (res) {

console.log('连接成功:')

console.log(res)

wx.hideLoading()

that.setData({

connectedDeviceId: e.currentTarget.id,    //currentTarget: 事件绑定的元素

msg: "已连接" + e.currentTarget.id,

})

that.getServices();

that.startBletNotify();

that.receiveMessages();

},

fail: function () {

console.log("调用失败");

},

complete: function () {

console.log('已连接设备ID:' + that.data.connectedDeviceId);

console.log("调用结束");

}

})

},


// 获取连接设备的service服务  

getServices: function () {

var that = this;

wx.getBLEDeviceServices({//获取在小程序蓝牙模块生效期间所有已发现的蓝牙设备,包括已经和本机处于连接状态的设备

// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取  

deviceId: that.data.connectedDeviceId,

success: function (res) {

//console.log('获取蓝牙设备所有服务成功:', res);

that.data.services = res.services

console.log('获取蓝牙设备所有服务成功:', that.data.services);

that.setData({

serviceId: that.data.services[0].uuid,

})

console.log("服务uuid:", that.data.serviceId)

wx.getBLEDeviceCharacteristics({

// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取  

deviceId: that.data.connectedDeviceId,

// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取  

serviceId: that.data.serviceId,   //-----注意是that.data.services[0].uuid

success: function (res) {

console.log('serviceId: that.data.services[0].uuid: ', that.data.serviceId)

console.log(res)

for (var i = 0; i < res.characteristics.length; i++) {

if (res.characteristics[i].properties.notify) {   //注意characteristic(特征值)信息,properties对象

that.setData({

notifyServicweId: that.data.services[0].uuid,

notifyCharacteristicsId: res.characteristics[i].uuid,

})

console.log("notifyServicweId:", that.data.notifyServicweId, "notifyCharacteristicsId", that.data.notifyCharacteristicsId)

}

if (res.characteristics[i].properties.write) {

that.setData({

writeServicweId: that.data.services[0].uuid,

writeCharacteristicsId: res.characteristics[i].uuid,

})

console.log("writeServicweId:", that.data.writeServicweId, "writeCharacteristicsId", that.data.writeCharacteristicsId)

} else if (res.characteristics[i].properties.read) {

that.setData({

readServicweId: that.data.services[0].uuid,

readCharacteristicsId: res.characteristics[i].uuid,

})

console.log("readServicweId:", that.data.readServicweId, "readCharacteristicsId", that.data.readCharacteristicsId)

}

}

},

fail: function () {

console.log("获取连接设备的所有特征值:", res);

},

complete: function () {

console.log("complete!");

}

})

}

})

},



//断开设备连接  

closeBluetooth: function () {

var that = this;

wx.closeBLEConnection({

deviceId: that.data.connectedDeviceId,

success: function (res) {

console.log('断开设备连接: ', res)

if (res.errCode==0){

}

}

})

},

Test:function(){

this.sendMessages();

},

//发送  

sendMessages: function () {

var that = this;

// 这里的回调可以获取到 write 导致的特征值改变  

wx.onBLECharacteristicValueChange(function (characteristic) {

console.log('characteristic value changed:1', characteristic)

})

var buf = new ArrayBuffer(16)

var dataView = new DataView(buf)

dataView.setUint8(0, 99)

wx.writeBLECharacteristicValue({

deviceId: that.data.connectedDeviceId,

serviceId: that.data.writeServicweId,

characteristicId: that.data.writeCharacteristicsId,

value: buf,

success: function (res) {

console.log('writeBLECharacteristicValue success', res)

}

})

},


//启用低功耗蓝牙设备特征值变化时的 notify 功能  

startBletNotify: function () {

var that = this;

wx.notifyBLECharacteristicValueChange({

state: true, // 启用 notify 功能  

deviceId: that.data.connectedDeviceId,

serviceId: that.data.notifyServicweId,

characteristicId: that.data.notifyCharacteristicsId,

success: function (res) {

console.log('notifyBLECharacteristicValueChange success', res.errMsg)

},

fail: function () {

console.log('启用notify功能失败!');

console.log(that.data.notifyServicweId);

console.log(that.data.notifyCharacteristicsId);

},

})

},


//接收消息  

receiveMessages: function () {

var that = this;

// 必须在这里的回调才能获取  

wx.onBLECharacteristicValueChange(function (characteristic) {

let hex = Array.prototype.map.call(new Uint8Array(characteristic.value), x => ('00' + x.toString(16)).slice(-2)).join('');

console.log(hex)

})

console.log(that.data.readServicweId);

console.log(that.data.readCharacteristicsId);

wx.readBLECharacteristicValue({

deviceId: that.data.connectedDeviceId,

serviceId: that.data.readServicweId,

characteristicId: that.data.readCharacteristicsId,

success: function (res) {

console.log('readBLECharacteristicValue:', res.errMsg);

}

})

},


回答关注问题邀请回答
收藏

12 个回答

  • 刘庆
    刘庆
    2018-09-04

    现在解决了吗,怎么解决的

    2018-09-04
    有用
    回复
  • 范橙(吉栗收银)13280828588
    范橙(吉栗收银)13280828588
    2018-07-15

    需要低功耗蓝牙的打印机  看看这个 :https://developers.weixin.qq.com/blogdetail?action=get_post_info&docid=7e85fa4514a56b7928646f96dd4bcabe&highline=%E8%93%9D%E7%89%99%E6%89%93%E5%8D%B0%E6%9C%BA

    2018-07-15
    有用
    回复
  • ડρꫀᥴiꪖꪶ .
    ડρꫀᥴiꪖꪶ .
    2018-06-01

    你的打印指令都没有,你怎么打印?

    输入这个绝对有反应

    dataView.setUint8(0x1B, 0x40)

    2018-06-01
    有用
    回复
  • 无色
    无色
    2018-05-29

    大兄弟,有没有demo啊,需要学习

    2018-05-29
    有用
    回复
  • Mr strong
    Mr strong
    2018-04-24

    还没有用安卓试呢,主要是暂时没有机子


    2018-04-24
    有用
    回复
  • Allons-y
    Allons-y
    2018-04-24

    你的安卓能连接上吗

    2018-04-24
    有用
    回复
  • Allons-y
    Allons-y
    2018-04-24

    飞鹅FP-58AWG 有2个serviceId,每个只有一个characteristics,第一个全支持,第二个只支持read


    佳博GP-58MB,有5个serviceId,

    前两个serviceId有2个characteristics,都是第2个characteristics只支持write.

    第3个serviceId有4个characteristics,都支持read,write

    .第4个serviceId有1个characteristics,支持read,write.notify,indicate

    第3个serviceId有5个characteristics,只支持read


    2018-04-24
    有用
    回复
  • Mr strong
    Mr strong
    2018-04-24

    我看了文档好像只需要支持writhe,可是就是没有反应!!

    2018-04-24
    有用
    回复
  • Mr strong
    Mr strong
    2018-04-24

    你的打印机是什么牌子的?我的只有两个characteristics,一个是write,一个是notify

    2018-04-24
    有用
    回复
  • Allons-y
    Allons-y
    2018-04-24


    符合res.characteristics[i].properties.write==true   uuid的有很多,每个都试过。都不能用。uuid只要write支持就行吗,还是要read, notify,indicate都支持


    2018-04-24
    有用
    回复

正在加载...

登录 后发表内容