prepare

protected abstract fun prepare(peripheral: Peripheral<*, *>, services: List<RemoteService>)

Validates and prepares the services to be used in initialize.

This method should validate if the services contain the required characteristics, validate if the characteristics have expected properties, and store the references to the characteristics for later use.

This method is called before initialize. It should use kotlin.require and kotlin.collections.List.first to validate the service and throw IllegalArgumentException or NoSuchElementException if validation fails. In that case, the outcome is downgraded to unsupported - exactly as if the services had not been found in the first place - and a required profile will still cause a disconnection with RequiredServiceNotFound.

Example

See Profile for a full worked example (ProximityProfile); the relevant part of its prepare is:

override fun prepare(peripheral: Peripheral<*, *>, services: List<RemoteService>) {
// Link Loss Service is required.
val linkLossService = services.first { it.uuid == LINK_LOSS_SERVICE_UUID }
linkLossAlertLevel = linkLossService.characteristics.first { it.uuid == ALERT_LEVEL_UUID }
require(linkLossAlertLevel.isWritable()) { "Alert Level characteristic must be writable." }

// Immediate Alert Service is optional - only present if the peripheral supports it.
immediateAlertLevel = services.firstOrNull { it.uuid == IMMEDIATE_ALERT_SERVICE_UUID }
?.characteristics?.first { it.uuid == ALERT_LEVEL_UUID }
}

Parameters

peripheral

The peripheral the profile is being resolved on.

services

The discovered services.

See also