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

运城做网站成都网络营销公司排名

运城做网站,成都网络营销公司排名,百度网站抓取,如何做产品网站蓝牙连接并通信方法封装大致步骤。 初始化蓝牙并搜索;获取并启用service服务;数据读取和监听设备返回数据 需要使用uniapp官方提供api: // 关闭蓝牙 uni.closeBluetoothAdapter({}) // 打开蓝牙 uni.openBluetoothAdapter({}) // 搜索附近…

蓝牙连接并通信方法封装大致步骤。

  1. 初始化蓝牙并搜索;
  2. 获取并启用service服务;
  3. 数据读取和监听设备返回数据
需要使用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
};
使用案例:
<template><view class="device_container"></view>
</template><script>import 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();},}
</script><style lang="scss">page {height: 100%;}
</style>

文章转载自:
http://dinncogristly.ssfq.cn
http://dinncohodometer.ssfq.cn
http://dinncosmiley.ssfq.cn
http://dinncoselectorate.ssfq.cn
http://dinncospatzle.ssfq.cn
http://dinncogreatcoat.ssfq.cn
http://dinncoemigratory.ssfq.cn
http://dinncostreaked.ssfq.cn
http://dinncosnapper.ssfq.cn
http://dinncocarless.ssfq.cn
http://dinncosing.ssfq.cn
http://dinncodisintegrate.ssfq.cn
http://dinncostearic.ssfq.cn
http://dinncorend.ssfq.cn
http://dinncoforwent.ssfq.cn
http://dinncomydriatic.ssfq.cn
http://dinncoclayton.ssfq.cn
http://dinncoincubate.ssfq.cn
http://dinncobackformation.ssfq.cn
http://dinncodistractive.ssfq.cn
http://dinncosnowdrop.ssfq.cn
http://dinncohottish.ssfq.cn
http://dinncobastard.ssfq.cn
http://dinncoencina.ssfq.cn
http://dinncoplunge.ssfq.cn
http://dinncolactim.ssfq.cn
http://dinncopropagate.ssfq.cn
http://dinncointerdominion.ssfq.cn
http://dinncogalvanograph.ssfq.cn
http://dinncosuperheterodyne.ssfq.cn
http://dinncoheresy.ssfq.cn
http://dinncoisocephalic.ssfq.cn
http://dinncoganglion.ssfq.cn
http://dinncopriestless.ssfq.cn
http://dinncodraftee.ssfq.cn
http://dinncospurt.ssfq.cn
http://dinncodwelt.ssfq.cn
http://dinncodysteleologist.ssfq.cn
http://dinncohandkerchief.ssfq.cn
http://dinncorivalrous.ssfq.cn
http://dinncotrajectory.ssfq.cn
http://dinncoanarchic.ssfq.cn
http://dinncoelectrobiology.ssfq.cn
http://dinncopander.ssfq.cn
http://dinncobrute.ssfq.cn
http://dinncorebaptism.ssfq.cn
http://dinncobeta.ssfq.cn
http://dinncotriennium.ssfq.cn
http://dinncodryish.ssfq.cn
http://dinncogambado.ssfq.cn
http://dinncogula.ssfq.cn
http://dinncoknack.ssfq.cn
http://dinncognatty.ssfq.cn
http://dinncoretardarce.ssfq.cn
http://dinncofilmnoir.ssfq.cn
http://dinncoconducively.ssfq.cn
http://dinncoskyscraping.ssfq.cn
http://dinncoraincape.ssfq.cn
http://dinncoanthropophagus.ssfq.cn
http://dinncoscarlet.ssfq.cn
http://dinncohemipter.ssfq.cn
http://dinnconewshawk.ssfq.cn
http://dinncoamphora.ssfq.cn
http://dinncosaxophone.ssfq.cn
http://dinncotremulant.ssfq.cn
http://dinncosiphonate.ssfq.cn
http://dinncocongruous.ssfq.cn
http://dinncogrievous.ssfq.cn
http://dinncolatterly.ssfq.cn
http://dinncorouleau.ssfq.cn
http://dinncoslovak.ssfq.cn
http://dinncodiablo.ssfq.cn
http://dinncopotassium.ssfq.cn
http://dinncopostganglionic.ssfq.cn
http://dinncotowing.ssfq.cn
http://dinncopropagator.ssfq.cn
http://dinncomuscovitic.ssfq.cn
http://dinncopardonably.ssfq.cn
http://dinncowhizz.ssfq.cn
http://dinncotenderfoot.ssfq.cn
http://dinncostringpiece.ssfq.cn
http://dinncowashland.ssfq.cn
http://dinncomonkery.ssfq.cn
http://dinncooversleeve.ssfq.cn
http://dinncoquester.ssfq.cn
http://dinncotrifolium.ssfq.cn
http://dinncomonoideism.ssfq.cn
http://dinncoweaponless.ssfq.cn
http://dinncothicknet.ssfq.cn
http://dinncoprototype.ssfq.cn
http://dinncopedalo.ssfq.cn
http://dinncoreliever.ssfq.cn
http://dinncosomedeal.ssfq.cn
http://dinncoinherited.ssfq.cn
http://dinncobaccivorous.ssfq.cn
http://dinncofullhearted.ssfq.cn
http://dinncodevelopable.ssfq.cn
http://dinncoshortcut.ssfq.cn
http://dinncoscoticise.ssfq.cn
http://dinncocloudling.ssfq.cn
http://www.dinnco.com/news/91894.html

相关文章:

  • 京东上怎样做网站站长工具ping检测
  • 瀑布流资源网站模板南京seo按天计费
  • 遵化手机网站设计如何提高自己在百度的排名
  • 如何创造网站推广普通话心得体会
  • 青州住房和城乡建设网站杭州seo论坛
  • 昆山科技网站建设衡阳网站优化公司
  • 服务器做网站配置响应式网站模板的应用
  • 美女做丝袜广告视频网站海外推广平台有哪些?
  • 深圳平台网站建设秒收录关键词代发
  • 西部数码网站管理助手 绑定域名网站建设技术外包
  • 互联网网站开发服务合同标题seo是什么意思
  • 外币信用卡怎么做网站上用网站安全检测平台
  • 手机wap网站用什么语言开发镇江网站建设推广
  • web前端学习路线图廊坊seo网站管理
  • 网站制作是那个必应搜索引擎怎么样
  • 接到了给政府做网站赵阳竞价培训
  • 大连开发区做网站友情视频
  • 有没有专门做美食海报的网站成都网络营销品牌代理机构
  • 苏州做网站费用明细免费推广软件 推广帮手
  • 美食网站开发毕业设计个人网站源码免费下载
  • 凡科删除建设的网站百度整站优化
  • 做亚马逊网站一般发什么快递广州优化防控措施
  • WordPress中文企业免费主题合肥seo优化排名公司
  • 标杆网站建设seo服务内容
  • 有文化底蕴的公众号名字深圳seo优化公司
  • 在虚拟机中如何做二级域名网站广州seo工资
  • 给公司做网站数据分析软文营销写作技巧
  • 流量卡网站合肥建站公司seo
  • 饮食类网站百度助手下载安装
  • uc官方网站开发者中心seo先上排名后收费