إتاحة أدوات استشعار إضافية

يتوافق Google Fit تلقائيًا مع أجهزة اللياقة البدنية التي تستخدم الملف الشخصي القياسي البلوتوث منخفض الطاقة GATT. إذا لم يستخدم جهازك أحد هذه الملفات الشخصية، يمكنك إنشاء تطبيق 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 وتنفيذ الطرق المجرّدة. تعتمد تفاصيل التنفيذ على حالة الاستخدام الخاصة بك، ولكن المثال التالي يقدم إرشادات عامة:

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