نمای کلی بلوتوث
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
پلتفرم اندروید شامل پشتیبانی از پشته شبکه بلوتوث است که به دستگاه اجازه می دهد به صورت بی سیم داده ها را با سایر دستگاه های بلوتوث مبادله کند. چارچوب برنامه دسترسی به عملکرد بلوتوث را از طریق APIهای بلوتوث فراهم می کند. این APIها به برنامهها اجازه میدهند به سایر دستگاههای بلوتوث متصل شوند و ویژگیهای بیسیم نقطه به نقطه و چند نقطه را فعال میکنند.
با استفاده از API های بلوتوث، یک برنامه می تواند کارهای زیر را انجام دهد:
- سایر دستگاه های بلوتوث را اسکن کنید.
- از آداپتور بلوتوث محلی برای دستگاه های بلوتوث جفت شده پرس و جو کنید.
- کانال های RFCOMM را ایجاد کنید.
- از طریق کشف سرویس به دستگاه های دیگر متصل شوید.
- انتقال داده به و از دستگاه های دیگر.
- چندین اتصال را مدیریت کنید
این موضوع بر روی بلوتوث کلاسیک تمرکز دارد. بلوتوث کلاسیک انتخاب مناسبی برای عملیاتهای با باتری فشردهتر است که شامل پخش جریانی و برقراری ارتباط بین دستگاهها میشود. برای دستگاههای بلوتوث با انرژی کم، از اتصالات کم انرژی بلوتوث استفاده کنید.
این مستند نمایههای مختلف بلوتوث را توضیح میدهد و نحوه استفاده از APIهای بلوتوث برای انجام چهار وظیفه اصلی لازم برای برقراری ارتباط با استفاده از بلوتوث را توضیح میدهد:
- راه اندازی بلوتوث
- یافتن دستگاه هایی که جفت شده اند یا در منطقه محلی موجود هستند.
- اتصال دستگاه ها
- انتقال داده ها بین دستگاه ها
برای نمایش استفاده از APIهای بلوتوث، به برنامه نمونه گپ بلوتوث مراجعه کنید.
اصول اولیه
برای اینکه دستگاههای دارای بلوتوث بتوانند دادهها را بین یکدیگر انتقال دهند، ابتدا باید یک کانال ارتباطی با استفاده از فرآیند جفتسازی ایجاد کنند. یک دستگاه، یک دستگاه قابل کشف، خود را برای درخواست های اتصال ورودی در دسترس قرار می دهد. دستگاه دیگری با استفاده از فرآیند کشف سرویس، دستگاه قابل کشف را پیدا می کند. پس از اینکه دستگاه قابل کشف درخواست جفت شدن را پذیرفت، دو دستگاه یک فرآیند پیوند را تکمیل می کنند که در آن کلیدهای امنیتی را مبادله می کنند. دستگاه ها این کلیدها را برای استفاده بعدی در حافظه پنهان ذخیره می کنند. پس از تکمیل فرآیندهای جفت و پیوند، دو دستگاه اطلاعات را مبادله می کنند. وقتی جلسه کامل شد، دستگاهی که درخواست جفتسازی را آغاز کرده است، کانالی را که آن را به دستگاه قابل شناسایی مرتبط کرده بود، آزاد میکند. با این حال، دو دستگاه به هم متصل میمانند، بنابراین تا زمانی که در محدوده یکدیگر قرار دارند و هیچیک از دستگاهها اتصال را حذف نکردهاند، میتوانند بهطور خودکار در جلسه آینده دوباره به هم متصل شوند.
استفاده از APIهای بلوتوث مستلزم اعلام چندین مجوز در فایل مانیفست شما است. هنگامی که برنامه شما مجوز استفاده از بلوتوث را دریافت کرد، برنامه شما باید به BluetoothAdapter
دسترسی داشته باشد و مشخص کند که آیا بلوتوث در دستگاه موجود است یا خیر . اگر بلوتوث در دسترس باشد، سه مرحله برای ایجاد اتصال وجود دارد:
برخی از دستگاهها از نمایه بلوتوث خاصی استفاده میکنند که دادههایی را که ارائه میکند را اعلام میکند.
کلاس ها و رابط های کلیدی
همه APIهای بلوتوث در بسته android.bluetooth
موجود هستند. کلاسها و رابطهایی که برای ایجاد اتصالات بلوتوث به آنها نیاز دارید، در زیر آمده است:
-
BluetoothAdapter
- نشان دهنده آداپتور بلوتوث محلی (رادیو بلوتوث) است.
BluetoothAdapter
نقطه ورود برای تمام تعاملات بلوتوث است. با استفاده از این، میتوانید سایر دستگاههای بلوتوث را کشف کنید، فهرستی از دستگاههای متصل (جفتشده) را جستجو کنید، یک BluetoothDevice
با استفاده از یک آدرس MAC شناختهشده نمونهسازی کنید، و یک BluetoothServerSocket
برای گوش دادن به ارتباطات سایر دستگاهها ایجاد کنید. -
BluetoothDevice
- یک دستگاه بلوتوث راه دور را نشان می دهد. از این برای درخواست اتصال با یک دستگاه راه دور از طریق
BluetoothSocket
یا جستجوی اطلاعات مربوط به دستگاه مانند نام، آدرس، کلاس و وضعیت اتصال آن استفاده کنید. -
BluetoothSocket
- نمایانگر رابط یک سوکت بلوتوث (شبیه به
Socket
TCP). این نقطه اتصال است که به یک برنامه اجازه می دهد تا با استفاده از InputStream
و OutputStream
با دستگاه بلوتوث دیگری تبادل داده کند. -
BluetoothServerSocket
- یک سوکت سرور باز را نشان می دهد که به درخواست های دریافتی گوش می دهد (شبیه به TCP
ServerSocket
). برای اتصال دو دستگاه، یک دستگاه باید یک سوکت سرور با این کلاس باز کند. هنگامی که یک دستگاه بلوتوث راه دور درخواست اتصال به این دستگاه را ارائه میکند، دستگاه اتصال را میپذیرد و سپس یک BluetoothSocket
متصل را برمیگرداند. -
BluetoothClass
- ویژگی ها و قابلیت های کلی یک دستگاه بلوتوث را شرح می دهد. این مجموعه ای از ویژگی های فقط خواندنی است که کلاس ها و سرویس های دستگاه را تعریف می کند. اگرچه این اطلاعات راهنمایی مفیدی در مورد نوع دستگاه ارائه می دهد، ویژگی های این کلاس لزوماً تمام نمایه ها و خدمات بلوتوثی را که دستگاه پشتیبانی می کند توصیف نمی کند.
-
BluetoothProfile
- رابطی که نمایانگر یک نمایه بلوتوث است. نمایه بلوتوث یک مشخصات رابط بی سیم برای ارتباط مبتنی بر بلوتوث بین دستگاه ها است. یک مثال نمایه هندزفری است. برای بحث بیشتر در مورد نمایهها، نمایههای بلوتوث را ببینید.
-
BluetoothHeadset
- پشتیبانی از هدست های بلوتوث برای استفاده با تلفن های همراه را فراهم می کند. این شامل پروفایل هدست بلوتوث و نمایه هندزفری (نسخه 1.5) می شود.
-
BluetoothA2dp
- نحوه پخش صدای با کیفیت بالا را از طریق اتصال بلوتوث با استفاده از نمایه توزیع صوتی پیشرفته (A2DP) از یک دستگاه به دستگاه دیگر تعریف می کند.
-
BluetoothHealth
- یک پروکسی نمایه دستگاه سلامت را نشان می دهد که سرویس بلوتوث را کنترل می کند.
-
BluetoothHealthCallback
- یک کلاس انتزاعی که از آن برای پیاده سازی تماس های
BluetoothHealth
استفاده می کنید. برای دریافت بهروزرسانیهای مربوط به تغییرات وضعیت ثبت برنامه و وضعیت کانال بلوتوث، باید این کلاس را گسترش دهید و روشهای پاسخ به تماس را پیادهسازی کنید. -
BluetoothHealthAppConfiguration
- نشاندهنده پیکربندی برنامهای است که برنامه شخص ثالث Bluetooth Health برای برقراری ارتباط با یک دستگاه سلامت بلوتوث از راه دور ثبت میکند.
-
BluetoothProfile.ServiceListener
- رابطی که مشتریان ارتباط بین فرآیندی
BluetoothProfile
(IPC) را هنگامی که به سرویس داخلی که نمایه خاصی را اجرا می کند متصل شده یا از آن جدا شده اند مطلع می کند.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی."],[],[],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."]]