추가 센서 지원

표준 Bluetooth 저에너지 GATT 프로필을 구현하는 피트니스 기기는 Google 피트니스에서 자동으로 지원됩니다. 기기가 이러한 프로필 중 하나를 구현하지 않는 경우 피트니스 기기와의 통신을 관리하고 이를 Google 피트니스에 소프트웨어 센서로 노출하는 Android 앱을 만들 수 있습니다. 앱에서 커스텀 소프트웨어 센서를 노출할 수도 있습니다.

앱에서 소프트웨어 센서를 만들려면 FitnessSensorService 클래스를 확장하고 매니페스트 파일에서 서비스로 선언합니다. 사용자가 앱을 설치하면 Google 피트니스가 소프트웨어 센서를 다른 앱에 제공합니다. 앱이 앱의 소프트웨어 센서에서 데이터를 수신하도록 등록하면 Google 피트니스가 서비스에 바인딩됩니다.

센서 서비스 선언

소프트웨어 센서를 정의하려면 앱의 매니페스트 파일에서 FitnessSensorService을 선언합니다.

<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>

이 예시의 서비스는 android:process 속성에서 지정된 별도의 프로세스에서 실행됩니다. 자세한 내용은 프로세스를 참고하세요.

센서 서비스 구현

소프트웨어 센서를 구현하려면 FitnessSensorService 클래스를 확장하고 추상 메서드를 구현합니다. 구현 세부정보는 구체적인 사용 사례에 따라 다르지만 다음 예에서는 일반적인 가이드라인을 제공합니다.

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
    }
}

자바

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
    }
}