Hỗ trợ thêm cảm biến

Google Fit sẽ tự động hỗ trợ các thiết bị thể dục triển khai hồ sơ tiêu chuẩn Bluetooth có năng lượng thấp Bluetooth. Nếu thiết bị không triển khai một trong các cấu hình này, thì bạn có thể tạo một ứng dụng Android quản lý việc giao tiếp với thiết bị thể dục và phơi sáng trên Google Fit dưới dạng cảm biến phần mềm. Bạn cũng có thể hiển thị cảm biến phần mềm tuỳ chỉnh trong ứng dụng.

Để tạo cảm biến phần mềm trong ứng dụng, bạn mở rộng lớp FitnessSensorService và khai báo lớp đó ở dạng dịch vụ trong tệp kê khai. Khi người dùng cài đặt ứng dụng, Google Fit sẽ cung cấp cảm biến phần mềm cho các ứng dụng khác. Khi một ứng dụng đăng ký nhận dữ liệu từ cảm biến phần mềm trong ứng dụng của bạn, Google Fit sẽ liên kết với dịch vụ của bạn.

Khai báo dịch vụ cảm biến

Để xác định cảm biến phần mềm, hãy khai báo FitnessSensorService trong tệp kê khai của ứng dụng:

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

Dịch vụ trong ví dụ này chạy trong một quy trình riêng, như được chỉ định bằng thuộc tính android:process. Để biết thêm thông tin, hãy xem Các quy trình.

Triển khai dịch vụ cảm biến

Để triển khai cảm biến phần mềm, hãy mở rộng lớp FitnessSensorService và triển khai các phương thức trừu tượng của lớp đó. Chi tiết triển khai tuỳ thuộc vào trường hợp sử dụng cụ thể, nhưng ví dụ sau đây cung cấp các nguyên tắc chung:

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