Skip to content

particle-iot/AndroidBLEExample

Repository files navigation

Android BLE Requests example

Getting started

  1. Clone this repo
  2. Flash the app from firmware directory to an Argon or P2 against at least Device OS 3.3.0 (previous versions didn't include the provisioning mode).
  3. Scan the QR code from the device. It will contain two lines of text
    • first line contains the serial number. Take the last six characters and update the setupCode constant
    • second line contains the mobile secret. Take it and update the mobileSecret constant
  4. Make sure you have Android Studio installed and configured
  5. Make sure your Android device is set up for development. The app requires at least Android 10 (API version 30)
  6. Open this repo in Android Studio and run the app on the device

Note: this app uses custom BLE UUIDs meaning it won't work with stock Listening Mode without flashing the firmware above. You can update this app to use the default UUIDs but it's recommended to use unique UUIDs for your products to avoid picking up Particle dev kits when setting up your devices.

Development

App logic flow

The app goes through following steps:

  1. Sets up and ensures all necessary BLE permissions
  2. Looks for devices with name or manufacturerData ending with hardcoded setupCode
  3. Connects to the first device found
  4. Looks for exposed service with hardcoded UUID
  5. Lists its characteristics and ensures that it contains all the necessary ones
    1. Checks if version characteristic has matching UUID AND is readable
    2. Checks if read characteristic has matching UUID AND is notifying
    3. Checks if write characteristic has matching UUID AND is writable
  6. Checks if the protocol version equals 2
  7. Send echo request
  8. Send scan wifi networks request

Generated protobuf module

The firmwareprotos directory contains generated Java definitions for the device-os-protobuf declarations. It allows easy encoding/decoding of the request/reply messages.

Encoding

import io.particle.firmwareprotos.ctrl.wifi.WifiNew;

void scanWiFiNetworks() {
    WifiNew.ScanNetworksRequest request = WifiNew.ScanNetworksRequest.newBuilder().build();
    sendRequest(request.toByteArray());
}

Decoding

import io.particle.firmwareprotos.ctrl.wifi.WifiNew;

void receivedScanWiFiNetworksReply(byte[] replyData) {
    WifiNew.ScanNetworksReply reply = WifiNew.ScanNetworksReply.parseFrom(replyData);

    log("Found " + reply.getNetworksCount() + " networks");
    for (WifiNew.ScanNetworksReply.Network network: reply.getNetworksList()) {
        log("  " + network.getSsid() + " [" + network.getBssid() + "] " + network.getRssi() + "dB");
    }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published