scratchfoundation / scratch-link

Device interoperability layer for Windows and MacOS
BSD 3-Clause "New" or "Revised" License
104 stars 84 forks source link

Feature/advertisementdata #120

Closed knandersen closed 5 years ago

knandersen commented 5 years ago

Resolves

112 - Discover devices based on BLE Advertisement Data

Proposed Changes

This pull request allows Scratch Link's BLE Session to accept a filter based on BLE Advertisement Manufacturer Data. This is an optional filter to distinguish different devices that share the same BLE Service and Characteristic UUID.

It assumes the manufacturer data is structured as proposed (although at the time of writing not established) by the Web Bluetooth standard: https://webbluetoothcg.github.io/web-bluetooth/#example-filter-by-manufacturer-service-data

An example of the filter provided by the client (in this case Scratch VM using a LEGO Boost extension):

{
  filters: [{
    services: [BoostBLE.service],
      manufacturerData: {
        0: {
          dataPrefix: [0x97, 0x03, 0x00, 0x40],
          mask: [0xFF, 0xFF, 0, 0xFF]
        }
      }
    }],
  optionalServices: []
}

The dataPrefix and mask will be bitwise AND'ed, and any device discovered by Scratch Link will have it's manufacturer data prefix AND'ed with the supplied mask. The two AND'ed results will be compared, and if they match, Scratch Link will include the device in discovered devices.

dataPrefix and mask must be the same length, and max 32 bytes.

Reason for Changes

LEGO products featuring BLE and the LEGO Wireless Protocol 3 and later (https://lego.github.io/lego-ble-wireless-protocol-docs/) will only feature a single BLE service and characteristic with the same UUID shared across multiple products. This means that individual "product" extensions will not be able to scan for individual devices based on UUID alone, but will need to distinguish devices based on the manufacturer data provided in the device's advertisement data.

knandersen commented 5 years ago

We will revisit this in the future. Closing for now.