支援其他感應器

Google Fit 會自動支援實作標準藍牙低功耗 GATT 設定檔的健身裝置。如果您的裝置並未實作上述任一設定檔,您可以建立 Android 應用程式來管理與健身裝置的通訊,並提供給 Google Fit 做為軟體感應器。您也可以在應用程式中公開自訂軟體感應器。

如要在應用程式中建立軟體感應器,請擴充 FitnessSensorService 類別,並在資訊清單檔案中宣告為服務。當使用者安裝應用程式時,Google Fit 會為其他應用程式提供軟體感應器。當應用程式註冊接收您應用程式中軟體感應器的資料時,Google Fit 會繫結至您的服務。

宣告感應器服務

如要定義軟體感應器,請在應用程式的資訊清單檔案中宣告 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
    }
}

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