自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
触控笔
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android 6.0 及更高版本支持蓝牙 (BT)、蓝牙低功耗 (BTLE) 或 USB 协议的蓝牙触控笔连接适用的标准数据格式。平台会分析触控输入和触控笔数据之间的时间关系并进行关联,然后提供触控笔数据,以便在前台应用中渲染 MotionEvent。下面几个部分提供的指南适用于原始设备制造商 (OEM) 设备实现者、触控笔配件制造者和触控笔应用开发者。
面向 OEM 设备实现者的指南
如需启用蓝牙触控笔支持,OEM 设备实现者必须支持蓝牙(并且应支持 BTLE 以实现更广泛的兼容性)。平台会针对支持的触控笔事件处理数据收集、时间关联操作,以及应用呈现。
在 14 及更高版本中,StylusButtonInputEventTest
会模拟已连接的触控笔的行为,并测试是否正确报告按钮事件。作为低版本的解决方法,我们建议您构建触控笔配件或可模拟触控笔事件的模拟器。
面向触控笔配件制造者的指南
如需在触控笔设备上实现支持,设备实现者必须使用下方所示的触控笔人机接口设备 (HID) 描述符,来描述触控笔数据(压力感应、橡皮擦、按钮、设备 ID、电池电量、充电状态等)的表示方式。触控笔设备会将 HID 信息发送到 Android 移动设备,使平台能够将 HID 数据与触摸屏的触摸数据相关联,以使用 MotionEvent
类产生触控笔事件。在 Android 14 及更高版本中,按下触控笔的按钮也会使用 KeyEvent
类生成内部事件。数据可通过蓝牙 (BT)、蓝牙低功耗 (BTLE) 或 USB 发送。
HID 描述符示例
以下示例 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 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[null,null,["最后更新时间 (UTC):2025-03-26。"],[],[],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)."]]