Coder Social home page Coder Social logo

wx-ant-ble's Introduction

wx-ant-ble

npm version

微信、支付宝小程序BLE蓝牙SDK

Release Notes

v1.1.0

2019/01/15
1、发布第一个可用版本。封装蓝牙接口,兼容微信和支付宝小程序。附有说明和demo。

v1.1.1

2019/08/15
1、修复微信小程序安卓手机断开连接,状态更新不回调的bug。
2、已知另一个微信小程序蓝牙bug。安卓手机在设备异常断开时(比如断电),不会触发onBLEConnectionStateChange回调。除非在已经设置监听notify的情况下。

Features

  • 兼容微信和支付宝小程序
  • 简洁但功能完整的Api,可以根据需求自由调用
  • 接口均有返回状态,判断是否调用成功
  • 单例模式
  • 完整例子

Directory

  • ~/index.js SDK入口
  • ~/src SDK源码
  • ~/example 微信和支付宝小程序demo

Installation

  • npm
  npm install wx-ant-ble

  // 微信小程序请查看npm文档,支付宝小程序仅下载
  • 直接下载或者使用git克隆
  git clone https://github.com/zhaodahai/wx-ant-ble.git

Usage

1、引入SDK管理类和枚举

  // 引入SDK管理类 微信小程序通过npm构建后
  import { BTManager, ConnectStatus } from 'wx-ant-ble';
  // 引入SDK管理类 支付宝小程序
  import { BTManager, ConnectStatus } from '../../wx-ant-ble/index.js';

2、初始化蓝牙管理器 & 设置用户信息

  // 初始化蓝牙管理器 单例模式
  this.bt = new BTManager({
    debug: true // 是否开启打印调试
  });

3、注册回调

  // 注册状态更新回调
  this.bt.registerDidUpdateConnectStatus(res => {
    // 参数结构注意查看log
    console.log('registerDidUpdateConnectStatus', res);
    if (res.connectStatus === ConnectStatus.connected) {
      this.setData({ isConnected: true });
      ...
    }else if (res.connectStatus === ConnectStatus.disconnected) {
      this.setData({ isConnected: false });
      ...
    }
  });
  // 注册发现外设回调,当扫描到设备时回调,或者达到超时时间回调。
  this.bt.registerDidDiscoverDevice(res => {
    // 参数结构注意查看log
    console.log('registerDidDiscoverDevice', res);
    ...
  });
  // 注册特征值改变回调,当监听的特征值改变时回调,或者读特征值时回调。
  this.bt.registerDidUpdateValueForCharacteristic(res => {
    // 参数结构注意查看log
    console.log('registerDidUpdateValueForCharacteristic', res);
    ...
  });

4、扫描 & 停止扫描

  // 开始扫描
  this.bt.scan({
    services: [],              // 主service的uuid列表
    allowDuplicatesKey: false, // 是否允许重复上报设备
    interval: 0,               // 上报新设备的间隔,默认为0
    timeout: 15000,            // 扫描超时时间,毫秒
    deviceName: '',            // 需要匹配的设备名称
    containName: ''            // 需要包含的设备名称
  }).then(res => {
    console.log('scan success', res);
  }).catch(e => {
    console.log('scan fail', e);
  });
  // 停止扫描
  this.bt.stopScan()
  .then(res => {
    console.log('stopScan success', res);
  }).catch(e => {
    console.log('stopScan fail', e);
  })

5、连接 & 断开连接

  // 连接设备
  this.bt.connect(device) // device应为registerDidDiscoverDevice注册的方法上报的设备对象
  .then(res => {
    console.log('connect success', res);
  }).catch(e => {
    console.log('connect fail', e);
  });
  // 断开连接
  this.bt.disconnect()
  .then(res => {
    console.log('disconnect success', res);
  }).catch(e => {
    console.log('disconnect fail', e);
  })

5、Read & Notify & Write

  // 读特征值,读到的值从registerDidUpdateValueForCharacteristic注册的方法上报
  this.bt.read({
    suuid: 'xxxx', // 特征对应的服务uuid
    cuuid: 'xxxx'  // 特征uuid
  }).then(res => {
    console.log('read success', res);
  }).catch(e => {
    console.log('read fail', e);
  })
  // 向蓝牙模块写入数据
  this.bt.write({
    suuid: 'xxxx', // 特征对应的服务uuid
    cuuid: 'xxxx', // 特征uuid
    value: 'FFFF'  // 写入的数据
  }).then(res => {
    console.log('write success', res);
  }).catch(e => {
    console.log('write fail', e);
  })
  // 监听/停止监听 特征值改变,改变特征值从registerDidUpdateValueForCharacteristic注册的方法上报
  this.bt.notify({
    suuid: 'xxxx',     // 特征对应的服务uuid
    cuuid: 'xxxx',     // 特征uuid
    state: true/false  // 是否监听
  }).then(res => {
    console.log('notify success', res);
  }).catch(e => {
    console.log('notify fail', e);
  })

WxApp

仓库地址

BLE蓝牙开发助手二维码

BLE蓝牙开发助手演示

Notice

wx-ant-ble's People

Contributors

hexiaoyanh avatar zhaodahai avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.