Adaptation guide for third-party Bluetooth communication plugins

更新时间:
复制 MD 格式

Bluetooth application-layer protocols vary widely. To integrate different protocols into a unified framework, an adaptation layer is required to handle these differences. For example, Alibaba Cloud IoT defines an application-layer communication protocol named Breeze.

Background information

Most Bluetooth application-layer protocols define the following features.

  • Bluetooth broadcast: Used for broadcasting device information, such as manufacturer information, device model, and MAC address.
  • Data communication: Includes features such as data segmentation and packet assembly.
For this reason, Alibaba Cloud IoT has integrated a Bluetooth communication plugin management system, called the Link Protocol Bridge System (LPBS), into its Bluetooth access framework. 蓝牙原理图

LPBS defines a set of abstract interfaces to connect devices that use different Bluetooth protocols to a unified framework. This enables device control and monitoring based on the Alibaba Cloud IoT Thing Specification Language model. Each specific implementation is called a plugin.

Procedure

  1. Obtain identity information.
    A smart device must have an identity on the Alibaba Cloud IoT Platform. This identity links the device to a user account. It also enables data to be sent between the device and the cloud and allows for device control. On the IoT Platform, a device identity is determined by two items:
    • A `ProductKey` is a random string that represents the product model. The IoT Platform issues a `ProductKey` when you register a product.
    • A `DeviceName` is a device ID specific to the IoT Platform. Together with the `ProductKey`, it uniquely identifies a device on the platform. The `DeviceName` can be a random string generated by the IoT Platform or a string that you specify when you add the device to the platform.

    When a smart device is manufactured, it is issued a globally unique ID. This ID usually consists of two parts.

    • `ProductModel`: The product model, such as BCD-320WGPZM for a refrigerator.
    • `DeviceId`: The device ID, which is usually a MAC address or serial number (SN).

    When you connect a smart device to the IoT Platform, you must map its private ID to the ID that is issued by the platform.

    1. Create a product.

      Add a new product on the IoT Platform. When you register the product, the IoT Platform issues a `ProductKey` to identify it.

    2. Enter device IDs.

      Add devices for the product in batches by entering their `DeviceId`s. The IoT Platform issues device identities for the batch and uses the `DeviceId`s that you entered as the `DeviceName`s.

  2. Create a data format transformation script.
    Because third-party Bluetooth devices do not use the Thing Specification Language model to define their features, you must transform data into a format that the device can understand. For example, in a scenario where you turn on a light, the message that is encapsulated based on the Thing Specification Language model is shown below.
    {
    "id":123,
    "version":"1.0.0",
    "method":"thing.service.property.set",
    "params":{
    "LightSwitch":1
    },
    }
  3. Develop a plugin for the Bluetooth communication plugin management system.
    The communication plugin abstract interface has three parts:
    • Device discovery interface

      You must convert the `ProductModel` from the broadcast into the `ProductKey` that you obtained during product registration. You can use the MAC address directly as the `DeviceName`.

      When purchasing activation codes in the Mass Production Management section of the IoT Platform console, select the batch upload method to generate the codes by uploading the MAC addresses to use as `DeviceName`s.

    • Device connection interface

      This interface must establish a connection between the mobile phone (Bluetooth Central) and the Bluetooth device (Bluetooth Peripheral).

    • Data sending/receiving interface

      This interface sends business data to the Bluetooth device and returns the device's response to the upper-layer Bluetooth access framework. You must also implement the event subscription feature. This lets you subscribe to device events so that the app can receive notifications when the events occur.

  4. Bind and control the Bluetooth device.
    After the plugin is implemented, the subsequent process for a third-party Bluetooth device is the same as for a standard Alibaba Cloud IoT Bluetooth device. For more information about binding and controlling the device, see the mobile software development kit (SDK) guide.

Examples

The following are related code examples.