Flutter教程

BLE

Contents

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

联系我们?

If you're building a serious project, you can't afford to spend hours troubleshooting. Ionic's experts offer official maintenance, support, and integration help.

Contact Us Today!

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.