gary-rowe / hid4java

A cross-platform Java Native Access (JNA) wrapper for the libusb/hidapi library. Works out of the box on Windows/Mac/Linux.
MIT License
223 stars 70 forks source link

I am not scanning my device using UsbHidEnumerationExample. Do I need to do anything to set the MAC? #129

Closed RKaixuan closed 1 year ago

RKaixuan commented 2 years ago

I am not scanning my device using UsbHidEnumerationExample. Do I need to do anything to set the MAC?

junction2412 commented 2 years ago

Well it worked fine for me in the underneath code.

import org.hid4java.*
import java.nio.ByteBuffer
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardOpenOption

fun main(args: Array<String>) {
    val path = Paths.get(System.getenv("USERPROFILE"), "Desktop", "hid", "test.txt")
    if (!Files.exists(path))
        Files.createFile(path)

    val arr: Array<HidDevice?> = Array(1) { null }

    for (device: HidDevice in HidManager.getHidServices().attachedHidDevices) {
        println("manufacturer: ${device.manufacturer}, product: ${device.product}, usage_page: ${device.usagePage}, usage: ${device.usage}")

        if (device.usagePage == 0x01 && device.usage == 0x05)
            arr[0] = device
    }

    if (arr[0] != null) {
        println("gamepad found\n")
        val gamepad: HidDevice = arr[0]!!
        gamepad.open()

        val start: Long = System.currentTimeMillis()

        do {
            val read: Array<Byte> = gamepad.read()!!
            val array = ByteArray(read.size)

            for (index in read.indices)
                array[index] = read[index]

            for (i in 0..(array.size / 8))
                print(String.format("%x ", ByteBuffer.wrap(array.copyOfRange(i, i + 8)).long))
            println()

            Files.write(
                path,
                array,
                StandardOpenOption.APPEND
            )
        } while (System.currentTimeMillis() - start <= 5_000)

        gamepad.close()
    }
}

Gotta admit I'm very impressed by this api, yet I still have to figure out how the input-reports work to be able to do something useful with it. 😅

RKaixuan commented 2 years ago

I have new problem, HipApi.open is returning null when I send information to the device for the second time

junction2412 commented 2 years ago

Did you close the former connection properly before opening it the 2nd time?

RKaixuan commented 2 years ago

Thinks, I got it

RKaixuan commented 1 year ago

Why 32-bit windows7 can't send data to the device, but can receive data

gary-rowe commented 1 year ago

Since this is an old question/answer support issue I'll close it for now.