赞
踩
在项目中找到app.json或者game.json,声明需要用到的权限
- {
- "deviceOrientation": "portrait",
- "permission": {
- "scope.userLocation": {
- "desc": "你的旧版本的位置信息将用于小程序位置接口的效果展示"
- },
- "scope.userFuzzyLocation": {
- "desc": "你的模糊位置信息将用于小程序位置接口的效果展示"
- },
- "scope.bluetooth": {
- "desc": "需要蓝牙进行连接手柄"
- },
- "scope.userInfo": {
- "desc": "需要用户同意授权隐私信息,确保蓝牙功能可用"
- }
- }
- }
- // 通过 wx.getSetting 查询用户是否已授权头像昵称信息
- wx.getSetting({
- success (res){
- if (res.authSetting['scope.userInfo']) {
- // 已经授权,可以直接调用 getUserInfo 获取头像昵称
- wx.getUserInfo({
- success: function(res) {
- console.log("获取用户信息成功",res.userInfo)
- }
- })
- } else {
- // 否则,先通过 wx.createUserInfoButton 接口发起授权
- let button = wx.createUserInfoButton({
- type: 'text',
- text: '获取用户信息',
- style: {
- left: 10,
- top: 160,
- width: 200,
- height: 40,
- lineHeight: 40,
- backgroundColor: '#ff0000',
- color: '#ffffff',
- textAlign: 'center',
- fontSize: 16,
- borderRadius: 4
- }
- })
- button.onTap((res) => {
- // 用户同意授权后回调,通过回调可获取用户头像昵称信息
- console.log(res)
- })
- }
- },
- fail (err){
- console.log("获取用户信息失败",err)
- }
- })
- wx.authorize({
- scope: 'scope.userFuzzyLocation',
- success (res) {
- console.log("获取定位权限成功",res)
- },
- fail (err){
- console.log("获取定位权限失败",err)
- }
- })
-
- wx.authorize({
- scope: 'scope.bluetooth',
- success (res) {
- console.log("获取蓝牙权限成功",res)
- },
- fail (err){
- console.log("获取蓝牙权限失败",err)
- }
- })
- // 首先
- wx.stopBluetoothDevicesDiscovery()
- // 接着
- wx.closeBluetoothAdapter()
- // 然后
- wx.openBluetoothAdapter()
- wx.createBLEConnection({
- deviceId : "A4:D5:78:13:05:A9",
- success (res) {
- console.log("连接成功", res)
- // 连接成功后获取服务
- },
- fail (err) {
- console.log("连接失败", err)
- }
- })
- const serviceId = '0000FFE0-0000-1000-8000-00805F9B34FB'
- const characteristicId = '0000FFE1-0000-1000-8000-00805F9B34FB'
- wx.notifyBLECharacteristicValueChange({
- deviceId: "A4:D5:78:13:05:A9",
- serviceId: serviceId,
- characteristicId: characteristicId,
- state: true, // 启用 notify 功能
- success (res) {
- console.log('notify success');
- }
- });
- function onCharacteristicValueChange(res) {
- // 创建一个Uint8Array视图,它引用res.value中的二进制数据
- // let uint8Array = new Uint8Array(res.value);
- // 8int
- // 数据是ASCII码范围是0-127
- let int8Array = new Int8Array(res.value);
- let str = '';
- for(let i = 0; i < int8Array.length; i++) {
- str += String.fromCharCode(int8Array[i]);
- }
- str = str.replace(/(\r\n|\n|\r|\[|\])/gm, "");
- if (str[0] === "K"){
- let kArray = str.substring(1).split(",");
- console.log(kArray);
- }
- // if (str[0] === "A") {
- // let aArray = str.substring(1).split(",");
- // for (let i = 0; i < aArray.length; i++){
- // aArray[i] = parseInt(aArray[i], 16);
- // //aArray[i] = 65535 - aArray[i];
- // }
- // console.log(aArray);
- // //console.log(str);
- // }
- // if (str[0] === "G") {
- // let gArray = str.substring(1).split(",");
- // for (let i = 0; i < gArray.length; i++){
- // gArray[i] = parseInt(gArray[i], 16);
- // gArray[i] = 65535 - gArray[i];
- // }
- // console.log(gArray);
- // }
- }
解析数据的这个函数,其实就是一个回调函数,用来处理(解析)蓝牙发送过来的数据。onCharacteristicValueChange函数作为参数传递给了wx.onBLECharacteristicValueChange;只要程序订阅了蓝牙的通知,传感器的数值发生了变化,蓝牙就会立刻给程序发送通知,程序就会立刻调用这个回调函数onCharacteristicValueChange解析蓝牙发送过来的数据。这个过程也叫事件监听。。。。。。
就是,你订阅了蓝牙某个服务某个特征的通知,又注册了监听事件(函数),就可以监听对应数据的变化。
- // 订阅通知后 注册监听事件, 订阅之后来个监听事件来监听
- wx.onBLECharacteristicValueChange(onCharacteristicValueChange);
-
- // 在不需要的时候停止监听并且取消订阅
- wx.offBLECharacteristicValueChange(onCharacteristicValueChange);
- wx.notifyBLECharacteristicValueChange({
- deviceId: "A4:D5:78:13:05:A9",
- serviceId: serviceId,
- characteristicId: characteristicId,
- state: false, // 关闭 notify 功能
- success (res) {
- console.log('取消特征值通知成功');
- },
- fail (error) {
- console.error('取消特征值通知失败', error);
- }
- });
-
- // 在不需要的时候断开蓝牙连接、关闭蓝牙适配器(收拾手尾)
- wx.closeBLEConnection({
- deviceId: "A4:D5:78:13:05:A9",
- success (res) {
- console.log('断开蓝牙设备成功', res);
- },
- fail (error) {
- console.log('断开蓝牙设备失败', error);
- }
- })
-
- wx.closeBluetoothAdapter()
(估计要蓝牙2.0甚至4.0版本的低功耗蓝牙才能被搜索到,估计比手机本身能识别的设备要少,就是,有些蓝牙手机能够搜索到,但是微信小程序是收不到的)
- wx.stopBluetoothDevicesDiscovery()
-
- wx.closeBluetoothAdapter()
-
- wx.openBluetoothAdapter()
-
- var DeviceIndex = 0;
- wx.startBluetoothDevicesDiscovery({
- allowDuplicatesKey: false,
- success: (res) => {
- console.log('开始搜索附近蓝牙设备', res);
- function ab2hex(buffer) {
- const hexArr = Array.prototype.map.call(
- new Uint8Array(buffer),
- function(bit) {
- return ('00' + bit.toString(16).slice(-2));
- }
- );
- return hexArr.join('');
- }
- function onDeviceFound(deviceRes) {
- deviceRes.devices.forEach((device) => {
- if (device.localName || device.name) {
- console.log('设备名称:', device.localName || device.name);
- console.log('广播数据转为十六进制字符串', ab2hex(device.advertisData));
- console.log('新设备列表', DeviceIndex, deviceRes.devices);
- DeviceIndex++;
- }
- });
- }
- wx.onBluetoothDeviceFound(onDeviceFound)
- },
- fail: (err) => {
- console.error('蓝牙设备搜索失败', err);
- wx.offBluetoothDeviceFound(onDeviceFound);
- console.log('停止监听新蓝牙设备');
- wx.stopBluetoothDevicesDiscovery({
- success: (res) => {
- console.log('停止蓝牙搜索', res);
- },
- fail: (err) => {
- console.error('停止蓝牙搜索失败', err);
- }
- });
- }
- });
- wx.offBluetoothDeviceFound(onDeviceFound);
- console.log('停止监听新蓝牙设备');
- wx.stopBluetoothDevicesDiscovery({
- success: (res) => {
- console.log('停止蓝牙搜索', res);
- },
- fail: (err) => {
- console.error('停止蓝牙搜索失败', err);
- }
- });
- wx.getBluetoothDevices({
- success: function(res) {
- console.log('All discovered devices', res.devices);
- },
- fail (err) {
- console.log(err)
- }
- })
- wx.startBluetoothDevicesDiscovery({
- allowDuplicatesKey: false,
- success: (res) => {
- console.log('开始搜索附近蓝牙设备', res);
- function ab2hex(buffer) {
- const hexArr = Array.prototype.map.call(
- new Uint8Array(buffer),
- function(bit) {
- return ('00' + bit.toString(16).slice(-2));
- }
- );
- return hexArr.join('');
- }
- function onDeviceFound(deviceRes) {
- deviceRes.devices.forEach((device) => {
- if (device.deviceId === "A4:D5:78:13:05:A9") {
- console.log('设备名称:', device.localName || device.name);
- console.log('广播数据转为十六进制字符串', ab2hex(device.advertisData));
- console.log('新设备列表', deviceRes.devices);
- wx.stopBluetoothDevicesDiscovery({
- success: (res) => {
- console.log('停止蓝牙搜索成功', res);
- },
- fail: (err) => {
- console.error('停止蓝牙搜索失败', err);
- }
- });
- wx.createBLEConnection({
- deviceId : "A4:D5:78:13:05:A9",
- success (res) {
- console.log("连接成功", res)
- // 连接成功后获取服务
- wx.getBLEDeviceServices({
- deviceId: 'A4:D5:78:13:05:A9',
- success (res) {
- console.log('设备服务列表', res.services);
- console.log('设备服务列表', res);
- },
- fail (err) {
- console.log("获取服务列表失败", err)
- }
- });
- },
- fail (err) {
- console.log("连接失败", err)
- }
- })
- wx.onBLEConnectionStateChange(function (res) {
- // res.connected 表示该设备是否处于连接状态
- console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
- })
- }
- });
- }
- wx.onBluetoothDeviceFound(onDeviceFound)
- },
- fail: (err) => {
- console.error('蓝牙设备搜索失败', err);
- wx.offBluetoothDeviceFound(onDeviceFound);
- console.log('停止监听新蓝牙设备');
- wx.stopBluetoothDevicesDiscovery({
- success: (res) => {
- console.log('停止蓝牙搜索成功', res);
- },
- fail: (err) => {
- console.error('停止蓝牙搜索失败', err);
- }
- });
- }
- });
- wx.getBLEDeviceServices({
- deviceId: 'A4:D5:78:13:05:A9',
- success (res) {
- console.log('设备服务列表', res.services);
- console.log('设备服务列表', res);
- },
- fail (err) {
- console.log("获取服务列表失败", err)
- }
- });
- const deviceId = 'A4:D5:78:13:05:A9';
- // 假设 serviceUuids 是您从 wx.getBLEDeviceServices 获取的服务UUID列表
- const serviceUuids = ['00001800-0000-1000-8000-00805F9B34FB', '00001801-0000-1000-8000-00805F9B34FB', '0000FFE0-0000-1000-8000-00805F9B34FB'];
- serviceUuids.forEach(serviceUuid => {
- wx.getBLEDeviceCharacteristics({
- deviceId: deviceId,
- serviceId: serviceUuid,
- success: function(res) {
- console.log(`服务 ${serviceUuid} 的特征列表:`, res.characteristics);
- // 根据特征属性进行后续操作,比如读取、写入、通知等
- },
- fail: function(err) {
- console.error(`获取服务 ${serviceUuid} 的特征失败:`, err);
- }
- });
- });
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。