微信小程序初始化蓝牙模块

上一篇 微信小程序蓝牙调用公共方法Bluetooth.js 

 

本篇将介绍微信小程序初始化蓝牙模块的相关代码

一、首先页面要引用 蓝牙调用公共方法 Bluetooth.js

 index.js 文件顶部加上如下代码:

import Bluetooth from '../../utils/Bluetooth.js'

二、data 里面定义两个参数:isBluetoothAdapterInit : false, //是否初始化蓝牙模块

isBluetoothAdapterState: false, //手机蓝牙状态 是否开启

三、onload 中自定义蓝牙状态改变事件代码

    let that = this;
    //蓝牙状态改变回调事件 
    Bluetooth.onBluetoothAdapterStateChangeCallback = function(res){
      console.log('手机蓝牙状态', res)
      that.setData({
        isBluetoothAdapterState:res.available
      });
    };

四、onShow 中实现初始化蓝牙模块

    let that=this;
    //初始化蓝牙
    Bluetooth.openBluetoothAdapter().then(
      function(res) {
        if(res)
        {
          that.setData({
            isBluetoothAdapterInit:true,
            isBluetoothAdapterState:true
          });
          Bluetooth.CheckSystemInfo().then(res=>{
            if(res)
            {

            }
          });
        }
        //监听蓝牙状态改变事件
        Bluetooth.onBluetoothAdapterStateChange();
      },
      function(err) {
        console.log(err);
        //当前蓝牙适配器不可用
        if(err.errCode=='10001')
        {
          that.setData({
            isBluetoothAdapterInit:true,
            isBluetoothAdapterState:false
          });
          //监听蓝牙状态改变事件
          Bluetooth.onBluetoothAdapterStateChange();
          Bluetooth.CheckSystemInfo().then(res=>{
            if(res)
            {
            }
          });
        }
        else
        {
          that.setData({
            isBluetoothAdapterInit:false,
            isBluetoothAdapterState:false
          });
        }
      }
    );

五、页面及样式代码

<!--index.wxml-->
<navigation-bar title="蓝牙连接" back="{{false}}" color="black" background="#FFF"></navigation-bar>
<scroll-view type="list" class="scroll_view" scroll-y="true">
  <view class="content" list-item>
    <text class="status">蓝牙适配器状态:{{ isBluetoothAdapterState?'可用':'不可用' }}</text>
  </view>
</scroll-view>
/**index.wxss**/
page {
  color: #333;
  background-color: #f7f7f7;
  display: flex;
  flex-direction: column;
}
.scroll_view {
  display: flex; 
  flex-direction: column; 
  flex: 1; 
  width: 100%; 
  overflow: auto;
} 
.content{
  display: flex;
  flex-flow: nowrap;
  justify-content: center;
  padding-top: 10rpx;
  padding-bottom: 10rpx;
}

注意:小程序页面初次打开会有 小程序使用蓝牙的弹窗,一般都选择允许,如果选择拒绝的话,我可以找到小程序右上方三个点——设置——蓝牙(选择使用小程序时允许)。我的程序可以不用设置,在onshow里面有判断会有弹窗提示直接去开启小程序的蓝牙使用权限。

下一篇 将继续介绍 微信小程序搜索蓝牙设备

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Android微信小程序中读写蓝牙BLE设备,您需要使用微信小程序提供的wx.createBLEConnection API连接设备,然后使用wx.writeBLECharacteristicValue和wx.readBLECharacteristicValue API来读写设备。 以下是实现步骤: 1. 初始化蓝牙模块 ``` wx.openBluetoothAdapter({ success: function(res) { console.log("初始化蓝牙模块成功") }, fail: function(res) { console.log("初始化蓝牙模块失败") } }) ``` 2. 扫描设备 ``` wx.startBluetoothDevicesDiscovery({ success: function(res) { console.log("蓝牙设备扫描成功") }, fail: function(res) { console.log("蓝牙设备扫描失败") } }) ``` 3. 连接设备 ``` wx.createBLEConnection({ deviceId: deviceId, success: function(res) { console.log("连接设备成功") }, fail: function(res) { console.log("连接设备失败") } }) ``` 4. 读取设备特征值 ``` wx.readBLECharacteristicValue({ deviceId: deviceId, serviceId: serviceId, characteristicId: characteristicId, success: function(res) { console.log("读取设备特征值成功") }, fail: function(res) { console.log("读取设备特征值失败") } }) ``` 5. 写入设备特征值 ``` wx.writeBLECharacteristicValue({ deviceId: deviceId, serviceId: serviceId, characteristicId: characteristicId, value: value, success: function(res) { console.log("写入设备特征值成功") }, fail: function(res) { console.log("写入设备特征值失败") } }) ``` 请注意,要使用以上API,您需要在小程序的app.json中声明蓝牙权限: ``` "permission": { "scope.userLocation": { "desc": "你的位置信息将用于小程序位置接口的效果展示" }, "scope.bluetooth": { "desc": "小程序需要使用蓝牙功能" } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wsk198726

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值