1.微信小程序实现蓝牙打印(HBuilderX)-扫描设备

实现蓝牙打印的基本步骤

1.扫描周围设备,找到您型号的设备

2.记录设备,同时创建打印服务获取特征(后台)

3.根据设备ID和服务和特征直接打印(响应速度在0.5s左右,可驱动蓝牙进行打印)

微信小程序代码:

var qyl = {
    
    toast:function(str){
		wx.showToast({
		    icon: 'none',
		    title: str
		});
    },
    bluetooth:{
		
		stop:function(){
			wx.stopBluetoothDevicesDiscovery({
				success(res) {
					console.log("stop search")
				}
			})
		},
		
		search:function(callback2){
			
			var myOpen  = function(callback){
				wx.openBluetoothAdapter({//首先初始化蓝牙
					success(res) { 
						callback();
					},fail(res) {
						console.log(res)
						if (res.errCode == 10001) { 
							qyl.toast("蓝牙未打开,请先打开蓝牙");
							
						} else {
							qyl.toast(res.errMsg);
						}  
					}  
				});
			}
			
			var myStart = function(callback){
				var list = [];
				wx.getBluetoothDevices({ //找到老设备
				  success(res) {
				   var devices = res.devices;
				   for(var i=0;i<devices.length;i++){
					   list.push(devices[i]);
				   }
				  }
				})
				
				wx.startBluetoothDevicesDiscovery({ //发现新设备
				  success(res) {
					wx.onBluetoothDeviceFound(function(devices){
						var devices = devices.devices;
						for(var i=0;i<devices.length;i++){
							list.push(devices[i]);
						}
					})
				  }
				});
				
				//延迟函数
				var Delayed = function(ms,callback){
					return new Promise(function(Resolve,Peject){
						setTimeout(function(){
							Resolve(callback);
						},ms);
					});
				}
				//9s停止
				Delayed(9000).then( ()=>{
					qyl.bluetooth.stop();
					console.log(JSON.stringify(list));
					callback(list);
				});
				
			}
			
			//执行搜索
			myOpen(function(){
				myStart(callback2)
			})
		}
    }
}

//调用方式:qyl.bluetooth.search(function(list){  console.log(list)  });

 

HBuilderX 代码如下:

var qyl = {
    
    toast:function(str){
		uni.showToast({
		    icon: 'none',
		    title: str
		});
    },
    bluetooth:{
		
		stop:function(){
			uni.stopBluetoothDevicesDiscovery({
				success(res) {
					console.log("stop search")
				}
			})
		},
		
		search:function(callback2){
			
			var myOpen  = function(callback){
				uni.openBluetoothAdapter({//首先初始化蓝牙
					success(res) { 
						callback();
					},fail(res) {
						console.log(res)
						if (res.errCode == 10001) { 
							qyl.toast("蓝牙未打开,请先打开蓝牙");
							
						} else {
							qyl.toast(res.errMsg);
						}  
					}  
				});
			}
			
			var myStart = function(callback){
				var list = [];
				uni.getBluetoothDevices({ //找到老设备
				  success(res) {
				   var devices = res.devices;
				   for(var i=0;i<devices.length;i++){
					   list.push(devices[i]);
				   }
				  }
				})
				
				uni.startBluetoothDevicesDiscovery({ //发现新设备
				  success(res) {
					uni.onBluetoothDeviceFound(function(devices){
						var devices = devices.devices;
						for(var i=0;i<devices.length;i++){
							list.push(devices[i]);
						}
					})
				  }
				});
				
				//延迟函数
				var Delayed = function(ms,callback){
					return new Promise(function(Resolve,Peject){
						setTimeout(function(){
							Resolve(callback);
						},ms);
					});
				}
				//9s停止
				Delayed(9000).then( ()=>{
					qyl.bluetooth.stop();
					console.log(JSON.stringify(list));
					callback(list);
				});
				
			}
			
			//执行搜索
			myOpen(function(){
				myStart(callback2)
			})
		}
    }
}

//调用方式:qyl.bluetooth.search(function(list){  console.log(list)  });

界面效果:

 

调试结果:

格式化结果:

感谢您的支持,写的文章如对您有所帮助,开源不易,请您打赏,谢谢啦~

 


版权声明:本文为qilin001cs原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。