微信小程序蓝牙连接打印机
发表时间:2020-10-12
发布人:葵宇科技
浏览次数:82
需求:蓝牙搜索打印机并打印信息
微信小程序中需要打印订单信息
1.搜索打印机蓝牙,并链接
2.发送打印
微信小程序连接蓝牙
1.初始化蓝牙
1.1 wx.openBluetoothAdapter(Object object) :初始化蓝牙
1.2 wx.getBluetoothAdapterState():检测本机蓝牙是否可用
wx.openBluetoothAdapter({//初始化蓝牙
success: function (res) {
wx.getBluetoothAdapterState({//获取本机蓝牙适配器状态。
success: function (res) {
if (res.available) {//蓝牙适配器是否可用
//下一步操作
} else {
wx.showModal({
title: '提示',
content: '本机蓝牙不可用',
})
}
}
})
},
fail: function () {
wx.showModal({
title: '提示',
content: '蓝牙初始化失败,请打开蓝牙',
})
}
})
2.开始搜索蓝牙设备
2.1 wx.startBluetoothDevicesDiscovery() : 开始搜索蓝牙设备
2.2 wx.getBluetoothDevices():获取搜索到的蓝牙设备
wx.startBluetoothDevicesDiscovery({//搜索附近的蓝牙设备
services: [],//筛选过滤搜索的蓝牙设备
success(res) {
setTimeout(() => {
wx.getBluetoothDevices({//获取蓝牙列表
services: [],
success: function (res) {
callback(res);//返回蓝牙列表
},
fail(res) {
wx.showModal({
title: '提示',
content: '获取蓝牙设备列表失败',
})
}
})
}, 2000)
//关闭搜索
wx.stopBluetoothDevicesDiscovery({
success: function (res) {}
})
}
})
注:搜索完后要停止搜索
3.连接蓝牙
wx.createBLEConnection():连接低功耗蓝牙设备
匹配到的蓝牙设备ID 发送连接蓝牙的请求
连接成功之后 应该断开蓝牙搜索的api,然后去获取所连接蓝牙设备的service服务
wx.createBLEConnection({//连接低功耗蓝牙设备
deviceId: _this.blue_data.device_id,//用于区分设备的 id
success(res) {
},
fail: function fail(err) {
wx.showToast({
title: '连接失败!',
icon: 'none',
duration: 2000
});
}
})
4.获取蓝牙设备的service服务
wx.getBLEDeviceServices():连接低功耗蓝牙设备
获取的serviceId有多个要试着连接最终确定哪个是稳定版本的service 获取服务完后获取设备特征值
wx.getBLEDeviceServices({//获取蓝牙设备所有服务(service)
deviceId: _this.blue_data.device_id,
success: function (res) {
_this.blue_data.service_id = service_id;
_this.getDeviceCharacter();
}
})
5.获取连接设备服务的所有特征值
wx.getBLEDeviceCharacteristics():获取蓝牙设备特征值
获取到的特征值有多个,最后要用的是能读,能写,能监听的那个值的uuid作为特征值id
wx.getBLEDeviceCharacteristics({
deviceId: _this.blue_data.device_id,
serviceId: _this.blue_data.service_id,
success: function (res) {
let notify_id, write_id, read_id;
for (let i = 0; i < res.characteristics.length; i++) {
let charc = res.characteristics[i];
if (charc.properties.notify) {
notify_id = charc.uuid;
}
if (charc.properties.write) {
write_id = charc.uuid;
}
if (charc.properties.write) {
read_id = charc.uuid;
}
}
if (notify_id != null && write_id != null) {
_this.blue_data.notify_id = notify_id;
_this.blue_data.write_id = write_id;
_this.blue_data.read_id = read_id;
}
}
})
完整代码
let blueApi ={
//连接的蓝牙信息
blue_data: {
device_id: "",
service_id: "",
write_id: "",
notify_id:"",
connFlag:false
},
//第一步:连接蓝牙
connect() {
if (!wx.openBluetoothAdapter) {
wx.showModal({
title: '提示',
content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。',
})
return;
}
var _this = this;
wx.openBluetoothAdapter({//初始化蓝牙
success: function (res) {
wx.getBluetoothAdapterState({//获取本机蓝牙适配器状态。
success: function (res) {
if (res.available) {//蓝牙适配器是否可用
if (res.discovering) {//蓝牙适配器是否处于搜索状态
wx.stopBluetoothDevicesDiscovery({
success: function (res) {}
})
}
_this.getBlueState();
} else {
wx.showModal({
title: '提示',
content: '本机蓝牙不可用',
})
}
}
})
},
fail: function () {
wx.showModal({
title: '提示',
content: '蓝牙初始化失败,请打开蓝牙',
})
}
})
},
//第二步:搜索附近蓝牙,并获取蓝牙列表
startSearch(callback) {
var _this = this;
wx.startBluetoothDevicesDiscovery({//搜索附近的蓝牙设备
services: [],//筛选过滤搜索的蓝牙设备
success(res) {
setTimeout(() => {
wx.getBluetoothDevices({//获取蓝牙列表
services: [],
success: function (res) {
callback(res);//返回蓝牙列表
},
fail(res) {
wx.showModal({
title: '提示',
content: '获取蓝牙设备列表失败',
})
}
})
}, 2000)
_this.stopSearch();
}
})
},
//第三步:选中列表中蓝牙设备,连接蓝牙设备
connectDevice(callback) {
var _this = this;
wx.createBLEConnection({//连接低功耗蓝牙设备
deviceId: _this.blue_data.device_id,//用于区分设备的 id
success(res) {
callback(res);
},
fail: function fail(err) {
wx.showToast({
title: '连接失败!',
icon: 'none',
duration: 2000
});
}
})
},
//第四步:获取蓝牙设备所有服务
getDeviceService() {
var _this = this;
wx.getBLEDeviceServices({//获取蓝牙设备所有服务(service)
deviceId: _this.blue_data.device_id,
success: function (res) {
_this.blue_data.service_id = service_id;
_this.getDeviceCharacter();
}
})
},
//第五步:获取连接设备服务的所有特征值
getDeviceCharacter() {
let _this = this;
wx.getBLEDeviceCharacteristics({
deviceId: _this.blue_data.device_id,
serviceId: _this.blue_data.service_id,
success: function (res) {
let notify_id, write_id, read_id;
for (let i = 0; i < res.characteristics.length; i++) {
let charc = res.characteristics[i];
if (charc.properties.notify) {
notify_id = charc.uuid;
}
if (charc.properties.write) {
write_id = charc.uuid;
}
if (charc.properties.write) {
read_id = charc.uuid;
}
}
if (notify_id != null && write_id != null) {
_this.blue_data.notify_id = notify_id;
_this.blue_data.write_id = write_id;
_this.blue_data.read_id = read_id;
}
}
})
},
//打印
printer(buffer,success){
var _this = this;
wx.showLoading({
title: '正在打印...',
});
_this.getDeviceService();
wx.writeBLECharacteristicValue({
deviceId: _this.blue_data.device_id,
serviceId: _this.blue_data.service_id,
characteristicId:_this.blue_data.notify_id,
value: buffer, // 这里的value是ArrayBuffer类型,中间层传过来的打印数据前端自己做转换,转换过程我这边就不描述了;
success(res) {
success(res)
wx.hideLoading();
},
fail: function fail(res) {
console.log("数据发送失败:" + JSON.stringify(res));
},
complete:function(){
_this.closeBluetoothAdapter();
}
})
},
/* 辅助模块 */
stopSearch() {
var _this = this;
wx.stopBluetoothDevicesDiscovery({
success: function (res) { }
})
},
//关闭蓝牙适配器
closeBluetoothAdapter() {
wx.closeBluetoothAdapter({
success: function success(res) {
console.log('关闭蓝牙适配器');
}
});
},
breakConnction(){
var _this = this;
wx.closeBLEConnection({
deviceId: _this.blue_data.device_id,
success:function(){
_this.blue_data.connFlag = false;
console.log("断开连接");
}
})
},
/* 优化 */
//第二步:连接设备模块
getBlueState() {
console.log("连接设备");
var _this = this;
if (_this.blue_data.device_id != "") {
wx.showLoading({
title: '正在连接',
})
_this.connectDevice(function(res){
wx.hideLoading();
if (res.errCode == 0) {
_this.blue_data.connFlag = true;
wx.showToast({
title: '连接成功',
icon: 'success',
duration: 1000
})
}
blue.stopSearch();
});
return;
} else {
wx.navigateTo({
url: '/printer/scan/scan',
})
}
}
}
module.exports = {
blue:blueApi
}
参考:https://imweb.io/topic/5bf3ce90611a25cc7bf1d788