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.
Bluetooth API có hỗ trợ làm việc với các cấu hình Bluetooth. Đáp
Cấu hình Bluetooth là thông số kỹ thuật giao diện không dây cho dựa trên Bluetooth
hoạt động giao tiếp giữa các thiết bị, chẳng hạn như hồ sơ Rảnh tay. Đối với thiết bị di động
để kết nối với tai nghe không dây, cả hai thiết bị đều phải hỗ trợ
Hồ sơ rảnh tay.
Bluetooth API cung cấp các cách triển khai cho Bluetooth sau
hồ sơ:
Tai nghe. Cấu hình Tai nghe cung cấp hỗ trợ cho tai nghe Bluetooth
được sử dụng với điện thoại di động. Android cung cấp
Lớp BluetoothHeadset,
là proxy để kiểm soát Dịch vụ tai nghe Bluetooth. bao gồm
cả cấu hình Tai nghe Bluetooth và Rảnh tay (v1.5). BluetoothHeadset
class sẽ hỗ trợ các lệnh AT. Để biết thêm về chủ đề này, hãy xem
Các lệnh AT dành riêng cho nhà cung cấp.
A2DP. Cấu hình Cấu hình phân phối âm thanh nâng cao (A2DP) xác định cách
bạn có thể truyền âm thanh chất lượng cao từ thiết bị này sang thiết bị khác qua Bluetooth
kết nối. Android cung cấp
Lớp BluetoothA2dp là
một proxy để kiểm soát Dịch vụ Bluetooth A2DP.
Thiết bị sức khoẻ. Android cung cấp hỗ trợ cho Thiết bị sức khoẻ Bluetooth
Cấu hình (HDP). Quyền này cho phép bạn tạo các ứng dụng sử dụng Bluetooth để giao tiếp
với các thiết bị y tế hỗ trợ Bluetooth, chẳng hạn như máy đo nhịp tim, thiết bị đo máu
mét, nhiệt kế, cân, v.v. Để biết danh sách các thiết bị được hỗ trợ và
mã chuyên môn về dữ liệu thiết bị tương ứng của họ, hãy xem HDP của Bluetooth
Dữ liệu thiết bị
Chuyên môn.
Các giá trị này cũng được tham chiếu trong thông số kỹ thuật ISO/IEEE 11073-20601 [7]
là MDC_DEV_SPEC_PROFILE_* trong Phụ lục Mã danh pháp. Để biết thêm
thông tin về HDP, hãy xem Hồ sơ thiết bị sức khoẻ.
Dưới đây là các bước cơ bản để làm việc với một trang doanh nghiệp:
Sử dụng
getProfileProxy()
để thiết lập kết nối với đối tượng proxy hồ sơ liên kết với
hồ sơ. Trong ví dụ sau, đối tượng proxy hồ sơ là một thực thể của
BluetoothHeadset.
Sau khi bạn có đối tượng proxy hồ sơ, hãy sử dụng đối tượng này để theo dõi trạng thái của
kết nối và thực hiện các thao tác khác liên quan đến hồ sơ đó.
Đoạn mã sau đây cho biết cách kết nối với proxy BluetoothHeadset
để bạn có thể kiểm soát cấu hình Tai nghe:
Kotlin
varbluetoothHeadset:BluetoothHeadset? =null// Get the default adaptervalbluetoothAdapter:BluetoothAdapter? =BluetoothAdapter.getDefaultAdapter()privatevalprofileListener=object:BluetoothProfile.ServiceListener{overridefunonServiceConnected(profile:Int,proxy:BluetoothProfile){if(profile==BluetoothProfile.HEADSET){bluetoothHeadset=proxyasBluetoothHeadset}}overridefunonServiceDisconnected(profile:Int){if(profile==BluetoothProfile.HEADSET){bluetoothHeadset=null}}}// Establish connection to the proxy.bluetoothAdapter?.getProfileProxy(context,profileListener,BluetoothProfile.HEADSET)// ... call functions on bluetoothHeadset// Close proxy connection after use.bluetoothAdapter?.closeProfileProxy(BluetoothProfile.HEADSET,bluetoothHeadset)
Java
BluetoothHeadsetbluetoothHeadset;// Get the default adapterBluetoothAdapterbluetoothAdapter=BluetoothAdapter.getDefaultAdapter();privateBluetoothProfile.ServiceListenerprofileListener=newBluetoothProfile.ServiceListener(){publicvoidonServiceConnected(intprofile,BluetoothProfileproxy){if(profile==BluetoothProfile.HEADSET){bluetoothHeadset=(BluetoothHeadset)proxy;}}publicvoidonServiceDisconnected(intprofile){if(profile==BluetoothProfile.HEADSET){bluetoothHeadset=null;}}};// Establish connection to the proxy.bluetoothAdapter.getProfileProxy(context,profileListener,BluetoothProfile.HEADSET);// ... call functions on bluetoothHeadset// Close proxy connection after use.bluetoothAdapter.closeProfileProxy(bluetoothHeadset);
Lệnh AT dành riêng cho nhà cung cấp
Các ứng dụng có thể đăng ký nhận thông báo truyền phát của hệ thống về dữ liệu AT được xác định trước dành riêng cho nhà cung cấp
các lệnh mà tai nghe gửi (chẳng hạn như lệnh Plantronics +XEVENT). Ví dụ:
một ứng dụng có thể nhận các thông báo cho biết mức pin của thiết bị đã kết nối
và có thể thông báo cho người dùng hoặc thực hiện hành động khác nếu cần. Tạo thông báo truyền tin
người nhận cho
ACTION_VENDOR_SPECIFIC_HEADSET_EVENT
ý định xử lý các lệnh AT dành riêng cho nhà cung cấp cho tai nghe.
Khi sử dụng Bluetooth Health API, sẽ rất hữu ích để hiểu rõ những HDP chính này
các khái niệm:
Nguồn
Một thiết bị y tế (chẳng hạn như cân nặng, máy đo đường huyết hoặc nhiệt kế)
truyền dữ liệu y tế sang thiết bị thông minh, chẳng hạn như điện thoại hoặc máy tính bảng Android.
Bồn rửa
Thiết bị thông minh nhận dữ liệu y tế. Trong một ứng dụng HDP,
bồn lưu trữ dữ liệu được biểu thị bằng một đối tượng BluetoothHealthAppConfiguration.
Đăng ký
Quy trình dùng để đăng ký bồn rửa dùng để thông báo về một tình trạng sức khoẻ cụ thể
thiết bị.
Sự kết nối
Quá trình dùng để mở một kênh giữa một thiết bị y tế (nguồn) và một
thiết bị thông minh (bồn rửa).
Tạo một ứng dụng HDP
Dưới đây là các bước cơ bản để tạo một ứng dụng HDP:
Lấy thông tin tham chiếu đến đối tượng proxy BluetoothHealth. Như bình thường
tai nghe và thiết bị cấu hình A2DP, bạn phải gọi getProfileProxy() bằng
BluetoothProfile.ServiceListener và
HEALTH loại hồ sơ
để thiết lập kết nối với đối tượng proxy hồ sơ.
Tạo một BluetoothHealthCallback và đăng ký cấu hình ứng dụng
(BluetoothHealthAppConfiguration) đóng vai trò là bồn rửa bát.
Thiết lập kết nối với một thiết bị sức khoẻ.
Khi kết nối thành công với một thiết bị sức khoẻ, hãy đọc và ghi vào dữ liệu sức khoẻ
bằng cách sử dụng chỉ số mô tả tệp. Cần diễn giải dữ liệu nhận được
bằng cách sử dụng một trình quản lý sức khoẻ, trình quản lý này triển khai IEEE 11073
.
Khi hoàn tất, hãy đóng kênh thông tin sức khoẻ và huỷ đăng ký ứng dụng. Kênh này cũng
đóng khi có không hoạt động trong thời gian dài.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],null,["# Bluetooth profiles\n\nThe Bluetooth API includes support for working with Bluetooth profiles. A\nBluetooth profile is a wireless interface specification for Bluetooth-based\ncommunication between devices, such as the Hands-Free profile. For a mobile\ndevice to connect to a wireless headset, both devices must support the\nHands-Free profile.\n\nThe Bluetooth API provides implementations for the following Bluetooth\nprofiles:\n\n- **Headset** . The Headset profile provides support for Bluetooth headsets to be used with mobile phones. Android provides the [`BluetoothHeadset`](/reference/android/bluetooth/BluetoothHeadset) class, which is a proxy for controlling the Bluetooth Headset Service. This includes both Bluetooth Headset and Hands-Free (v1.5) profiles. The `BluetoothHeadset` class includes support for AT commands. For more on this topic, see [Vendor-specific AT commands](#at-commands).\n- **A2DP** . The Advanced Audio Distribution Profile (A2DP) profile defines how high-quality audio can be streamed from one device to another over a Bluetooth connection. Android provides the [`BluetoothA2dp`](/reference/android/bluetooth/BluetoothA2dp) class, which is a proxy for controlling the Bluetooth A2DP Service.\n- **Health Device** . Android provides support for the Bluetooth Health Device Profile (HDP). This lets you create apps that use Bluetooth to communicate with health devices that support Bluetooth, such as heart-rate monitors, blood meters, thermometers, scales, and so on. For a list of supported devices and their corresponding device data specialization codes, see [Bluetooth's HDP\n Device Data\n Specializations](https://www.bluetooth.com/specifications/assigned-numbers/health-device-profile). These values are also referenced in the ISO/IEEE 11073-20601 \\[7\\] specification as `MDC_DEV_SPEC_PROFILE_*` in the Nomenclature Codes Annex. For more information about HDP, see [Health Device Profile](#health-profile).\n\nHere are the basic steps for working with a profile:\n\n1. Get the default adapter, as described in [Bluetooth setup](/develop/connectivity/bluetooth/setup).\n2. Set up a [`BluetoothProfile.ServiceListener`](/reference/android/bluetooth/BluetoothProfile.ServiceListener). This listener notifies [`BluetoothProfile`](/reference/android/bluetooth/BluetoothProfile) clients when they have been connected to or disconnected from the service.\n3. Use [`getProfileProxy()`](/reference/android/bluetooth/BluetoothAdapter#getProfileProxy(android.content.Context,%20android.bluetooth.BluetoothProfile.ServiceListener,%20int)) to establish a connection to the profile proxy object associated with the profile. In the following example, the profile proxy object is an instance of `BluetoothHeadset`.\n4. In [`onServiceConnected()`](/reference/android/bluetooth/BluetoothProfile.ServiceListener#onServiceConnected(int,%20android.bluetooth.BluetoothProfile)), get a handle to the profile proxy object.\n5. Once you have the profile proxy object, use it to monitor the state of the connection and perform other operations that are relevant to that profile.\n\nThe following code snippet shows how to connect to a `BluetoothHeadset` proxy\nobject so that you can control the Headset profile: \n\n### Kotlin\n\n```kotlin\nvar bluetoothHeadset: BluetoothHeadset? = null\n\n// Get the default adapter\nval bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()\n\nprivate val profileListener = object : BluetoothProfile.ServiceListener {\n\n override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {\n if (profile == BluetoothProfile.HEADSET) {\n bluetoothHeadset = proxy as BluetoothHeadset\n }\n }\n\n override fun onServiceDisconnected(profile: Int) {\n if (profile == BluetoothProfile.HEADSET) {\n bluetoothHeadset = null\n }\n }\n}\n\n// Establish connection to the proxy.\nbluetoothAdapter?.getProfileProxy(context, profileListener, BluetoothProfile.HEADSET)\n\n// ... call functions on bluetoothHeadset\n\n// Close proxy connection after use.\nbluetoothAdapter?.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset)\n```\n\n### Java\n\n```java\nBluetoothHeadset bluetoothHeadset;\n\n// Get the default adapter\nBluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();\n\nprivate BluetoothProfile.ServiceListener profileListener = new BluetoothProfile.ServiceListener() {\n public void onServiceConnected(int profile, BluetoothProfile proxy) {\n if (profile == BluetoothProfile.HEADSET) {\n bluetoothHeadset = (BluetoothHeadset) proxy;\n }\n }\n public void onServiceDisconnected(int profile) {\n if (profile == BluetoothProfile.HEADSET) {\n bluetoothHeadset = null;\n }\n }\n};\n\n// Establish connection to the proxy.\nbluetoothAdapter.getProfileProxy(context, profileListener, BluetoothProfile.HEADSET);\n\n// ... call functions on bluetoothHeadset\n\n// Close proxy connection after use.\nbluetoothAdapter.closeProfileProxy(bluetoothHeadset);\n```\n\n### Vendor-specific AT commands\n\nApps can register to receive system broadcasts of predefined vendor-specific AT\ncommands sent by headsets (such as a Plantronics +XEVENT command). For example,\nan app could receive broadcasts that indicate a connected device's battery level\nand could notify the user or take other action as needed. Create a broadcast\nreceiver for the\n[`ACTION_VENDOR_SPECIFIC_HEADSET_EVENT`](/reference/android/bluetooth/BluetoothHeadset#ACTION_VENDOR_SPECIFIC_HEADSET_EVENT)\nintent to handle vendor-specific AT commands for the headset.\n\nHealth Device Profile\n---------------------\n\nAndroid supports the Bluetooth Health Device Profile (HDP). The Bluetooth Health\nAPI includes the classes\n[`BluetoothHealth`](/reference/android/bluetooth/BluetoothHealth),\n[`BluetoothHealthCallback`](/reference/android/bluetooth/BluetoothHealthCallback),\nand\n[`BluetoothHealthAppConfiguration`](/reference/android/bluetooth/BluetoothHealthAppConfiguration),\nwhich are described in [Key classes and\ninterfaces](/develop/connectivity/bluetooth#key-classes).\n| **Caution:** The Health Device Profile (HDP) and MCAP protocols are no longer used. New apps should use Bluetooth Low Energy based solutions such as [`BluetoothGatt`](/reference/android/bluetooth/BluetoothGatt), [`BluetoothAdapter.listenUsingL2capChannel()`](/reference/android/bluetooth/BluetoothAdapter#listenUsingL2capChannel()), or [`BluetoothDevice#createL2capChannel(int)`](/reference/android/bluetooth/BluetoothDevice#createL2capChannel(int)).\n\nWhen using the Bluetooth Health API, it's helpful to understand these key HDP\nconcepts:\n\nSource\n: A health device, such as a weight scale, glucose meter, or thermometer, which\n transmits medical data to a smart device, such as an Android phone or tablet.\n\nSink\n: The smart device that receives the medical data. In an HDP app, the\n sink is represented by a `BluetoothHealthAppConfiguration` object.\n\nRegistration\n: The process used to register a sink for communicating with a particular health\n device.\n\nConnection\n: The process used to open a channel between a health device (source) and a\n smart device (sink).\n\n### Create an HDP app\n\nHere are the basic steps involved in creating an HDP app:\n\n1. Get a reference to the `BluetoothHealth` proxy object. As with regular\n headset and A2DP profile devices, you must call `getProfileProxy()` with a\n `BluetoothProfile.ServiceListener` and the\n [`HEALTH`](/reference/android/bluetooth/BluetoothProfile#HEALTH) profile type\n to establish a connection with the profile proxy object.\n\n2. Create a `BluetoothHealthCallback` and register an app configuration\n (`BluetoothHealthAppConfiguration`) that acts as a health sink.\n\n3. Establish a connection to a health device.\n\n | **Note:** Some devices initiate the connection automatically. It is unnecessary to carry out this step for those devices.\n4. When connected successfully to a health device, read and write to the health\n device using the file descriptor. The received data needs to be interpreted\n using a health manager, which implements the [IEEE 11073\n specifications](https://standards.ieee.org/standard/11073-10207-2017.html).\n\n5. When done, close the health channel and unregister the app. The channel also\n closes when there is extended inactivity."]]