Supporto di sensori aggiuntivi

I dispositivi per il fitness che implementano un profilo Bluetooth Low Energy GATT standard sono supportati automaticamente da Google Fit. Se il tuo dispositivo non implementa uno di questi profili, puoi creare un'app Android che gestisca la comunicazione con il dispositivo per l'attività fisica e lo mostri a Google Fit come sensore software. Puoi anche esporre i sensori software personalizzati nella tua app.

Per creare un sensore software nella tua app, estendi la classe FitnessSensorService e dichiarala come servizio nel file manifest. Quando gli utenti installano la tua app, Google Fit rende disponibili i sensori del software ad altre app. Quando un'app si registra per ricevere dati da un sensore software nell'app, Google Fit si vincola al tuo servizio.

Dichiarare un servizio di sensori

Per definire un sensore software, dichiara un FitnessSensorService nel file manifest della tua app:

<service android:name="com.example.MySensorService"
         android:process=":sensor">
  <intent-filter>
    <action android:name="com.google.android.gms.fitness.service.FitnessSensorService" />
    <!-- include at least one mimeType filter for the supported data types -->
    <data android:mimeType="vnd.google.fitness.data_type/com.google.heart_rate.bpm" />
  </intent-filter>
</service>

Il servizio in questo esempio viene eseguito in un processo separato, come specificato dall'attributo android:process. Per ulteriori informazioni, consulta la sezione Processi.

Implementare un servizio di sensori

Per implementare un sensore software, estendi la classe FitnessSensorService e implementa i metodi astratti. I dettagli dell'implementazione dipendono dal tuo caso d'uso particolare, ma il seguente esempio fornisce linee guida generali:

Kotlin

class MySensorService : FitnessSensorService() {
    override fun onCreate() {
        super.onCreate()
        // 1. Initialize your software sensor(s).
        // 2. Create DataSource representations of your software sensor(s).
        // 3. Initialize some data structure to keep track of a registration
        // for each sensor.
    }

    override fun onFindDataSources(dataTypes: List<DataType>): List<DataSource> {
        // 1. Find which of your software sensors provide the data types requested.
        // 2. Return those as a list of DataSource objects.
    }

    override fun onRegister(request: FitnessSensorServiceRequest): Boolean {
        // 1. Determine which sensor to register with request.dataSource.
        // 2. If a registration for this sensor already exists, replace it with
        //    this one.
        // 3. Keep (or update) a reference to the request object.
        // 4. Configure your sensor according to the request parameters.
        // 5. When the sensor has new data, deliver it to the platform by
        //    calling request.dispatcher.publish(dataPoints)
    }

    override fun onUnregister(dataSource: DataSource): Boolean {
        // 1. Configure this sensor to stop delivering data to the platform
        // 2. Discard the reference to the registration request object
    }
}

Java

public class MySensorService extends FitnessSensorService {
    @Override
    public void onCreate() {
        super.onCreate();
        // 1. Initialize your software sensor(s).
        // 2. Create DataSource representations of your software sensor(s).
        // 3. Initialize some data structure to keep track of a registration
        //    for each sensor.
    }

    @NonNull
    @Override
    public List<DataSource> onFindDataSources(@NonNull List<DataType> list) {
        // 1. Find which of your software sensors provide the data types
        //    requested.
        // 2. Return those as a list of DataSource objects.
    }

    @Override
    public boolean onRegister(
            @NonNull FitnessSensorServiceRequest fitnessSensorServiceRequest) {
        // 1. Determine which sensor to register with request.dataSource.
        // 2. If a registration for this sensor already exists, replace it with
        //    this one.
        // 3. Keep (or update) a reference to the request object.
        // 4. Configure your sensor according to the request parameters.
        // 5. When the sensor has new data, deliver it to the platform by
        //    calling request.getDispatcher.publish(dataPoints);
    }

    @Override
    public boolean onUnregister(@NonNull DataSource dataSource) {
        // 1. Configure this sensor to stop delivering data to the platform
        // 2. Discard the reference to the registration request object
    }
}