返回介绍

Ionic4 蓝牙插件-蓝牙插件BLE

发布于 2019-11-23 07:40:57 字数 6237 浏览 1617 评论 0 收藏 0

This plugin enables communication between a phone and Bluetooth Low Energy (BLE) peripherals.

The plugin provides a simple JavaScript API for iOS and Android.

  • Scan for peripherals
  • Connect to a peripheral
  • Read the value of a characteristic
  • Write new value to a characteristic
  • Get notified when characteristic's value changes

Advertising information is returned when scanning for peripherals. Service, characteristic, and property info is returned when connecting to a peripheral. All access is via service and characteristic UUIDs. The plugin manages handles internally.

Simultaneous connections to multiple peripherals are supported.

https://github.com/don/cordova-plugin-ble-central

Ionic 蓝牙插件BLE插件的安装(Installation)

ionic cordova plugin add cordova-plugin-ble-central npm install @ionic-native/ble 
Ionic EE comes with fully supported and maintained plugins from the Ionic Team. Learn More or Contact Us
ionic enterprise register --key=YOURPRODUCTKEY npm install @ionic-enterprise/ble 

Ionic 蓝牙插件BLE插件支持的平台(Supported Platforms)

  • Android
  • iOS

Ionic 蓝牙插件BLE插件的用法(Usage)


import { BLE } from '@ionic-native/ble/ngx';

constructor(private ble: BLE) { }

Peripheral Data

Peripheral Data is passed to the success callback when scanning and connecting. Limited data is passed when scanning.

  {
      'name': 'Battery Demo',
      'id': '20:FF:D0:FF:D1:C0',
      'advertising': [2,1,6,3,3,15,24,8,9,66,97,116,116,101,114,121],
      'rssi': -55
  }

After connecting, the peripheral object also includes service, characteristic and descriptor information.

  {
      'name': 'Battery Demo',
      'id': '20:FF:D0:FF:D1:C0',
      'advertising': [2,1,6,3,3,15,24,8,9,66,97,116,116,101,114,121],
      'rssi': -55,
      'services': [
          '1800',
          '1801',
          '180f'
      ],
      'characteristics': [
          {
              'service': '1800',
              'characteristic': '2a00',
              'properties': [
                  'Read'
              ]
          },
          {
              'service': '1800',
              'characteristic': '2a01',
              'properties': [
                  'Read'
              ]
          },
          {
              'service': '1801',
              'characteristic': '2a05',
              'properties': [
                  'Read'
              ]
          },
          {
              'service': '180f',
              'characteristic': '2a19',
              'properties': [
                  'Read'
              ],
              'descriptors': [
                  {
                      'uuid': '2901'
                  },
                  {
                      'uuid': '2904'
                  }
              ]
          }
      ]
  }

Advertising Data

Bluetooth advertising data is returned in when scanning for devices. The format varies depending on your platform. On Android advertising data will be the raw advertising bytes. iOS does not allow access to raw advertising data, so a dictionary of data is returned.

The advertising information for both Android and iOS appears to be a combination of advertising data and scan response data.

Android

  {
      'name': 'demo',
      'id': '00:1A:7D:DA:71:13',
      'advertising': ArrayBuffer,
     'rssi': -37
 }

Convert the advertising info to a Uint8Array for processing. var adData = new Uint8Array(peripheral.advertising)

iOS

Note that iOS uses the string value of the constants for the Advertisement Data Retrieval Keys. This will likely change in the future.

  {
      'name': 'demo',
      'id': 'D8479A4F-7517-BCD3-91B5-3302B2F81802',
      'advertising': {
          'kCBAdvDataChannel': 37,
          'kCBAdvDataServiceData': {
              'FED8': {
                  'byteLength': 7 // data not shown
              }
          },
          'kCBAdvDataLocalName': 'demo',
          'kCBAdvDataServiceUUIDs': ['FED8'],
          'kCBAdvDataManufacturerData': {
              'byteLength': 7  // data not shown
          },
          'kCBAdvDataTxPowerLevel': 32,
          'kCBAdvDataIsConnectable': true
      },
      'rssi': -53
  }

Typed Arrays

This plugin uses typed Arrays or ArrayBuffers for sending and receiving data.

This means that you need convert your data to ArrayBuffers before sending and from ArrayBuffers when receiving.

  // ASCII only
  function stringToBytes(string) {
     var array = new Uint8Array(string.length);
     for (var i = 0, l = string.length; i < l; i++) {
         array[i] = string.charCodeAt(i);
      }
      return array.buffer;
  }

  // ASCII only
  function bytesToString(buffer) {
      return String.fromCharCode.apply(null, new Uint8Array(buffer));
  }

You can read more about typed arrays in these articles on MDN and HTML5 Rocks.

UUIDs

UUIDs are always strings and not numbers. Some 16-bit UUIDs, such as '2220' look like integers, but they're not. (The integer 2220 is 0x8AC in hex.) This isn't a problem with 128 bit UUIDs since they look like strings 82b9e6e1-593a-456f-be9b-9215160ebcac. All 16-bit UUIDs should also be passed to methods as strings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文