Omówienie Bluetootha
Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
Platforma Android obsługuje stos sieci Bluetooth, który umożliwia urządzeniu bezprzewodową wymianę danych z innymi urządzeniami Bluetooth. Platforma aplikacji umożliwia dostęp do funkcji Bluetooth za pomocą interfejsów API Bluetooth. Te interfejsy API pozwalają aplikacjom łączyć się z innymi urządzeniami Bluetooth, co pozwala korzystać z połączeń bezprzewodowych i połączeń między punktami.
Dzięki interfejsom API Bluetooth aplikacja może wykonywać te czynności:
- Wyszukaj inne urządzenia Bluetooth.
- Zapytanie o lokalną kartę Bluetooth w przypadku sparowanych urządzeń Bluetooth.
- Ustal kanały RFCOMM.
- łączyć się z innymi urządzeniami przez wykrywanie usług;
- przenosić dane na inne urządzenia i z innych urządzeń,
- Zarządzaj wieloma połączeniami.
Ten temat dotyczy klasycznego Bluetootha. Klasyczny Bluetooth to odpowiedni wybór do działań, które zużywają więcej energii, np. przesyłania strumieniowego i komunikacji między urządzeniami. W przypadku urządzeń Bluetooth o niskich wymaganiach zasilania rozważ użycie połączeń Bluetooth Low Energy.
W tej dokumentacji opisujemy różne profile Bluetootha oraz wyjaśniamy, jak używać interfejsów API Bluetooth do wykonywania 4 głównych zadań niezbędnych do komunikacji przez Bluetooth:
- Konfiguruję Bluetooth.
- Znajduję urządzenia, które są sparowane lub dostępne w okolicy.
- Łączę urządzenia.
- Przenoszenie danych między urządzeniami
Instrukcję korzystania z interfejsów API Bluetooth znajdziesz w artykule o przykładowej aplikacji do obsługi czatu Bluetooth.
Podstawy
Aby urządzenia z obsługą Bluetootha mogły przesyłać dane między sobą, muszą najpierw utworzyć kanał komunikacji za pomocą procesu parowania. Jedno urządzenie, które jest wykrywalne, udostępnia się do obsługi przychodzących żądań połączenia.
Inne urządzenie znajduje wykrywalne urządzenie przy użyciu procesu wykrywania usług.
Gdy wykrywalne urządzenie zaakceptuje żądanie parowania, 2 urządzenia zakończą proces łączenia i wymieniają się kluczami bezpieczeństwa. Urządzenia zapisują je w pamięci podręcznej, by użyć ich później. Po zakończeniu procesu parowania i łączenia
urządzenia wymieniają informacje. Po zakończeniu sesji urządzenie, które zainicjowało żądanie parowania, zwalnia kanał, który je połączył z urządzeniem wykrywalnym. Oba urządzenia pozostają jednak połączone, więc mogą się połączyć automatycznie podczas kolejnej sesji, o ile znajdują się one w odległości od siebie, a żadne z nich nie usunęło powiązania.
Korzystanie z interfejsów API Bluetooth wymaga zadeklarowania kilku uprawnień w pliku manifestu. Gdy aplikacja ma uprawnienia do korzystania z Bluetootha, musi mieć dostęp do urządzenia BluetoothAdapter
i określić, czy na urządzeniu dostępny jest Bluetooth.
Jeśli Bluetooth jest dostępny, możesz wykonać połączenie w 3 krokach:
Niektóre urządzenia używają określonego profilu Bluetooth, który deklaruje przesyłane dane.
Klasy kluczy i interfejsy
Wszystkie interfejsy API Bluetooth są dostępne w pakiecie android.bluetooth
.
Oto klasy i interfejsy potrzebne do tworzenia połączeń Bluetooth:
BluetoothAdapter
- Reprezentuje lokalny adapter Bluetooth (radio Bluetooth).
BluetoothAdapter
to punkt wejścia wszystkich interakcji Bluetooth. Korzystając z niej, możesz wykrywać inne urządzenia Bluetooth, wysyłać zapytania o listę połączonych (sparowanych) urządzeń, tworzyć instancje BluetoothDevice
przy użyciu znanego adresu MAC oraz tworzyć BluetoothServerSocket
do nasłuchiwania komunikacji z innych urządzeń.
BluetoothDevice
- Reprezentuje zdalne urządzenie Bluetooth. Służy do żądania połączenia z urządzeniem zdalnym za pomocą obiektu
BluetoothSocket
lub wysyłania zapytań dotyczących urządzenia, takich jak jego nazwa, adres, klasa i stan powiązania.
BluetoothSocket
- Reprezentuje interfejs gniazda Bluetooth (podobnie jak TCP
Socket
). Umożliwia aplikacji wymianę danych z innym urządzeniem Bluetooth za pomocą InputStream
i OutputStream
.
BluetoothServerSocket
- Reprezentuje otwarte gniazdo serwera, które nasłuchuje żądań przychodzących (podobnie jak port TCP
ServerSocket
). Aby można było połączyć 2 urządzenia, jedno z nich musi mieć otwarte gniazdo serwera tej klasy. Gdy zdalne urządzenie Bluetooth wysyła żądanie połączenia z tym urządzeniem, akceptuje je, a potem zwraca połączone BluetoothSocket
.
BluetoothClass
- Opisuje ogólne cechy i możliwości urządzenia Bluetooth.
To jest dostępny tylko do odczytu zbiór właściwości, który określa klasy i usługi urządzenia. Te informacje zapewniają przydatne wskazówki dotyczące typu urządzenia, ale atrybuty tej klasy niekoniecznie opisują wszystkie profile i usługi Bluetooth obsługiwane przez urządzenie.
BluetoothProfile
- Interfejs reprezentujący profil Bluetooth. Profil Bluetooth to specyfikacja interfejsu bezprzewodowego do komunikacji między urządzeniami za pomocą Bluetootha. Przykładem może być profil Obsługa bez użycia rąk. Więcej o profilach dowiesz się na stronie Profile Bluetooth.
BluetoothHeadset
- Obsługują zestawy słuchawkowe Bluetooth używane z telefonami komórkowymi. Dotyczy to zarówno profilu zestawu słuchawkowego Bluetooth, jak i profilu zestawu słuchawkowego (v1.5).
BluetoothA2dp
- Określa, jak wysokiej jakości dźwięk może być przesyłany z jednego urządzenia na drugie przez połączenie Bluetooth przy użyciu profilu A2DP (Advanced Audio Distribution Profile).
BluetoothHealth
- Reprezentuje serwer proxy Profilu urządzenia zdrowotnego, który steruje usługą Bluetooth.
BluetoothHealthCallback
- Klasa abstrakcyjna służąca do implementowania wywołań zwrotnych (
BluetoothHealth
). Musisz rozszerzyć tę klasę i zaimplementować metody wywołania zwrotnego, aby otrzymywać informacje o zmianach w stanie rejestracji aplikacji i stanie kanału Bluetooth.
BluetoothHealthAppConfiguration
- Reprezentuje konfigurację aplikacji, którą aplikacja innej firmy z Bluetooth Health rejestruje, aby komunikować się ze zdalnym urządzeniem Bluetooth do zarządzania stanem danych.
BluetoothProfile.ServiceListener
- Interfejs powiadamiający klienty komunikacji międzyprocesowej (IPC) firmy
BluetoothProfile
, gdy zostały połączone z usługą wewnętrzną, która obsługuje określony profil, lub od niej rozłączona.
Treść strony i umieszczone na niej fragmenty kodu podlegają licencjom opisanym w Licencji na treści. Java i OpenJDK są znakami towarowymi lub zastrzeżonymi znakami towarowymi należącymi do firmy Oracle lub jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2025-07-26 UTC.
[[["Łatwo zrozumieć","easyToUnderstand","thumb-up"],["Rozwiązało to mój problem","solvedMyProblem","thumb-up"],["Inne","otherUp","thumb-up"]],[["Brak potrzebnych mi informacji","missingTheInformationINeed","thumb-down"],["Zbyt skomplikowane / zbyt wiele czynności do wykonania","tooComplicatedTooManySteps","thumb-down"],["Nieaktualne treści","outOfDate","thumb-down"],["Problem z tłumaczeniem","translationIssue","thumb-down"],["Problem z przykładami/kodem","samplesCodeIssue","thumb-down"],["Inne","otherDown","thumb-down"]],["Ostatnia aktualizacja: 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."]]