Skip to content

Commit

Permalink
Updated to comply with the API change
Browse files Browse the repository at this point in the history
  • Loading branch information
nyholku committed Sep 24, 2016
1 parent d68505c commit 4388fde
Showing 1 changed file with 66 additions and 45 deletions.
111 changes: 66 additions & 45 deletions example/Example2.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,81 @@
import purejavahidapi.*;

public class Example2 {
volatile static boolean deviceOpen = false;

public static void main(String[] args) {
try {

//System.exit(0);

List<HidDeviceInfo> devList = PureJavaHidApi.enumerateDevices();
HidDeviceInfo devInfo = null;
for (HidDeviceInfo info : devList) {
if (info.getVendorId() == (short) 0x16C0 && info.getProductId() == (short) 0x05DF) {
devInfo = info;
break;
}
}
if (devInfo == null)
System.err.println("device not found");
else {
final HidDevice dev = PureJavaHidApi.openDevice(devInfo.getPath());
dev.setDeviceRemovalListener(new DeviceRemovalListener() {
@Override
public void onDeviceRemoval(HidDevice source) {
System.out.println("onDeviceRemoval");
}
});
dev.setInputReportListener(new InputReportListener() {
@Override
public void onInputReport(HidDevice source, byte Id, byte[] data, int len) {
System.out.printf("onInputReport: id %d len %d data ", Id, len);
for (int i = 0; i < len; i++)
System.out.printf("%02X ", data[i]);
System.out.println();
while (true) {
// System.exit(0);
HidDeviceInfo devInfo = null;
if (!deviceOpen) {
System.out.println("scanning");
List<HidDeviceInfo> devList = PureJavaHidApi.enumerateDevices();
for (HidDeviceInfo info : devList) {
// if (info.getVendorId() == (short) 0x16C0 &&
// info.getProductId() == (short) 0x05DF) {
//if (info.getVendorId() == (short) 0x16C0 && info.getProductId() == (short) 0x0a99) {
if (info.getVendorId() == (short) 0x16D0 && info.getProductId() == (short) 0x044B) {
devInfo = info;
break;
}
}
});
if (devInfo == null) {
System.out.println("device not found");
Thread.sleep(1000);
} else {
System.out.println("device found");
if (true) {
deviceOpen = true;
final HidDevice dev = PureJavaHidApi.openDevice(devInfo);

new Thread(new Runnable() {
@Override
public void run() {
while (true) {
byte[] data = new byte[132];
data[0] = 1;
int len = 0;
if (((len = dev.getFeatureReport(data, data.length)) >= 0)) {
int Id=data[0];
System.out.printf("getFeatureReport: id %d len %d data ", Id, len);
for (int i = 0; i < data.length; i++)
System.out.printf("%02X ", data[i]);
System.out.println();
}
}
dev.setDeviceRemovalListener(new DeviceRemovalListener() {
@Override
public void onDeviceRemoval(HidDevice source) {
System.out.println("device removed");
deviceOpen = false;

}
});
dev.setInputReportListener(new InputReportListener() {
@Override
public void onInputReport(HidDevice source, byte Id, byte[] data, int len) {
System.out.printf("onInputReport: id %d len %d data ", Id, len);
for (int i = 0; i < len; i++)
System.out.printf("%02X ", data[i]);
System.out.println();
}
});

new Thread(new Runnable() {
@Override
public void run() {
while (true) {
byte[] data = new byte[132];
data[0] = 1;
int len = 0;
if (((len = dev.getFeatureReport(data, data.length)) >= 0) && true) {
int Id = data[0];
System.out.printf("getFeatureReport: id %d len %d data ", Id, len);
for (int i = 0; i < data.length; i++)
System.out.printf("%02X ", data[i]);
System.out.println();
}

}

}
}).start();

Thread.sleep(2000);
//dev.close();
//deviceOpen = false;
}
}
}).run();
}
}
} catch (Exception e) {
} catch (Throwable e) {
e.printStackTrace();
}

Expand Down

0 comments on commit 4388fde

Please sign in to comment.