Hỗ trợ các cảm biến khác
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Thiết bị thể dục có Bluetooth năng lượng thấp Bluetooth tiêu chuẩn
GATT
được Google Fit hỗ trợ tự động. Nếu thiết bị của bạn
không triển khai một trong các cấu hình này, bạn có thể tạo một ứng dụng Android
quản lý hoạt động giao tiếp với thiết bị thể dục và tiếp cận thiết bị đó
Google Fit làm cảm biến phần mềm. Bạn cũng có thể hiển thị phần mềm tuỳ chỉnh
các cảm biến trong ứng dụng của bạn.
Để tạo cảm biến phần mềm trong ứng dụng, bạn hãy mở rộng
FitnessSensorService
và khai báo dịch vụ đó dưới 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,
Google Fit 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ởi
android:process
. Để biết thêm thông tin, hãy xem
Quy trình.
Triển khai dịch vụ cảm biến
Để triển khai một 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. Chi tiết của việc triển khai phụ thuộc vào
trường hợp sử dụng cụ thể, nhưng ví dụ sau là 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
}
}
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-31 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\u003cp\u003eGoogle Fit automatically supports fitness devices using standard Bluetooth Low Energy GATT profiles.\u003c/p\u003e\n"],["\u003cp\u003eFor devices lacking standard profiles, an Android app can bridge communication with Google Fit using software sensors.\u003c/p\u003e\n"],["\u003cp\u003eSoftware sensors are created by extending the \u003ccode\u003eFitnessSensorService\u003c/code\u003e class and declaring it as a service in the app's manifest.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Fit exposes your software sensors to other apps upon installation, facilitating data sharing.\u003c/p\u003e\n"],["\u003cp\u003eImplementation of a software sensor involves initializing sensors, handling data source requests, and managing sensor registration and data delivery.\u003c/p\u003e\n"]]],[],null,["# Support additional sensors\n\nFitness devices that implement a standard [Bluetooth Low Energy\nGATT](https://www.bluetooth.com/bluetooth-resources/intro-to-bluetooth-gap-gatt/)\nprofile are automatically supported by Google Fit. If your device\ndoesn't implement one of these profiles, you can create an Android app that\nmanages the communication with the fitness device and exposes it to\nGoogle Fit as a software sensor. You can also expose custom software\nsensors in your app.\n\nTo create a software sensor in your app, you extend the\n[`FitnessSensorService`](/android/reference/com/google/android/gms/fitness/service/FitnessSensorService)\nclass and declare it as a service in the manifest file. When users install your\napp, Google Fit makes your software sensors available to other apps.\nWhen an app registers to receive data from a software sensor in your app,\nGoogle Fit binds to your service.\n| **Note:** For best practices when you manage user data, see [Google Fit Developer and User Data Policy](/fit/policy).\n\nDeclare a sensor service\n------------------------\n\nTo define a software sensor, declare a\n[`FitnessSensorService`](/android/reference/com/google/android/gms/fitness/service/FitnessSensorService)\nin your app's manifest file: \n\n```carbon\n\u003cservice android:name=\"com.example.MySensorService\"\n android:process=\":sensor\"\u003e\n \u003cintent-filter\u003e\n \u003caction android:name=\"com.google.android.gms.fitness.service.FitnessSensorService\" /\u003e\n \u003c!-- include at least one mimeType filter for the supported data types --\u003e\n \u003cdata android:mimeType=\"vnd.google.fitness.data_type/com.google.heart_rate.bpm\" /\u003e\n \u003c/intent-filter\u003e\n\u003c/service\u003e\n```\n\nThe service in this example runs in a separate process, as specified by the\n[`android:process`](https://developer.android.com/guide/topics/manifest/service-element#proc)\nattribute. For more information, see\n[Processes](https://developer.android.com/guide/components/processes-and-threads#Processes).\n\nImplement a sensor service\n--------------------------\n\nTo implement a software sensor, extend the `FitnessSensorService` class and\nimplement its abstract methods. The details of the implementation depend on your\nparticular use case, but the following example provides general guidelines: \n\n### Kotlin\n\n```kotlin\nclass MySensorService : FitnessSensorService() {\n override fun onCreate() {\n super.onCreate()\n // 1. Initialize your software sensor(s).\n // 2. Create DataSource representations of your software sensor(s).\n // 3. Initialize some data structure to keep track of a registration\n // for each sensor.\n }\n\n override fun onFindDataSources(dataTypes: List\u003cDataType\u003e): List\u003cDataSource\u003e {\n // 1. Find which of your software sensors provide the data types requested.\n // 2. Return those as a list of DataSource objects.\n }\n\n override fun onRegister(request: FitnessSensorServiceRequest): Boolean {\n // 1. Determine which sensor to register with request.dataSource.\n // 2. If a registration for this sensor already exists, replace it with\n // this one.\n // 3. Keep (or update) a reference to the request object.\n // 4. Configure your sensor according to the request parameters.\n // 5. When the sensor has new data, deliver it to the platform by\n // calling request.dispatcher.publish(dataPoints)\n }\n\n override fun onUnregister(dataSource: DataSource): Boolean {\n // 1. Configure this sensor to stop delivering data to the platform\n // 2. Discard the reference to the registration request object\n }\n}\n```\n\n### Java\n\n```java\npublic class MySensorService extends FitnessSensorService {\n @Override\n public void onCreate() {\n super.onCreate();\n // 1. Initialize your software sensor(s).\n // 2. Create DataSource representations of your software sensor(s).\n // 3. Initialize some data structure to keep track of a registration\n // for each sensor.\n }\n\n @NonNull\n @Override\n public List\u003cDataSource\u003e onFindDataSources(@NonNull List\u003cDataType\u003e list) {\n // 1. Find which of your software sensors provide the data types\n // requested.\n // 2. Return those as a list of DataSource objects.\n }\n\n @Override\n public boolean onRegister(\n @NonNull FitnessSensorServiceRequest fitnessSensorServiceRequest) {\n // 1. Determine which sensor to register with request.dataSource.\n // 2. If a registration for this sensor already exists, replace it with\n // this one.\n // 3. Keep (or update) a reference to the request object.\n // 4. Configure your sensor according to the request parameters.\n // 5. When the sensor has new data, deliver it to the platform by\n // calling request.getDispatcher.publish(dataPoints);\n }\n\n @Override\n public boolean onUnregister(@NonNull DataSource dataSource) {\n // 1. Configure this sensor to stop delivering data to the platform\n // 2. Discard the reference to the registration request object\n }\n}\n```"]]