当前位置: 首页 > news >正文

导航网站模板软件技术招聘信息

导航网站模板,软件技术招聘信息,手机免费建网站软件,博客类网站怎么做蓝牙连接并通信方法封装大致步骤。 初始化蓝牙并搜索#xff1b;获取并启用service服务#xff1b;数据读取和监听设备返回数据 需要使用uniapp官方提供api#xff1a; // 关闭蓝牙 uni.closeBluetoothAdapter({}) // 打开蓝牙 uni.openBluetoothAdapter({}) // 搜索附近…蓝牙连接并通信方法封装大致步骤。 初始化蓝牙并搜索获取并启用service服务数据读取和监听设备返回数据 需要使用uniapp官方提供api // 关闭蓝牙 uni.closeBluetoothAdapter({}) // 打开蓝牙 uni.openBluetoothAdapter({}) // 搜索附近的蓝牙 uni.startBluetoothDevicesDiscovery({}) // 停止搜索 uni.stopBluetoothDevicesDiscovery({}) // 连接低功耗BLE蓝牙 uni.createBLEConnection({}) // 获取蓝牙设备所有服务(service) uni.getBLEDeviceServices({}) // 获取蓝牙特征值 uni.getBLEDeviceCharacteristics({}) // 启用低功耗蓝牙设备特征值变化时的 notify 功能订阅特征值 uni.notifyBLECharacteristicValueChange({}) // 监听低功耗蓝牙设备的特征值变化事件 uni.onBLECharacteristicValueChange({}) // 读取低功耗蓝牙设备的特征值的二进制数据值 uni.readBLECharacteristicValue({})1、开启蓝牙适配器初始化蓝牙模块获取手机蓝牙是否打开 const getBluetoothState () {return new Promise((resolve, reject) {uni.openBluetoothAdapter({mode: central, // 主机模式success: (r) {console.log(蓝牙初始化成功);// 获取蓝牙的匹配状态uni.getBluetoothAdapterState({success: function(row) {console.log(蓝牙状态, row.available);if (row.available) {// 蓝牙连接成功开启蓝牙设备搜索resolve();} else {// 请开启蓝牙uni.showToast({title: 请开启蓝牙,icon: none,duration: 2000});reject();}},fail: function(err) {// 请开启蓝牙uni.showToast({title: 请开启蓝牙,icon: none,duration: 2000});reject();}})},fail: () {// 请开启蓝牙uni.showToast({title: 请开启蓝牙,icon: none,duration: 2000});reject();}});}); }2、开启蓝牙设备搜索 const discoveryBluetooth () {return new Promise((resolve) {uni.startBluetoothDevicesDiscovery({success(res) {uni.showLoading({title: 正在搜索设备,icon:none,duration: 2000});console.log(搜索蓝牙外围设备完成, res)setTimeout(() {// 监听寻找到的蓝牙设备resolve();}, 2000);}});}); }3、获取搜索到的设备信息 const getBluetoothDevices () {return new Promise((resolve, reject) {uni.getBluetoothDevices({success(res) {// 过滤掉name为空或者未知设备的设备let devices res.devices.filter(function(obj) {return obj.name ! obj.name ! 未知设备});devices devices.forEach(item {// 获取指定连接deviceIdif(item.name item.name.substring(0, 5) aaaa-){// aaaa-*********蓝牙设备型号根据需求选择设备型号// item.deviceId;}});},fail: function() {console.log(搜索蓝牙设备失败);reject();},complete: function() {console.log(蓝牙搜索完成);resolve();}});}); }4、关闭蓝牙搜索 const stopDiscoveryBluetooth () {uni.stopBluetoothDevicesDiscovery({success(r) {console.log(停止搜索蓝牙设备, r);}}); };5、连接蓝牙 const connectBluetooth () {return new Promise((resolve, reject) {uni.createBLEConnection({deviceId: deviceId, // 设备idsuccess() {// 蓝牙连接成功后关闭蓝牙搜索并获取服务idstopDiscoveryBluetooth();resolve();// 获取服务idgetServiceId();},fail() {console.log(蓝牙连接失败);reject();}});}); };6、获取服务id const getServiceId () {uni.getBLEDeviceServices({deviceId: deviceId,success(res) {console.log(获取服务Id, res)// serviceId固定获取蓝牙设备某个服务中的所有特征值【读写属性】let model res.services[0];serviceId model.uuid;// 调用蓝牙监听和写入功能getCharacteId();},fail(err){console.log(获取服务失败, err);}}) };7、获取蓝牙低功耗设备某个服务中所有特征 const getCharacteId () {uni.getBLEDeviceCharacteristics({deviceId: deviceId, // 蓝牙设备idserviceId: serviceId, // 蓝牙服务UUIDsuccess(res) {console.log(数据监听, res);res.characteristics.forEach(item {// 003if (item.properties.notify true) {// 监听notify item.uuid;}// 002if (item.properties.write true) {// 写入let writeId item.uuid;}});},fail(err) {console.log(数据监听失败, err)}}) };8、监听设备返回数据启用低功耗蓝牙设备特征值变化时的notify功能 const startNotice () {uni.notifyBLECharacteristicValueChange({characteristicId: notify,deviceId: deviceId,serviceId: serviceId,state: true,success(res) {// 监听低功耗蓝牙设备的特征值变化uni.onBLECharacteristicValueChange(result {console.log(监听低功耗蓝牙设备的特征值变化, result);if (result.value) {console.log(设备返回数据, result.value)}})}}); };9、向蓝牙设备发送数据 const writeData (buffer) {return new Promise((resolve, reject) {uni.writeBLECharacteristicValue({characteristicId: uni.getStorageSync(writeId),deviceId: deviceId,serviceId: serviceId,value: buffer,success(res) {console.log(writeBLECharacteristicValue success, res);resolve();},fail(err) {console.log(报错了, err);reject();}});}); };封装完整方法 import { TextDecoder } from text-encoding-utf-8; let bluetoothOpen false; // 手机蓝牙是否打开 let bluetoothConnect false; // 设备和蓝牙是否连接 let isHaveDevice false; // 是否查找到设备 let deviceId null; // 设备id let serviceId null; // 服务id let notify null; // 监听uuid let writeId null; // 写入uuid /*** 获取手机蓝牙是否打开*/ const getBluetoothState () {// 主机模式return new Promise((resolve, reject) {uni.openBluetoothAdapter({mode: central,success: (r) {console.log(蓝牙初始化成功);// 获取蓝牙的匹配状态uni.getBluetoothAdapterState({success: function(row) {console.log(蓝牙状态, row.available);if (row.available) {bluetoothOpen true;resolve();} else {// 请开启蓝牙bluetoothOpen false;bluetoothConnect false;reject();}},fail: function(err) {// 请开启蓝牙bluetoothOpen false;bluetoothConnect false;reject();}})},fail: () {// 请开启蓝牙bluetoothOpen false;bluetoothConnect false;reject();}});}); }; /*** 开始搜索蓝牙设备*/ const discoveryBluetooth () {return new Promise((resolve) {uni.startBluetoothDevicesDiscovery({success(res) {console.log(搜索蓝牙外围设备完成, res)setTimeout(() {resolve();}, 2000);}});}) }; // 关闭蓝牙搜索 const stopDiscoveryBluetooth () {uni.stopBluetoothDevicesDiscovery({success(r) {console.log(停止搜索蓝牙设备, r);}}); }; /*** 获取搜索到的设备信息*/ const getBluetoothDevices () {return new Promise((resolve, reject) {uni.getBluetoothDevices({success(res) {bluetoothConnect false;// 过滤掉name为空或者未知设备的设备let devices res.devices.filter(function(obj) {return obj.name ! obj.name ! 未知设备});devices devices.forEach(item {if(item.name item.name.substring(0, 5) aaaa-){deviceId item.deviceId;}});},fail: function() {console.log(搜索蓝牙设备失败);bluetoothConnect false;reject();},complete: function() {console.log(蓝牙搜索完成);// 是否具有当前设备if (deviceId) {isHaveDevice true;} else {isHaveDevice false;}resolve(isHaveDevice);}});}); } /*** 连接蓝牙* deviceId 蓝牙设备id*/ const connectBluetooth () {return new Promise((resolve, reject) {uni.createBLEConnection({deviceId: deviceId, // 设备idsuccess() {bluetoothConnect true;// 蓝牙连接成功后关闭蓝牙搜索stopDiscoveryBluetooth();resolve();// 获取服务idgetServiceId();},fail() {bluetoothConnect false;console.log(蓝牙连接失败);reject();}});}); }; // 获取服务id const getServiceId () {uni.getBLEDeviceServices({deviceId: deviceId,success(res) {console.log(获取服务Id, res)let model res.services[0];serviceId model.uuid;// 调用蓝牙监听和写入功能getCharacteId();}}) }; // 获取蓝牙低功耗设备某个服务中所有特征 const getCharacteId () {uni.getBLEDeviceCharacteristics({deviceId: deviceId, // 蓝牙设备idserviceId: serviceId, // 蓝牙服务UUIDsuccess(res) {console.log(数据监听, res);res.characteristics.forEach(item {// 003if (item.properties.notify true) {// 监听notify item.uuid;startNotice();}// 002if (item.properties.write true) {// 写入let writeId item.uuid;uni.setStorageSync(writeId, item.uuid);}});},fail(err) {console.log(数据监听失败, err)}}) }; // 启用低功耗蓝牙设备特征值变化时的notify功能 const startNotice () {uni.notifyBLECharacteristicValueChange({characteristicId: notify,deviceId: deviceId,serviceId: serviceId,state: true,success(res) {// 监听低功耗蓝牙设备的特征值变化uni.onBLECharacteristicValueChange(result {console.log(监听低功耗蓝牙设备的特征值变化, result);if (result.value) {let decoder new TextDecoder(utf-8);let data decoder.decode(result.value);console.log(帽子返回数据, data)}})}}); }; // 蓝牙发送数据 const writeData (buffer) {return new Promise((resolve, reject) {uni.writeBLECharacteristicValue({characteristicId: uni.getStorageSync(writeId),deviceId: deviceId,serviceId: serviceId,value: buffer,success(res) {console.log(writeBLECharacteristicValue success, res);resolve();},fail(err) {console.log(报错了, err);reject();}});}); }; export default {getBluetoothState,discoveryBluetooth,stopDiscoveryBluetooth,getBluetoothDevices,connectBluetooth,getServiceId,getCharacteId,startNotice,writeData };使用案例 templateview classdevice_container/view /templatescriptimport bluetooth from ../bluetooth.js;export default {data() {return {bluetoothStatus: false, // 蓝牙连接状态}},methods: {// 获取蓝牙和设备是否已连接initBluetooth() {let _this this;// 初始化蓝牙bluetooth.getBluetoothState().then(() {// 搜索外围蓝牙设备bluetooth.discoveryBluetooth().then(() {this.discoveryLoading true;// 获取蓝牙设备bluetooth.getBluetoothDevices().then((isHaveDevice) {if (isHaveDevice) {// 搜索到指定设备,连接蓝牙bluetooth.connectBluetooth().then(() {_this.bluetoothStatus true;}, () {_this.bluetoothStatus false;});} else {// 未搜到设备_this.bluetoothStatus false;}}, () {// 蓝牙搜索失败_this.bluetoothStatus false;});});}, () {// 未开启蓝牙_this.bluetoothStatus false;});},// 向设备发送数据writeBlueData() {let obj {cmd: 3,freq: 430125,speakable: 1};let objStr JSON.stringify(obj);let buffer new ArrayBuffer(objStr.length); // 每个字符占用2个字节let bufView new Uint8Array(buffer);for (let i 0; i objStr.length; i) {bufView[i] objStr.charCodeAt(i);}bluetooth.writeData(buffer);}},onShow() {// 获取蓝牙和设备连接状态this.initBluetooth();},} /scriptstyle langscsspage {height: 100%;} /style
http://www.dnsts.com.cn/news/267212.html

相关文章:

  • 网页搜索快捷方式seo常见优化技术
  • 国外企业画册设计网站wordpress sae 上传
  • 当当网网站系统建设的意义自己做购物网站推广
  • 广州建网站的公司晋州做网站的联系电话
  • 建立网站需要什么大良手机网站建设
  • 河北网站建站系统哪家好网站空间后台密码
  • 如何发布自己的html网站大庆市城乡建设局网站
  • 手机网站的静态页面wordpress导航菜单加图片
  • 教做3d的网站微信公众号 网站开发 2016
  • 网站新闻百度收录wordpress 设置网站目录权限
  • 湛江制作网站企业徐州市建设工程交易网
  • 湛江有人做网站 的吗非凡网站建设
  • 购物网站哪个是正品网站建设合同的注意事项
  • 东莞规划局官方网站网站建设app开发合同
  • 如何建立个人网站网站不备案可以登录吗
  • 网站建设方案流程电脑网站开发者模式
  • 淘宝刷单网站开发蒙古文网站建设汇报材料
  • 台州城乡建设规划网站青岛知名网站建设
  • 大连专业html5网站建设wordpress win8 主题
  • 如何做网站关键词收录美食网页设计模板国内版
  • 保定做网站建设展示类网站开发费用
  • 哪个网站可以卖自己做的模型建个大型网站要多少钱
  • 安阳网站建设设计创意办公空间设计
  • 常熟祥云平台网站建设网站平台怎么做推广
  • 烤漆 东莞网站建设绵阳网站制作
  • 巴中做网站的公司wordpress 管理插件下载
  • 网站建设方面的文章全媒体运营师报考条件
  • 一家专门做打折的网站网站建设与管理好过吗
  • 在线设计的网站网站开发软件开发流程图
  • 做棋牌网站建设哪家便宜sh域名做的好的网站