標準の Bluetooth Low Energy GATT プロファイルを実装するフィットネス デバイスは、Google Fit で自動的にサポートされます。デバイスにこれらのプロファイルのいずれかが実装されていない場合は、フィットネス デバイスとの通信を管理し、ソフトウェア センサーとして Google Fit に公開する Android アプリを作成できます。アプリ内でカスタム ソフトウェア センサーを公開することもできます。
アプリにソフトウェア センサーを作成するには、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 } }