รองรับเซ็นเซอร์เพิ่มเติม

Google Fit รองรับอุปกรณ์ฟิตเนสที่ใช้โปรไฟล์บลูทูธพลังงานต่ํา GATT มาตรฐานโดยอัตโนมัติ หากอุปกรณ์ไม่ได้ใช้ 1 ใน 3 โปรไฟล์นี้ คุณอาจสร้างแอป Android ที่จัดการการสื่อสารกับอุปกรณ์ฟิตเนส แล้วเปิดให้ Google Fit เป็นเซ็นเซอร์ซอฟต์แวร์ได้ และยังเปิดเผยเซ็นเซอร์ซอฟต์แวร์ที่กําหนดเอง ในแอปได้ด้วย

หากต้องการสร้างเซ็นเซอร์ซอฟต์แวร์ในแอป คุณจะต้องขยายคลาส FitnessSensorService และประกาศเป็นบริการในไฟล์ Manifest เมื่อผู้ใช้ติดตั้งแอป Google Fit จะทําให้เซ็นเซอร์ซอฟต์แวร์ของคุณใช้งานได้กับแอปอื่นๆ เมื่อแอปลงทะเบียนเพื่อรับข้อมูลจากเซ็นเซอร์ซอฟต์แวร์ในแอป Google Fit จะผูกบริการของคุณ

ประกาศบริการเซ็นเซอร์

หากต้องการกําหนดเซ็นเซอร์ซอฟต์แวร์ ให้ประกาศ FitnessSensorService ในไฟล์ Manifest ของแอป ดังนี้

<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 และนําวิธีการนามธรรมไปใช้ รายละเอียดของการนําไปใช้จะขึ้นอยู่กับ Use Case ที่เจาะจงแต่ตัวอย่างต่อไปนี้เป็นหลักเกณฑ์ทั่วไป

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