סקירה כללית של Bluetooth
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
פלטפורמת Android כוללת תמיכה במערך פרוטוקולי Bluetooth, שמאפשר למכשיר להחליף נתונים באופן אלחוטי עם מכשירי Bluetooth אחרים. מסגרת האפליקציה מספקת גישה לפונקציונליות של Bluetooth באמצעות ממשקי Bluetooth API. ממשקי ה-API האלה מאפשרים לאפליקציות להתחבר למכשירי Bluetooth אחרים, וכך להשתמש בתכונות אלחוטיות של נקודה לנקודה ושל נקודות רבות.
באמצעות ממשקי ה-API של Bluetooth, אפליקציה יכולה לבצע את הפעולות הבאות:
- סורקים כדי למצוא מכשירי Bluetooth אחרים.
- שאילתה של מתאם ה-Bluetooth המקומי לגבי מכשירי Bluetooth שהותאמו.
- יצירת ערוצי RFCOMM.
- מתחברים למכשירים אחרים באמצעות חיפוש שירותים.
- העברת נתונים למכשירים אחרים וממכשירים אחרים.
- ניהול של כמה חיבורים.
הנושא הזה מתמקד בBluetooth קלאסי. חיבור Bluetooth קלאסי הוא הבחירה הנכונה לפעולות שצורכות יותר סוללה, כולל סטרימינג ותקשורת בין מכשירים. למכשירי Bluetooth עם דרישות הספק נמוכות, כדאי להשתמש בחיבורי Bluetooth Low Energy.
במסמך הזה מתוארים פרופילים שונים של Bluetooth ומוסבר איך להשתמש בממשקי ה-API של Bluetooth כדי לבצע את ארבע המשימות העיקריות שנדרשות לתקשורת באמצעות Bluetooth:
- הגדרת Bluetooth.
- חיפוש מכשירים שמשויכים או זמינים באזור המקומי.
- חיבור המכשירים.
- העברת נתונים בין מכשירים.
כדי לראות הדגמה של שימוש בממשקי ה-API של Bluetooth, אפשר לעיין באפליקציית הדוגמה Bluetooth Chat.
העקרונות הבסיסיים
כדי שמכשירים עם Bluetooth יוכלו להעביר נתונים ביניהם, הם צריכים קודם ליצור ערוץ תקשורת באמצעות תהליך התאמה. מכשיר אחד, מכשיר שאפשר לגלות אותו, הופך לזמין לבקשות חיבור נכנסות.
מכשיר אחר מוצא את המכשיר שניתן לגילוי באמצעות תהליך גילוי שירותים.
אחרי שהמכשיר שניתן לגלות אותו מאשר את בקשת ההתאמה, שני המכשירים משלימים תהליך שבו הם מחליפים מפתחות אבטחה. המכשירים שומרים את המפתחות האלה במטמון לשימוש מאוחר יותר. אחרי שתהליכי ההתאמה והקישור יסתיימו, שני המכשירים יחליפו מידע ביניהם. כשהסשן מסתיים, המכשיר ששלח את בקשת ההתאמה משחרר את הערוץ שקישר אותו למכשיר שאפשר לגלות. עם זאת, שני המכשירים יישארו מקושרים, כך שהם יוכלו להתחבר מחדש באופן אוטומטי במהלך הפעלה עתידית, כל עוד הם נמצאים בטווח אחד של השני ואף אחד מהמכשירים לא הסיר את הקישור.
כדי להשתמש ב-Bluetooth API, צריך להצהיר על כמה הרשאות בקובץ המניפסט. אחרי שהאפליקציה מקבלת הרשאה להשתמש ב-Bluetooth, היא צריכה לגשת אל BluetoothAdapter
ולקבוע אם Bluetooth זמין במכשיר.
אם Bluetooth זמין, יש שלושה שלבים לחיבור:
במכשירים מסוימים נעשה שימוש בפרופיל Bluetooth
ספציפי שמצהיר על הנתונים שהוא מספק.
ממשקי ושיעורי מפתח
כל ממשקי ה-API של Bluetooth זמינים בחבילה android.bluetooth
.
אלה המחלקות והממשקים שצריך כדי ליצור חיבורי Bluetooth:
BluetoothAdapter
- מייצג את מתאם ה-Bluetooth המקומי (רדיו Bluetooth).
BluetoothAdapter
היא נקודת הכניסה לכל האינטראקציות עם Bluetooth. באמצעות האפשרות הזו, אפשר לגלות מכשירי Bluetooth אחרים, לשלוח שאילתה לגבי רשימה של מכשירים מקושרים (מזווגים), ליצור מופע של BluetoothDevice
באמצעות כתובת MAC ידועה וליצור BluetoothServerSocket
כדי להאזין לתקשורת ממכשירים אחרים.
BluetoothDevice
- מייצג מכשיר Bluetooth מרוחק. אפשר להשתמש בזה כדי לבקש חיבור למכשיר מרוחק דרך
BluetoothSocket
או כדי לשאול על פרטי המכשיר, כמו השם, הכתובת, הסיווג ומצב הקישור שלו.
BluetoothSocket
- מייצג את הממשק לשקע Bluetooth (בדומה ל-TCP
Socket
). זוהי נקודת החיבור שמאפשרת לאפליקציה להחליף נתונים עם מכשיר Bluetooth אחר באמצעות InputStream
ו-OutputStream
.
BluetoothServerSocket
- מייצג שקע שרת פתוח שמקשיב לבקשות נכנסות (בדומה ל-TCP
ServerSocket
). כדי לחבר שני מכשירים, מכשיר אחד צריך לפתוח שקע שרת באמצעות המחלקה הזו. כשמכשיר Bluetooth מרוחק שולח בקשת חיבור למכשיר הזה, המכשיר מאשר את החיבור ומחזיר BluetoothSocket
מחובר.
BluetoothClass
- מתאר את המאפיינים והיכולות הכלליים של מכשיר Bluetooth.
זוהי קבוצה לקריאה בלבד של מאפיינים שמגדירה את המחלקות והשירותים של המכשיר. למרות שהמידע הזה מספק רמז שימושי לגבי סוג המכשיר, המאפיינים של הסיווג הזה לא מתארים בהכרח את כל הפרופילים והשירותים של Bluetooth שהמכשיר תומך בהם.
BluetoothProfile
- ממשק שמייצג פרופיל Bluetooth. פרופיל Bluetooth הוא מפרט של ממשק אלחוטי לתקשורת מבוססת Bluetooth בין מכשירים. דוגמה לכך היא פרופיל הדיבורית. מידע נוסף על פרופילים זמין במאמר פרופילים של Bluetooth.
BluetoothHeadset
- תמיכה באוזניות Bluetooth לשימוש עם טלפונים ניידים. הפרופיל הזה כולל את פרופיל אוזניות ה-Bluetooth ואת פרופיל הדיבורית (גרסה 1.5).
BluetoothA2dp
- הפרופיל מגדיר איך אפשר להזרים אודיו באיכות גבוהה ממכשיר אחד למכשיר אחר באמצעות חיבור Bluetooth, באמצעות פרופיל מתקדם להפצת אודיו (A2DP).
BluetoothHealth
- מייצג פרופיל של מכשיר בריאות (Health Device Profile) ששולט בשירות Bluetooth.
BluetoothHealthCallback
- מחלקה מופשטת שמשמשת להטמעה של קריאות חוזרות (callbacks) מסוג
BluetoothHealth
. חובה להרחיב את המחלקה הזו ולהטמיע את שיטות הקריאה החוזרת כדי לקבל עדכונים לגבי שינויים במצב הרישום של האפליקציה ובמצב של ערוץ ה-Bluetooth.
BluetoothHealthAppConfiguration
- מייצג הגדרת אפליקציה שאפליקציית צד שלישי של Bluetooth Health נרשמת אליה כדי לתקשר עם מכשיר Bluetooth Health מרוחק.
BluetoothProfile.ServiceListener
- ממשק שמיידע לקוחות של
BluetoothProfile
תקשורת בין תהליכים (IPC) כשהם מחוברים לשירות הפנימי שמפעיל פרופיל מסוים או מנותקים ממנו.
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. Java ו-OpenJDK הם סימנים מסחריים או סימנים מסחריים רשומים של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-07-27 (שעון UTC).
[[["התוכן קל להבנה","easyToUnderstand","thumb-up"],["התוכן עזר לי לפתור בעיה","solvedMyProblem","thumb-up"],["סיבה אחרת","otherUp","thumb-up"]],[["חסרים לי מידע או פרטים","missingTheInformationINeed","thumb-down"],["התוכן מורכב מדי או עם יותר מדי שלבים","tooComplicatedTooManySteps","thumb-down"],["התוכן לא עדכני","outOfDate","thumb-down"],["בעיה בתרגום","translationIssue","thumb-down"],["בעיה בדוגמאות/בקוד","samplesCodeIssue","thumb-down"],["סיבה אחרת","otherDown","thumb-down"]],["עדכון אחרון: 2025-07-27 (שעון 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."]]