自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
觸控筆
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 6.0 以上版本支援透過藍牙 (BT)、藍牙低功耗 (BTLE) 或 USB 連線的藍牙觸控筆標準資料格式。平台會將觸控輸入與觸控筆資料的時間點連結,然後提供觸控筆資料,將 MotionEvents 算繪至有效應用程式。以下各節提供 OEM 裝置導入者、觸控筆配件建立者和觸控筆應用程式開發人員的規範。
原始設備製造商 (OEM) 裝置實作者指南
如要啟用藍牙觸控筆支援功能,原始設備製造商 (OEM) 裝置實作者必須支援藍牙 (並應支援 BTLE 以便提高相容性)。平台會處理資料收集、時間相關性,並將支援的觸控筆事件算繪至應用程式。
在 14 以上版本中,StylusButtonInputEventTest
會模擬已連結觸控筆的行為,並測試按鈕事件是否正確回報。針對較舊版本,我們建議您建立可模擬觸控筆事件的觸控筆配件或模擬器,做為解決方法。
觸控筆配件製造商指南
如要在觸控筆裝置上實作支援功能,裝置實作者必須使用下方的觸控筆人機介面裝置 (HID) 描述符,說明如何呈現觸控筆資料 (壓力靈敏度、橡皮擦、按鈕、裝置 ID、電池電量、充電狀態等)。觸控筆裝置會將 HID 資訊傳送至 Android 行動裝置,讓平台將 HID 資料與觸控螢幕的觸控資料建立關聯,以便使用 MotionEvent
類別產生觸控筆事件。在 Android 14 以上版本中,觸控筆的按鈕按壓也會使用 KeyEvent
類別產生內部事件。資料可透過藍牙 (BT)、藍牙低功耗 (BTLE) 或 USB 傳送。
HID 描述元範例
以下是 HID 描述元檔案範例,可回報筆尖接觸、壓力、主要和次要按鈕、橡皮擦和序號。您所附加的人機介面裝置描述元應準確反映觸控筆透過藍牙回報的內容,這可能會是可用功能的任何子集。
UsagePage(Digitizer)
Usage(Pen)
Collection(Application)
Usage(Stylus)
Collection(Logical)
Usage(Tip Pressure)
Logical Minimum(0)
Logical Maximum(1023)
Report Count(1)
Report Size(10)
Input(Data, Variable, Absolute, No Null)
Usage(Barrel Switch)
Usage(Secondary Barrel Switch)
Usage(Tip Switch)
Usage(Invert)
Logical Maximum(1)
Report Count(4)
Report Size(1)
Input(Data, Variable, Absolute, No Null)
Usage(Transducer Serial Number)
Report Count(1)
Report Size(128)
Feature(Constant, Variable)
EndCollection
EndCollection
unsigned char HID_DESC[] = {
0x05, 0x0D, // UsagePage(Digitizer)
0x09, 0x02, // Usage(Pen)
0xA1, 0x01, // Collection(Application)
0x09, 0x20, // Usage(Stylus)
0xA1, 0x02, // Collection(Logical)
0x09, 0x30, // Usage(Tip Pressure)
0x15, 0x00, // Logical Minimum(0)
0x26, 0xFF, 0x03, // Logical Maximum(1023)
0x95, 0x01, // Report Count(1)
0x75, 0x0A, // Report Size(10)
0x81, 0x02, // Input(Data, Variable, Absolute, No Null)
0x09, 0x44, // Usage(Barrel Switch)
0x09, 0x5A, // Usage(Secondary Barrel Switch)
0x09, 0x42, // Usage(Tip Switch)
0x09, 0x3C, // Usage(Invert)
0x25, 0x01, // Logical Maximum(1)
0x95, 0x04, // Report Count(4)
0x75, 0x01, // Report Size(1)
0x81, 0x02, // Input(Data, Variable, Absolute, No Null)
0x09, 0x5B, // Usage(Transducer Serial Number)
0x95, 0x01, // Report Count(1)
0x75, 0x80, // Report Size(128)
0xB1, 0x03, // Feature(Constant, Variable)
0xC0, // End Collection
0xC0, // End Collection
}
觸控筆應用程式開發人員指南
Android 平台會自動處理配對和事件關聯,因此現有和新應用程式都會預設支援藍牙觸控筆。如要進一步瞭解如何在 Android 應用程式中支援觸控筆,請參閱 Android 開發人員說明文件。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","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 (世界標準時間)。"],[],[],null,["# Stylus\n\nAndroid 6.0 and higher supports a standard data format for Bluetooth stylus\nconnections over Bluetooth (BT), Bluetooth Low Energy (BTLE), or USB. The\nplatform correlates timing between touch input and stylus data then provides\nstylus data to render MotionEvents to the active app. The following\nsections provide guidelines for OEM device implementers, stylus accessory creators, and\nstylus app developers.\n\nGuidelines for OEM device implementers\n--------------------------------------\n\nTo enable Bluetooth stylus support, OEM device implementers must support Bluetooth\n(and should support BTLE for wider compatibility). The platform handles data\ncollection, timing correlation, and rendering to the app for supported\nstylus events.\n\nIn 14 and higher,\n`StylusButtonInputEventTest` emulates the behavior of a connected stylus and tests\nthat button events are reported correctly. As a\nworkaround for lower versions, we recommend creating a stylus accessory or emulator that can\nsimulate stylus events.\n\nGuidelines for stylus accessory creators\n----------------------------------------\n\nTo implement support on a stylus device, device implementers must use the Stylus\nHuman Interface Device (HID) Descriptor shown below to describe how stylus data\n(pressure sensitivity, eraser, buttons, device ID, battery level, charging status, etc.) is\nrepresented. The stylus device sends the HID information to the Android mobile device,\nenabling the platform to correlate HID data with touch data from the touchscreen\nto produce stylus events using the\n[`MotionEvent`](//developer.android.com/reference/android/view/MotionEvent)\nclass. In Android 14 and higher, button presses on the stylus also generate\ninternal events using the\n[`KeyEvent`](//developer.android.com/reference/android/view/KeyEvent) class.\nData can be sent over Bluetooth (BT), Bluetooth Low Energy (BTLE), or USB.\n\n### Example HID descriptor\n\nThe following sample HID descriptor reports tip contact, pressure, primary and secondary buttons, eraser, and serial number. The HID descriptor you include should accurately reflect what is being reported by the stylus through bluetooth, which could be any subset of the available capabilities. \n\n```\nUsagePage(Digitizer)\nUsage(Pen)\nCollection(Application)\n Usage(Stylus)\n Collection(Logical)\n Usage(Tip Pressure)\n Logical Minimum(0)\n Logical Maximum(1023)\n Report Count(1)\n Report Size(10)\n Input(Data, Variable, Absolute, No Null)\n\n Usage(Barrel Switch)\n Usage(Secondary Barrel Switch)\n Usage(Tip Switch)\n Usage(Invert)\n Logical Maximum(1)\n Report Count(4)\n Report Size(1)\n Input(Data, Variable, Absolute, No Null)\n\n Usage(Transducer Serial Number)\n Report Count(1)\n Report Size(128)\n Feature(Constant, Variable)\n EndCollection\nEndCollection\n\nunsigned char HID_DESC[] = {\n 0x05, 0x0D, // UsagePage(Digitizer)\n 0x09, 0x02, // Usage(Pen)\n 0xA1, 0x01, // Collection(Application)\n 0x09, 0x20, // Usage(Stylus)\n 0xA1, 0x02, // Collection(Logical)\n 0x09, 0x30, // Usage(Tip Pressure)\n 0x15, 0x00, // Logical Minimum(0)\n 0x26, 0xFF, 0x03, // Logical Maximum(1023)\n 0x95, 0x01, // Report Count(1)\n 0x75, 0x0A, // Report Size(10)\n 0x81, 0x02, // Input(Data, Variable, Absolute, No Null)\n\n 0x09, 0x44, // Usage(Barrel Switch)\n 0x09, 0x5A, // Usage(Secondary Barrel Switch)\n 0x09, 0x42, // Usage(Tip Switch)\n 0x09, 0x3C, // Usage(Invert)\n 0x25, 0x01, // Logical Maximum(1)\n 0x95, 0x04, // Report Count(4)\n 0x75, 0x01, // Report Size(1)\n 0x81, 0x02, // Input(Data, Variable, Absolute, No Null)\n\n 0x09, 0x5B, // Usage(Transducer Serial Number)\n 0x95, 0x01, // Report Count(1)\n 0x75, 0x80, // Report Size(128)\n 0xB1, 0x03, // Feature(Constant, Variable)\n 0xC0, // End Collection\n 0xC0, // End Collection\n}\n```\n\nGuidelines for stylus app developers\n------------------------------------\n\nThe Android platform automatically handles pairing and event correlation,\nso both existing and new apps support Bluetooth stylus by default. To learn more about\nsupporting styluses in Android apps, see the\n[Android Developer documentation](https://developer.android.com/develop/ui/views/touch-and-input/stylus)."]]