Pairing Device via BLE

Pre-requisite: Ensure the app has all necessary runtime permissions to access Bluetooth services.

  • For Android 10 and below: ACCESS_FINE_LOCATION permission is required.
  • For Android 12 and above: BLUETOOTH_SCAN and BLUETOOTH_CONNECT permissions are required.
  • Bluetooth must be enabled before starting BLE operations.

Start Scanning for BLE Devices

To begin scanning for nearby BLE devices:

InstaVision.bleService.startScan()

Optional: Customize BLE Configuration You can configure BLE scan and connection behavior using BleConfig.

val config = BleScanConfig(
  scanTimeout = 90_000L,           // Scan timeout in ms
  connectionTimeout = 30_000L,     // Connection timeout in ms
)
InstaVision.bleService.startScan(bleConfig = config)

Observe Scan Results

You can observe the scan results using a Flow:

InstaVision.bleService.scanResults.collectLatest { deviceList ->
  // Filter or display device info
}

Stop BLE Scanning

BLE scan will automatically stop after the configured timeout (BleScanConfig.scanTimeout), defaulting to 90 seconds.

To manually stop scanning before timeout:

InstaVision.bleService.stopScan()

Best Practice: Always call stopScan() before initiating a connection.


Connect to BLE Device

Once you have selected a BluetoothDevice from scan results:

InstaVision.bleService.connectToDevice(device)

You will receive the onConnected() callback if the connection is successful.


Start Service Discovery

After connecting, initiate service discovery:

InstaVision.bleService.startServiceDiscovery()

This will trigger the BLE device to discover services and characteristics. Once completed, you’ll receive:

BleCallback.onDeviceReady()

Check Available Wifi (Optional)

After the device is connected and services are discovered, you can send a command to initiate a WiFi scan:

InstaVision.bleService.sendWifiScanCommand()

This triggers the BLE device to start scanning for available WiFi networks.

You can collect the list of available networks using:

InstaVision.bleService.wifiNetworks.collectLatest { networks ->
  networks.forEach { network ->
    Log.d("BLE", "SSID: ${network.ssid}")
  }
}

The list updates as new networks are discovered by the BLE device.


Send WiFi Configuration

To update WiFi credentials the device needs to connect to, use:

InstaVision.bleService.sendWifiConfig(
  ssid = "YourNetwork",
  password = "YourPassword",
  sessionKey = "session_key",
  region = ServerRegions.US,
  env = Environment.PRODUCTION
)

Upon success, the callback onWifiConfigSent() will be invoked.


Release BLE Resources

After the pairing process is complete or when restarting a new pairing session, release all BLE resources:

InstaVision.bleService.release()

This ensures clean disconnection, cancels any timers, clears callbacks, and closes GATT connections.

Registering BLE Callback

To receive BLE state updates and responses, register your callback:

val bleCallback = object: BleCallback() {
  override fun onConnected() {
    Log.d("BLE", "Connected to BLE device")
  }

  override fun onDeviceReady() {
    Log.d("BLE", "Device is ready")
  }

  override fun onWifiConfigSent() {
    Log.d("BLE", "WiFi config successfully sent")
  }

  override fun onDeviceConnectionFailed() {
    Log.e("BLE", "Connection failed")
  }

  override fun onScanStopped() {
    Log.e("BLE", "Ble scanning stopped")
  }
}

InstaVision.bleService.registerCallback(bleCallback)