收藏
回答

wx.writeBLECharacteristicValue写入成功,打印机没反应?

wx.writeBLECharacteristicValue,调试基础库2.11.3 安卓机

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

2 个回答

  • 社区技术运营专员-Jahozheng
    社区技术运营专员-Jahozheng
    2020-12-03

    你好,iOS上是正常的吗

    2020-12-03
    有用
    回复 3
  • Giggle圆
    Giggle圆
    03-26
    //第一阶段 
    			// 1.确定蓝牙状态
    			openBluetooth: function() {
    				var that = this
    				wx.openBluetoothAdapter({
    					success: function(res) {
    						that.getBluetoothAdapterState();
    					},
    					fail: function() {
    						wx.showModal({
    							title: '提示',
    							content: '请打开蓝牙和定位功能',
    						})
    					}
    				})
    			},
    			//2.获取本机蓝牙适配器状态
    			getBluetoothAdapterState: function() {
    				let that = this
    				wx.getBluetoothAdapterState({
    					success: function(res) {
    						if (res.available) { //蓝牙适配器是否可用
    							that.startBluetoothDevicesDiscovery();
    							if (res.discovering) { //是否正在搜索设备
    								// wx.stopBluetoothDevicesDiscovery({
    								// 	success: function(res) {
    								// 		console.log(res)
    								// 	}
    								// })
    							}
    						} else {
    							wx.showModal({
    								title: '提示',
    								content: '本机蓝牙不可用',
    							})
    						}
    					},
    				})
    			},
    			// 3.开始搜索蓝牙设备
    			startBluetoothDevicesDiscovery: function() {
    				var that = this
    				wx.startBluetoothDevicesDiscovery({
    					success: function(res) {
    						that.getBluetoothDevices(res);
    					},
    					fail: function() {
    						wx.showModal({
    							title: '提示',
    							content: '搜索周边设备失败:' + res,
    						})
    					}
    				})
    			},
    			//4.获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
    			getBluetoothDevices: function(res) {
    				let that = this
    				wx.getBluetoothDevices({
    					success: function(res) {
    						let devices = JSON.stringify(res.devices);
    						that.devicesLists = res.devices.filter(item => item.name != '未知设备');
    						if (devices.length > 0) {
    							// 在蓝牙模块使用流程结束后及时调用 wx.closeBluetoothAdapter 释放资源
    							// wx.stopBluetoothDevicesDiscovery({
    							// 	success: function(res) {
    							// 		console.log(res)
    							// 	}
    							// })
    						}
    					},
    					fail: function() {
    						wx.showModal({
    							title: '提示',
    							content: '获取蓝牙设备失败:' + res,
    						})
    					}
    				})
    			},
    //第二阶段
    			//1.连接用户点击选中的设备
    			createBLEConnection(deviceId) { //连接选中的设备
    				var that = this
    				that.deviceId = deviceId
    				wx.createBLEConnection({
    					// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
    					deviceId,
    					success(res) {
    						that.getBLEDeviceServices();
    					},
    					fail() {
    						wx.showModal({
    							title: '提示',
    							content: '连接' + deviceId + '失败',
    						})
    					}
    				})
    			},
    			//2.获取设备服务信息
    			getBLEDeviceServices: function() {
    				var that = this
    				wx.getBLEDeviceServices({
    					deviceId: that.deviceId,
    					success: function(res) {
    						/* 获取连接设备的所有特征值 */
    						for (let i = 0; i < res.services.length; i++) {
    							that.getDeviceCharacteristics(res.services[i])
    						}
    					},
    					fail: function() {
    						wx.showModal({
    							title: '提示',
    							content: '获取设备服务信息失败',
    						})
    					}
    				})
    			},
    			//3.获取设备特征
    			getDeviceCharacteristics(services) {
    				let that = this
    				wx.getBLEDeviceCharacteristics({ //获取蓝牙设备某个服务中所有特征值(characteristic)
    					deviceId: that.deviceId,
    					serviceId: services.uuid,
    					success: function(res) {
    						for (let i = 0; i < res.characteristics.length; i++) {
    							let item = res.characteristics[i]
    							// if (item.properties.read && item.properties.write && (item.properties.notify || item.properties.indicate)) {
    							if (item.properties.write && item.properties.notify && item.properties.indicate && item.properties.read) {
    
    
    								that.characteristicId = item.uuid
    								that.serviceId = services.uuid
    
    
    								var str =
    									'<div style="padding:10% 20px"><h4 style="text-align:center;">尊敬的会员您好,</h4></div>';
    								that.writeBLECharacteristicValue('1')
    								console.log('获取到具有写功能的特征了', item)
    								return
    							}
    						}
    
    
    					}
    				})
    			},
    			//4.往蓝牙写数据
    			writeBLECharacteristicValue(str) {
    				let that = this
    				let dataBuffer = new ArrayBuffer(str.length)
    				let dataView = new DataView(dataBuffer)
    				for (var i = 0; i < str.length; i++) {
    					dataView.setUint8(i, str[i])
    				}
    				// console.log('dataBuffer', dataBuffer)
    				// console.log('deviceId', that.deviceId)
    				// console.log('serviceId', that.serviceId)
    				// console.log('characteristicId', that.characteristicId)
    				wx.writeBLECharacteristicValue({
    					deviceId: that.deviceId,
    					serviceId: that.serviceId,
    					characteristicId: that.characteristicId,
    					value: dataBuffer,
    					success: function(res) {
    						console.log('写入成功', res)
    						wx.closeBLEConnection({
    							deviceId: that.deviceId,
    							success(res) {
    								console.log('关闭连接这个设备', res)
    							}
    						})
    
    
    					},
    					fail: function(res) {
    						wx.showModal({
    							title: '提示',
    							content: '写入失败',
    						})
    					}
    				})
    			},
    
    
    
    03-26
    有用
    回复
登录 后发表内容
问题标签