支援其他感應器

採用標準 藍牙低功耗 GATT 設定檔的健身裝置,會自動受到 Google Fit 支援。如果您的裝置 「未」實作其中一個設定檔,您可以建立一個 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 類別和 實作抽象方法實作方式的細節取決於您的特定用途,但以下範例提供一般指引:

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