अन्य सेंसर के साथ काम करता है

जिन फ़िटनेस डिवाइसों में स्टैंडर्ड ब्लूटूथ कम ऊर्जा GATT प्रोफ़ाइल लागू होती है वे 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 क्लास को बढ़ाएं और इसके ऐब्स्ट्रैक्ट तरीके लागू करें. लागू करने के बारे में जानकारी आपके इस्तेमाल के खास उदाहरण पर निर्भर करती है, लेकिन नीचे दिए गए उदाहरण में सामान्य दिशा-निर्देश दिए गए हैं:

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