Support additional sensors

Fitness devices that implement a standard Bluetooth Low Energy GATT profile are automatically supported by Google Fit. If your device doesn't implement one of these profiles, you can create an Android app that manages the communication with the fitness device and exposes it to Google Fit as a software sensor. You can also expose custom software sensors in your app.

To create a software sensor in your app, you extend the FitnessSensorService class and declare it as a service in the manifest file. When users install your app, Google Fit makes your software sensors available to other apps. When an app registers to receive data from a software sensor in your app, Google Fit binds to your service.

Declare a sensor service

To define a software sensor, declare a FitnessSensorService in your app's manifest file:

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

The service in this example runs in a separate process, as specified by the android:process attribute. For more information, see Processes .

Implement a sensor service

To implement a software sensor, extend the FitnessSensorService class and implement its abstract methods. The details of the implementation depend on your particular use case, but the following example provides general guidelines:

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 
  
 } 
 } 
Create a Mobile Website
View Site in Mobile | Classic
Share by: