下面列出了android.hardware.usb.UsbDevice#getProductId ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static public boolean isG4Connected(Context c){
UsbManager manager = (UsbManager) c.getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Log.i("USB DEVICES = ", deviceList.toString());
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
Log.i("USB DEVICES = ", String.valueOf(deviceList.size()));
while(deviceIterator.hasNext()){
UsbDevice device = deviceIterator.next();
if (device.getVendorId() == 8867 && device.getProductId() == 71
&& device.getDeviceClass() == 2 && device.getDeviceSubclass() ==0
&& device.getDeviceProtocol() == 0){
Log.i("CALIBRATION-CHECK-IN: ", "Dexcom Found!");
return true;
}
}
return false;
}
static public boolean isG4Connected(Context c){
UsbManager manager = (UsbManager) c.getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Log.w("USB DEVICES = ", deviceList.toString());
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
Log.w("USB DEVICES = ", String.valueOf(deviceList.size()));
while(deviceIterator.hasNext()){
UsbDevice device = deviceIterator.next();
if (device.getVendorId() == 8867 && device.getProductId() == 71
&& device.getDeviceClass() == 2 && device.getDeviceSubclass() ==0
&& device.getDeviceProtocol() == 0){
Log.w("CALIBRATION-CHECK-IN: ", "Dexcom Found!");
return true;
}
}
return false;
}
/**
* 指定したUsbDeviceがこのDeviceFilterにマッチするかどうかを返す
* isExcludeフラグは別途#isExcludeか自前でチェックすること
* @param device
* @return
*/
public boolean matches(@NonNull final UsbDevice device) {
if (mVendorId != -1 && device.getVendorId() != mVendorId) {
return false;
}
if (mProductId != -1 && device.getProductId() != mProductId) {
return false;
}
// check device class/subclass/protocol
if (matches(
device.getDeviceClass(),
device.getDeviceSubclass(),
device.getDeviceProtocol())) {
return true;
}
// check device interface class/interface subclass/interface protocol
return interfaceMatches(device);
}
public static boolean isSupported(UsbDevice device)
{
int vid = device.getVendorId();
int pid = device.getProductId();
if(FTDISioIds.isDeviceSupported(device))
return true;
else if(CP210xIds.isDeviceSupported(vid, pid))
return true;
else if(PL2303Ids.isDeviceSupported(vid, pid))
return true;
else if(CH34xIds.isDeviceSupported(vid, pid))
return true;
else if(isCdcDevice(device))
return true;
else
return false;
}
public UsbDevice findDexcom() {
Log.i("CALIBRATION-CHECK-IN: ", "Searching for dexcom");
mUsbManager = (UsbManager) getApplicationContext().getSystemService(Context.USB_SERVICE);
Log.i("USB MANAGER = ", mUsbManager.toString());
HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
Log.i("USB DEVICES = ", deviceList.toString());
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
Log.i("USB DEVICES = ", String.valueOf(deviceList.size()));
while(deviceIterator.hasNext()){
UsbDevice device = deviceIterator.next();
if (device.getVendorId() == 8867 && device.getProductId() == 71
&& device.getDeviceClass() == 2 && device.getDeviceSubclass() ==0
&& device.getDeviceProtocol() == 0){
dexcom = device;
Log.i("CALIBRATION-CHECK-IN: ", "Dexcom Found!");
return device;
} else {
Log.w("CALIBRATION-CHECK-IN: ", "that was not a dexcom (I dont think)");
}
}
return null;
}
public boolean matches(final UsbDevice device) {
if (mVendorId != -1 && device.getVendorId() != mVendorId)
return false;
if (mProductId != -1 && device.getProductId() != mProductId)
return false;
/* if (mManufacturerName != null && device.getManufacturerName() == null)
return false;
if (mProductName != null && device.getProductName() == null)
return false;
if (mSerialNumber != null && device.getSerialNumber() == null)
return false;
if (mManufacturerName != null && device.getManufacturerName() != null
&& !mManufacturerName.equals(device.getManufacturerName()))
return false;
if (mProductName != null && device.getProductName() != null
&& !mProductName.equals(device.getProductName()))
return false;
if (mSerialNumber != null && device.getSerialNumber() != null
&& !mSerialNumber.equals(device.getSerialNumber()))
return false; */
// check device class/subclass/protocol
if (matches(device.getDeviceClass(), device.getDeviceSubclass(),
device.getDeviceProtocol()))
return true;
// if device doesn't match, check the interfaces
final int count = device.getInterfaceCount();
for (int i = 0; i < count; i++) {
final UsbInterface intf = device.getInterface(i);
if (matches(intf.getInterfaceClass(), intf.getInterfaceSubclass(),
intf.getInterfaceProtocol()))
return true;
}
return false;
}
private static boolean isRoyaleDevice(final UsbDevice device) {
final int vid = device.getVendorId();
final int pid = device.getProductId();
if (vid != ROYALE_VENDOR_ID) {
return false;
}
for (int id : ROYALE_PRODUCT_IDS) {
if (id == pid)
return true;
}
return false;
}
public static UsbDevice getDevice(UsbManager manager) {
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
for (UsbDevice device : deviceList.values()) {
if ((device.getVendorId() == VID || device.getVendorId() == VID2) &&
((device.getProductId() == PID_WINUSB) || (device.getProductId() == PID_HID) ||
(device.getProductId() == PID_NANOS) || (device.getProductId() == PID_BLUE) ||
(device.getProductId() == PID_NANOX) || (device.getProductId() == PID_HID_LEDGER) ||
(device.getProductId() == PID_HID_LEDGER_PROTON))) {
return device;
}
}
return null;
}
/**
* Updates the list of available devices in the list preference
*/
private void updateDevicesList() {
HashMap<String, UsbDevice> connectedUsbDevices = usbManager.getDeviceList();
String[] entryValues = new String[connectedUsbDevices.size()];
String[] entries = new String[connectedUsbDevices.size()];
int i = 0;
// Loop through usb devices
for (UsbDevice device : connectedUsbDevices.values()) {
// Add the name and address to the ListPreference entities and entyValues
String entryValue = device.getDeviceName() +
" - " + device.getVendorId() + " : " + device.getProductId();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
entryValue = device.getManufacturerName() + " " + device.getProductName() +
" - " + device.getVendorId() + " : " + device.getProductId();
}
entryValues[i] = device.getDeviceName();
entries[i] = entryValue;
i++;
}
devicePreference.setEntryValues(entryValues);
devicePreference.setEntries(entries);
}
/**
* @return the radio device
*/
private UsbDevice getDevice() {
for (UsbDevice device : usbManager.getDeviceList().values()) {
if (device.getProductId() == RadioDevice.PRODUCT_ID &&
device.getVendorId() == RadioDevice.VENDOR_ID) {
return device;
}
}
return null;
}
public static UsbDevice getUsbDevice(final int vendorId, final int productId, final String search) {
final UsbManager manager = (UsbManager) xdrip.getAppContext().getSystemService(Context.USB_SERVICE);
if (manager == null) return null;
final HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
for (Map.Entry<String, UsbDevice> entry : deviceList.entrySet()) {
final UsbDevice device = entry.getValue();
if (device.getVendorId() == vendorId && device.getProductId() == productId
&& device.toString().contains(search)) {
Log.d(TAG, "Found device: " + entry.getKey() + " " + device.toString());
return device;
}
}
return null;
}
public static UsbDevice getUsbDevice(UsbManager usbManager, int vendorId, int productId) {
// Iterate all the available devices and find ours.
for (UsbDevice device : usbManager.getDeviceList().values()) {
if (device.getProductId() == productId && device.getVendorId() == vendorId) {
return device;
}
}
return null;
}
public static UsbDevice getDevice(UsbManager manager) {
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
for (UsbDevice device : deviceList.values()) {
if ((device.getVendorId() == VID || device.getVendorId() == VID2) &&
((device.getProductId() == PID_WINUSB) || (device.getProductId() == PID_HID) ||
(device.getProductId() == PID_NANOS) || (device.getProductId() == PID_BLUE) ||
(device.getProductId() == PID_HID_LEDGER) || (device.getProductId() == PID_HID_LEDGER_PROTON))) {
return device;
}
}
return null;
}
@SuppressLint("NewApi")
public DeviceFilter(@NonNull final UsbDevice device, final boolean isExclude) {
mVendorId = device.getVendorId();
mProductId = device.getProductId();
mClass = device.getDeviceClass();
mSubclass = device.getDeviceSubclass();
mProtocol = device.getDeviceProtocol();
// getInterfaceCountは内部配列のlengthを返すので負にはならないはずだけど年のために下限を0にする
final int count = Math.max(device.getInterfaceCount(), 0);
mIntfClass = new int[count];
mIntfSubClass = new int[count];
mIntfProtocol = new int[count];
for (int i = 0; i < count; i++) {
final UsbInterface intf = device.getInterface(i);
mIntfClass[i] = intf.getInterfaceClass();
mIntfSubClass[i] = intf.getInterfaceSubclass();
mIntfProtocol[i] = intf.getInterfaceProtocol();
}
if (BuildCheck.isLollipop()) {
mManufacturerName = device.getManufacturerName();
mProductName = device.getProductName();
mSerialNumber = device.getSerialNumber();
} else {
mManufacturerName = null;
mProductName = null;
mSerialNumber = null;
}
this.isExclude = isExclude;
}
/**
* 指定したUsbDeviceがこのDeviceFilterにマッチするかどうかを返す
* mExcludeフラグは別途#isExcludeか自前でチェックすること
* @param device
* @return
*/
public boolean matches(final UsbDevice device) {
if (mVendorId != -1 && device.getVendorId() != mVendorId) {
return false;
}
if (mProductId != -1 && device.getProductId() != mProductId) {
return false;
}
/* if (mManufacturerName != null && device.getManufacturerName() == null)
return false;
if (mProductName != null && device.getProductName() == null)
return false;
if (mSerialNumber != null && device.getSerialNumber() == null)
return false;
if (mManufacturerName != null && device.getManufacturerName() != null
&& !mManufacturerName.equals(device.getManufacturerName()))
return false;
if (mProductName != null && device.getProductName() != null
&& !mProductName.equals(device.getProductName()))
return false;
if (mSerialNumber != null && device.getSerialNumber() != null
&& !mSerialNumber.equals(device.getSerialNumber()))
return false; */
// check device class/subclass/protocol
if (matches(device.getDeviceClass(), device.getDeviceSubclass(), device.getDeviceProtocol())) {
return true;
}
// if device doesn't match, check the interfaces
final int count = device.getInterfaceCount();
for (int i = 0; i < count; i++) {
final UsbInterface intf = device.getInterface(i);
if (matches(intf.getInterfaceClass(), intf.getInterfaceSubclass(), intf.getInterfaceProtocol())) {
return true;
}
}
return false;
}
public static UsbDevice getDevice(UsbManager manager) {
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
for (UsbDevice device : deviceList.values()) {
if ((device.getVendorId() == VID || device.getVendorId() == VID2) &&
((device.getProductId() == PID_WINUSB) || (device.getProductId() == PID_HID) ||
(device.getProductId() == PID_NANOS) || (device.getProductId() == PID_BLUE) ||
(device.getProductId() == PID_HID_LEDGER) || (device.getProductId() == PID_HID_LEDGER_PROTON))) {
return device;
}
}
return null;
}
private UsbDevice getDeviceFromAttached() {
debugLog("Checking all connected devices");
for (UsbDevice connectedDevice : usbManager.getDeviceList().values()) {
debugLog("Checking device: " + connectedDevice.getProductId() + " " + connectedDevice.getVendorId());
if (connectedDevice.getVendorId() == gpsVendorId & connectedDevice.getProductId() == gpsProductId) {
debugLog("Found correct device");
return connectedDevice;
}
}
return null;
}
public static List<UsbDevice> findUsbDevices(UsbManager usbManager, int vendorId, int productId) {
List<UsbDevice> usbDeviceList = new ArrayList<UsbDevice>();
if (usbManager != null) {
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
// Iterate over USB devices
for (Entry<String, UsbDevice> e : deviceList.entrySet()) {
Log.i(LOG_TAG, "String: " + e.getKey() + " " + e.getValue().getVendorId() + " " + e.getValue().getProductId());
UsbDevice device = e.getValue();
if (device.getVendorId() == vendorId && device.getProductId() == productId) {
usbDeviceList.add(device);
}
}
}
return usbDeviceList;
}
/**
* get product id
* @return
*/
public int getProductId() {
final UsbDevice device = mWeakDevice.get();
return device != null ? device.getProductId() : 0;
}
@Override
public boolean equals(final Object obj) {
// can't compare if we have wildcard strings
if (mVendorId == -1 || mProductId == -1 || mClass == -1
|| mSubclass == -1 || mProtocol == -1) {
return false;
}
if (obj instanceof DeviceFilter) {
final DeviceFilter filter = (DeviceFilter) obj;
if (filter.mVendorId != mVendorId
|| filter.mProductId != mProductId
|| filter.mClass != mClass || filter.mSubclass != mSubclass
|| filter.mProtocol != mProtocol) {
return false;
}
if ((filter.mManufacturerName != null && mManufacturerName == null)
|| (filter.mManufacturerName == null && mManufacturerName != null)
|| (filter.mProductName != null && mProductName == null)
|| (filter.mProductName == null && mProductName != null)
|| (filter.mSerialNumber != null && mSerialNumber == null)
|| (filter.mSerialNumber == null && mSerialNumber != null)) {
return false;
}
if ((filter.mManufacturerName != null && mManufacturerName != null && !mManufacturerName
.equals(filter.mManufacturerName))
|| (filter.mProductName != null && mProductName != null && !mProductName
.equals(filter.mProductName))
|| (filter.mSerialNumber != null && mSerialNumber != null && !mSerialNumber
.equals(filter.mSerialNumber))) {
return false;
}
return (filter.isExclude != isExclude);
}
if (obj instanceof UsbDevice) {
final UsbDevice device = (UsbDevice) obj;
if (isExclude
|| (device.getVendorId() != mVendorId)
|| (device.getProductId() != mProductId)
|| (device.getDeviceClass() != mClass)
|| (device.getDeviceSubclass() != mSubclass)
|| (device.getDeviceProtocol() != mProtocol) ) {
return false;
}
/* if ((mManufacturerName != null && device.getManufacturerName() == null)
|| (mManufacturerName == null && device
.getManufacturerName() != null)
|| (mProductName != null && device.getProductName() == null)
|| (mProductName == null && device.getProductName() != null)
|| (mSerialNumber != null && device.getSerialNumber() == null)
|| (mSerialNumber == null && device.getSerialNumber() != null)) {
return (false);
} */
/* if ((device.getManufacturerName() != null && !mManufacturerName
.equals(device.getManufacturerName()))
|| (device.getProductName() != null && !mProductName
.equals(device.getProductName()))
|| (device.getSerialNumber() != null && !mSerialNumber
.equals(device.getSerialNumber()))) {
return (false);
} */
return true;
}
return false;
}