Tổng quan về Bluetooth
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.
Nền tảng Android hỗ trợ ngăn xếp mạng Bluetooth, cho phép thiết bị trao đổi dữ liệu không dây với các thiết bị Bluetooth khác. Khung ứng dụng cung cấp quyền truy cập vào chức năng Bluetooth thông qua các API Bluetooth. Các API này cho phép ứng dụng kết nối với các thiết bị Bluetooth khác, tạo điều kiện cho các tính năng không dây điểm nối và đa điểm.
Khi sử dụng API Bluetooth, ứng dụng có thể thực hiện những việc sau:
- Quét tìm các thiết bị Bluetooth khác.
- Truy vấn bộ điều hợp Bluetooth cục bộ cho các thiết bị Bluetooth đã ghép nối.
- Thiết lập các kênh RFCOMM.
- Kết nối với các thiết bị khác thông qua tính năng khám phá dịch vụ.
- Chuyển dữ liệu sang và từ thiết bị khác.
- Quản lý nhiều kết nối.
Chủ đề này tập trung vào Bluetooth cũ. Bluetooth cũ là lựa chọn phù hợp cho các hoạt động tốn nhiều pin hơn, bao gồm cả việc phát trực tuyến và giao tiếp giữa các thiết bị. Đối với các thiết bị Bluetooth có yêu cầu sử dụng pin thấp, hãy cân nhắc sử dụng kết nối Bluetooth năng lượng thấp.
Tài liệu này mô tả nhiều hồ sơ Bluetooth và giải thích cách sử dụng các API Bluetooth để hoàn thành 4 nhiệm vụ chính cần thiết để giao tiếp bằng Bluetooth:
- Đang thiết lập Bluetooth.
- Tìm các thiết bị đã được ghép nối hoặc khả dụng trong khu vực địa phương.
- Đang kết nối thiết bị.
- Chuyển dữ liệu giữa các thiết bị.
Để xem bản minh hoạ cách sử dụng các API Bluetooth, hãy xem ứng dụng mẫu Trò chuyện qua Bluetooth.
Thông tin cơ bản
Để các thiết bị hỗ trợ Bluetooth truyền dữ liệu giữa nhau, trước tiên các thiết bị đó phải tạo thành một kênh liên lạc bằng quy trình ghép nối. Một thiết bị (tức là một thiết bị có thể phát hiện) sẽ sẵn sàng xử lý các yêu cầu kết nối đến.
Một thiết bị khác tìm thiết bị có thể tìm thấy bằng quy trình khám phá dịch vụ.
Sau khi thiết bị tìm thấy chấp nhận yêu cầu ghép nối, hai thiết bị sẽ hoàn tất quá trình liên kết, trong đó chúng trao đổi khoá bảo mật. Thiết bị lưu các khoá này vào bộ nhớ đệm để sử dụng sau này. Sau khi quá trình ghép nối và liên kết hoàn tất, hai thiết bị sẽ trao đổi thông tin. Khi phiên hoàn tất, thiết bị đưa ra yêu cầu ghép nối sẽ huỷ kênh đã liên kết với thiết bị có thể tìm thấy. Tuy nhiên, 2 thiết bị vẫn liên kết với nhau nên chúng có thể tự động kết nối lại trong phiên sau này, miễn là chúng vẫn nằm trong phạm vi của nhau và không thiết bị nào loại bỏ liên kết.
Để sử dụng các API Bluetooth, bạn phải khai báo một số quyền trong tệp kê khai. Sau khi có quyền sử dụng Bluetooth, ứng dụng của bạn cần truy cập vào BluetoothAdapter
và xác định xem có Bluetooth trên thiết bị hay không.
Nếu có Bluetooth, bạn cần thực hiện 3 bước để kết nối:
Một số thiết bị dùng hồ sơ Bluetooth cụ thể để khai báo dữ liệu mà ứng dụng cung cấp.
Các lớp và giao diện chính
Tất cả API Bluetooth đều có trong gói android.bluetooth
.
Sau đây là các lớp và giao diện bạn cần có để tạo kết nối Bluetooth:
BluetoothAdapter
- Đại diện cho bộ chuyển đổi Bluetooth cục bộ (Đài phát thanh Bluetooth).
BluetoothAdapter
là điểm truy cập cho tất cả hoạt động tương tác qua Bluetooth. Bằng cách này, bạn có thể khám phá các thiết bị Bluetooth khác, truy vấn danh sách thiết bị đã liên kết (đã ghép nối), tạo thực thể cho BluetoothDevice
bằng địa chỉ MAC đã biết và tạo một BluetoothServerSocket
để nghe thông tin liên lạc từ các thiết bị khác.
BluetoothDevice
- Đại diện cho một thiết bị Bluetooth từ xa. Hãy sử dụng phương thức này để yêu cầu kết nối với một thiết bị từ xa thông qua
BluetoothSocket
hoặc truy vấn thông tin về thiết bị, chẳng hạn như tên, địa chỉ, lớp và trạng thái liên kết.
BluetoothSocket
- Đại diện cho giao diện của cổng Bluetooth (tương tự như
Socket
của TCP). Đây là điểm kết nối cho phép ứng dụng trao đổi dữ liệu với một thiết bị Bluetooth khác bằng InputStream
và OutputStream
.
BluetoothServerSocket
- Đại diện cho một ổ cắm máy chủ mở để lắng nghe các yêu cầu đến (tương tự như TCP
ServerSocket
). Để kết nối hai thiết bị, một thiết bị phải mở một cổng máy chủ bằng lớp này. Khi một thiết bị Bluetooth từ xa gửi yêu cầu kết nối với thiết bị này, thiết bị sẽ chấp nhận kết nối rồi trả về một BluetoothSocket
đã kết nối.
BluetoothClass
- Mô tả các đặc điểm và chức năng chung của thiết bị Bluetooth.
Đây là tập hợp các thuộc tính chỉ có thể đọc xác định các lớp và dịch vụ của thiết bị. Mặc dù thông tin này cung cấp gợi ý hữu ích về loại thiết bị, nhưng các thuộc tính của lớp này không nhất thiết phải mô tả tất cả các cấu hình và dịch vụ Bluetooth mà thiết bị hỗ trợ.
BluetoothProfile
- Giao diện đại diện cho hồ sơ Bluetooth. Cấu hình Bluetooth là thông số kỹ thuật của giao diện không dây để giao tiếp dựa trên Bluetooth giữa các thiết bị. Một ví dụ là hồ sơ rảnh tay. Để thảo luận thêm về hồ sơ, hãy xem bài viết Hồ sơ Bluetooth.
BluetoothHeadset
- Cung cấp tính năng hỗ trợ tai nghe Bluetooth dùng được với điện thoại di động. Cấu hình này bao gồm cả cấu hình Tai nghe Bluetooth và cấu hình Rảnh tay (phiên bản 1.5).
BluetoothA2dp
- Xác định cách phát âm thanh chất lượng cao từ thiết bị này sang thiết bị khác qua kết nối Bluetooth bằng Cấu hình phân phối âm thanh nâng cao (A2DP).
BluetoothHealth
- Đại diện cho một proxy Hồ sơ thiết bị sức khoẻ kiểm soát dịch vụ Bluetooth.
BluetoothHealthCallback
- Một lớp trừu tượng mà bạn dùng để triển khai lệnh gọi lại
BluetoothHealth
. Bạn phải mở rộng lớp này và triển khai các phương thức gọi lại để nhận thông tin cập nhật về những thay đổi đối với trạng thái đăng ký của ứng dụng và trạng thái kênh Bluetooth.
BluetoothHealthAppConfiguration
- Đại diện cho cấu hình ứng dụng mà ứng dụng Bluetooth Health bên thứ ba đăng ký để giao tiếp với thiết bị theo dõi Bluetooth từ xa.
BluetoothProfile.ServiceListener
- Giao diện thông báo cho các ứng dụng giao tiếp liên quy trình (IPC)
BluetoothProfile
khi các ứng dụng này đã được kết nối hoặc ngắt kết nối khỏi dịch vụ nội bộ chạy một hồ sơ cụ thể.
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-26 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[],[],null,["# Bluetooth overview\n\nThe Android platform includes support for the Bluetooth network stack, which\nallows a device to wirelessly exchange data with other Bluetooth devices. The\napp framework provides access to the Bluetooth functionality through Bluetooth\nAPIs. These APIs let apps connect to other Bluetooth devices, enabling\npoint-to-point and multipoint wireless features.\n\nUsing the Bluetooth APIs, an app can perform the following:\n\n- Scan for other Bluetooth devices.\n- Query the local Bluetooth adapter for paired Bluetooth devices.\n- Establish RFCOMM channels.\n- Connect to other devices through service discovery.\n- Transfer data to and from other devices.\n- Manage multiple connections.\n\nThis topic focuses on *Classic Bluetooth* . Classic Bluetooth is the right choice\nfor more battery-intensive operations, which include streaming and communicating\nbetween devices. For Bluetooth devices with low power requirements, consider\nusing [Bluetooth Low Energy](/develop/connectivity/bluetooth/ble-overview)\nconnections.\n\nThis documentation describes different Bluetooth\n[profiles](/develop/connectivity/bluetooth/profiles) and explains how to\nuse the Bluetooth APIs to accomplish the four major tasks necessary to\ncommunicate using Bluetooth:\n\n- Setting up Bluetooth.\n- Finding devices that are either paired or available in the local area.\n- Connecting devices.\n- Transferring data between devices.\n\nFor a demonstration of using the Bluetooth APIs, see the [Bluetooth Chat sample\napp](https://github.com/android/connectivity-samples/tree/master/BluetoothChat).\n\nThe basics\n----------\n\nFor Bluetooth-enabled devices to transmit data between each other, they must\nfirst form a channel of communication using a pairing process. One device, a\ndiscoverable device, makes itself available for incoming connection requests.\nAnother device finds the discoverable device using a service discovery process.\nAfter the discoverable device accepts the pairing request, the two devices\ncomplete a bonding process in which they exchange security keys. The devices\ncache these keys for later use. After the pairing and bonding processes are\ncomplete, the two devices exchange information. When the session is complete,\nthe device that initiated the pairing request releases the channel that had\nlinked it to the discoverable device. The two devices remain bonded, however, so\nthey can reconnect automatically during a future session as long as they're in\nrange of each other and neither device has removed the bond.\n\nUse of the Bluetooth APIs requires\n[declaring several permissions](/develop/connectivity/bluetooth/bt-permissions#declare)\nin your manifest file. Once your app has permission to use Bluetooth, your app\nneeds to access the\n[`BluetoothAdapter`](/reference/android/bluetooth/BluetoothAdapter) and\n[determine if Bluetooth is available on the device](/develop/connectivity/bluetooth/setup).\nIf Bluetooth is available, there are three steps to make a connection:\n\n- [Find nearby Bluetooth\n devices](/develop/connectivity/bluetooth/find-bluetooth-devices), either devices that are already paired or new ones.\n- [Connect to a Bluetooth\n device](/develop/connectivity/bluetooth/connect-bluetooth-devices).\n- [Transfer data with the connected\n device](/develop/connectivity/bluetooth/transfer-data).\n\nCertain devices use a specific [Bluetooth\nprofile](/develop/connectivity/bluetooth/profiles) that declares the data\nit provides.\n\nKey classes and interfaces\n--------------------------\n\nAll of the Bluetooth APIs are available in the\n[`android.bluetooth`](/reference/android/bluetooth/package-summary) package.\nThe following are the classes and interfaces you need in order to create\nBluetooth connections:\n\n[`BluetoothAdapter`](/reference/android/bluetooth/BluetoothAdapter)\n: Represents the local Bluetooth adapter (Bluetooth radio). The\n `BluetoothAdapter` is the entry-point for all Bluetooth interaction. Using\n this, you can discover other Bluetooth devices, query a list of bonded\n (paired) devices, instantiate a\n `BluetoothDevice` using a known MAC address, and create a\n `BluetoothServerSocket` to listen for communications from other devices.\n\n[`BluetoothDevice`](/reference/android/bluetooth/BluetoothDevice)\n: Represents a remote Bluetooth device. Use this to request a connection with a\n remote device through a `BluetoothSocket` or query information about the\n device such as its name, address, class, and bonding state.\n\n[`BluetoothSocket`](/reference/android/bluetooth/BluetoothSocket)\n: Represents the interface for a Bluetooth socket (similar to a TCP\n [`Socket`](/reference/java/net/Socket)). This is the connection point that\n allows an app to exchange data with another Bluetooth device using\n [`InputStream`](/reference/java/io/InputStream) and\n [`OutputStream`](/reference/java/io/OutputStream).\n\n[`BluetoothServerSocket`](/reference/android/bluetooth/BluetoothServerSocket)\n: Represents an open server socket that listens for incoming requests (similar\n to a TCP [`ServerSocket`](/reference/java/net/ServerSocket)). In order to\n connect two devices, one device must open a server socket with this\n class. When a remote Bluetooth device makes a connection request to this\n device, the device accepts the connection and then returns a connected\n `BluetoothSocket`.\n\n[`BluetoothClass`](/reference/android/bluetooth/BluetoothClass)\n: Describes the general characteristics and capabilities of a Bluetooth device.\n This is a read-only set of properties that defines the device's classes and\n services. Although this information provides a useful hint regarding a\n device's type, the attributes of this class don't necessarily describe all\n Bluetooth profiles and services that the device supports.\n\n[`BluetoothProfile`](/reference/android/bluetooth/BluetoothProfile)\n: An interface that represents a Bluetooth profile. A Bluetooth profile is a\n wireless interface specification for Bluetooth-based communication between\n devices. An example is the Hands-Free profile. For more discussion of\n profiles, see [Bluetooth profiles](/develop/connectivity/bluetooth/profiles).\n\n[`BluetoothHeadset`](/reference/android/bluetooth/BluetoothHeadset)\n: Provides support for Bluetooth headsets to be used with mobile phones. This\n includes both the Bluetooth Headset profile and the Hands-Free (v1.5) profile.\n\n[`BluetoothA2dp`](/reference/android/bluetooth/BluetoothA2dp)\n: Defines how high-quality audio can be streamed from one device to another over\n a Bluetooth connection using the Advanced Audio Distribution Profile (A2DP).\n\n[`BluetoothHealth`](/reference/android/bluetooth/BluetoothHealth)\n: Represents a Health Device Profile proxy that controls the Bluetooth service.\n\n[`BluetoothHealthCallback`](/reference/android/bluetooth/BluetoothHealthCallback)\n: An abstract class that you use to implement `BluetoothHealth` callbacks. You\n must extend this class and implement the callback methods to receive updates\n about changes in the app's registration state and Bluetooth channel\n state.\n\n[`BluetoothHealthAppConfiguration`](/reference/android/bluetooth/BluetoothHealthAppConfiguration)\n: Represents an app configuration that the Bluetooth Health third-party\n app registers to communicate with a remote Bluetooth health device.\n\n[`BluetoothProfile.ServiceListener`](/reference/android/bluetooth/BluetoothProfile.ServiceListener)\n: An interface that notifies `BluetoothProfile` interprocess communication (IPC)\n clients when they have been connected to or disconnected from the internal\n service that runs a particular profile."]]