Android: Programmatically detect if device has hardware touchscreen connected

后端 未结 2 1750
北恋
北恋 2021-02-18 15:44

I need to write a script to detect if the physical touchscreen is connected to my Android device at boot time. I tried to list the content of the folder /dev/input

相关标签:
2条回答
  • 2021-02-18 16:16

    Find a driver name for the touch controller of your device. Then check its sysfs location. There will be few files mapped to the internal variables which were populated with data read from the physical touchscreen device during its initialization. For example most touchscreen controllers have updateable firmware and provide a way to query its current version.

    One of my devices uses atmel_mxt_ts touchscreen controller and its sysfs location is /sys/bus/i2c/drivers/atmel_mxt_ts/1-004a/. There is a fw_version file in that folder. If the physical touchscreen is connected that file would contain the current firmware label. The empty file would mean that there is no touchscreen.

    0 讨论(0)
  • 2021-02-18 16:18

    You can read /proc/bus/input/devices to get details of your existing input devices.

    Depending on your hardware's name, you could do something like that and check if there is any output:

    cat /proc/bus/input/devices | grep "Name=" | grep "Touch"
    

    This is the full output of /proc/bus/input/devices:

    I: Bus=0011 Vendor=0002 Product=0008 Version=2222
    N: Name="AlpsPS/2 ALPS DualPoint TouchPad"
    P: Phys=isa0060/serio1/input0
    S: Sysfs=/class/input/input2
    H: Handlers=mouse1 event2 ts1
    B: EV=f
    B: KEY=420 0 70000 0 0 0 0 0 0 0 0
    B: REL=3
    B: ABS=1000003
    
    [...] (blank line, next device)
    

    The B in front stands for bitmap, N, P, S, U, H are simply first letter in corresponding name value and I is for ID. In ordered fashion:

    • I → @id: id of the device (struct input_id)
      • Bus → id.bustype
      • Vendor → id.vendor
      • Product → id.product
      • Version → id.version
    • N → name of the device
    • P → physical path to the device in the system hierarchy
    • S → sysfs path
    • U → unique identification code for the device (if device has it)
    • H → list of input handles associated with the device
    • B → bitmaps
      • PROP → device properties and quirks
      • EV → types of events supported by the device
      • KEY → keys/buttons this device has
      • MSC → miscellaneous events supported by the device
      • LED → leds present on the device
      • REL → relative address
      • ABS → absolute address

    To test if the device is actually attached, you can try simulating events and see if you get any errors:

    input tap [x] [y]
    

    Android comes with an input command-line tool that can simulate miscellaneous input events.

    • input → The command line tool to send events
      • tap → the action
      • [x] → X coordinate on the screen
      • [y] → Y coordinate on the screen
    0 讨论(0)
提交回复
热议问题