پشتیبانی از سنسورهای اضافی

دستگاه‌های تناسب اندام که نمایه استاندارد گات کم مصرف بلوتوث را اجرا می‌کنند، به‌طور خودکار توسط 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 را گسترش داده و روش های انتزاعی آن را پیاده سازی کنید. جزئیات پیاده سازی به مورد استفاده خاص شما بستگی دارد، اما مثال زیر دستورالعمل های کلی را ارائه می دهد:

کاتلین

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