文章目录
一、通过系统报告查看
本机 —> 系统报告 —> 硬件—>USB
二、通过 IORegistryExplorer 获取详细信息
IORegistryExplorer 下载地址:
https://download.csdn.net/download/lovechris00/10317601
三、通过 IOKit.framework 编码获取
-(void)getUSBInfo{
CFMutableDictionaryRef properties = 0;
kern_return_t status = KERN_SUCCESS;
io_registry_entry_t service = IORegistryEntryFromPath(kIOMasterPortDefault, "IOUSB:/");
status = IORegistryEntryCreateCFProperties(service,&properties,kCFAllocatorDefault,kNilOptions );
if (status){
NSLog(@"status : %d",status);
}else{
NSDictionary *pro = (__bridge NSDictionary *)properties;
NSLog(@"\n\n properties : %@-------\n\n",pro);
for (NSString *key in pro.allKeys) {
id data = pro[key];
if ([data isKindOfClass:[NSData class]]) {
NSString *value = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@ : %@",key,value);
}else{
NSLog(@"%@ - %@",key,data);
}
}
}
//获取子节点
io_iterator_t iterator = IO_OBJECT_NULL;
status = IORegistryEntryGetChildIterator(service,kIOUSBPlane,&iterator);
kern_return_t kr;
io_registry_entry_t usb;
io_name_t name;
while((usb = IOIteratorNext(iterator))) {
NSLog(@"\n\n---------------");
if( KERN_SUCCESS == IORegistryEntryGetName(usb, name)){
printf(name);
}
assert( KERN_SUCCESS == (kr = IORegistryEntryCreateCFProperties(usb, &properties, kCFAllocatorDefault, kNilOptions )));
NSLog(@"\nproperties : %@\n",(__bridge NSDictionary *)properties);
}
IOObjectRelease(usb);
CFRelease(properties);
IOObjectRelease(service);
}
USB 部分日志类似如下:
---------------
DataTraveler 3.0
properties : {
"Built-In" = 0;
"Bus Power Available" = 450;
"Device Speed" = 3;
IOCFPlugInTypes = {
"9dc7b780-9ec0-11d4-a54f-000a27052861" = "IOUSBFamily.kext/Contents/PlugIns/IOUSBLib.bundle";
};
IOClassNameOverride = IOUSBDevice;
IOGeneralInterest = "IOCommand is not serializable";
IOPowerManagement = {
CapabilityFlags = 65536;
CurrentPowerState = 3;
DevicePowerState = 0;
DriverPowerState = 3;
MaxPowerState = 4;
};
IOUserClientClass = IOUSBDeviceUserClientV2;
PortNum = 11;
"USB Address" = 2;
"USB Product Name" = "DataTraveler 3.0";
"USB Serial Number" = 08606E6D3FE5B011581A0F09;
"USB Vendor Name" = Kingston;
bDeviceClass = 0;
bDeviceProtocol = 0;
bDeviceSubClass = 0;
bMaxPacketSize0 = 9;
bNumConfigurations = 1;
bcdDevice = 272;
bcdUSB = 768;
iManufacturer = 1;
iProduct = 2;
iSerialNumber = 3;
idProduct = 5734;
idVendor = 2385;
kUSBCurrentConfiguration = 1;
locationID = 347078656;
"non-removable" = no;
sessionID = 1579503837;
uid = "USB:09511666B011581A0F09";
}
四、通过命令行获取
1、$ system_profiler SPUSBDataType
$ system_profiler SPUSBDataType
USB:
USB 3.0 Bus:
Host Controller Driver: AppleUSBXHCILPT
PCI Device ID: 0x9c31
PCI Revision ID: 0x0004
PCI Vendor ID: 0x8086
DataTraveler 3.0:
Product ID: 0x1666
Vendor ID: 0x0951 (Kingston Technology Company)
Version: 1.10
Serial Number: 08606E6D3FE5B011581A0F09
Speed: Up to 5 Gb/sec
Manufacturer: Kingston
Location ID: 0x14b00000 / 2
Current Available (mA): 900
Current Required (mA): 504
Extra Operating Current (mA): 0
...
2、$ ioreg -Src IOUSBDevice
如果希望输出 xml 数据,命令可以改为 $ ioreg -Src IOUSBDevice -a
$ ioreg -Src IOUSBDevice
+-o AppleUSBXHCI Root Hub Simulation@14000000 <class AppleUSBRootHubDevice, id 0x10000031$
{
"iManufacturer" = 0
"bNumConfigurations" = 1
"idProduct" = 32775
"bMaxPacketSize0" = 8
"Built-In" = Yes
"iProduct" = 0
"USB Product Name" = "AppleUSBXHCI Root Hub Simulation"
"iSerialNumber" = 0
"bDeviceClass" = 9
"IOPowerManagement" = {"DevicePowerState"=0,"CurrentPowerState"=4,"CapabilityFlags"=$
"IOUserClientClass" = "IOUSBDeviceUserClientV2"
"locationID" = 335544320
"bDeviceSubClass" = 255
"bcdUSB" = 256
"non-removable" = "yes"
"IOCFPlugInTypes" = {"9dc7b780-9ec0-11d4-a54f-000a27052861"="IOUSBFamily.kext/Conten$
"bDeviceProtocol" = 3
"USB Vendor Name" = "Apple Inc."
"Device Speed" = 3
"idVendor" = 1452
"IOClassNameOverride" = "IOUSBRootHubDevice"
}
+-o DataTraveler 3.0@14b00000 <class AppleUSBDevice, id 0x100000320, registered, matched,$
| {
| "sessionID" = 1579503837
| "iManufacturer" = 1
| "bNumConfigurations" = 1
| "idProduct" = 5734
| "bcdDevice" = 272
| "Bus Power Available" = 450
| "USB Address" = 2
| "bMaxPacketSize0" = 9
| "iProduct" = 2
| "iSerialNumber" = 3
| "bDeviceClass" = 0
| "Built-In" = No
| "locationID" = 347078656
| "bDeviceSubClass" = 0
| "bcdUSB" = 768
| "USB Product Name" = "DataTraveler 3.0"
| "PortNum" = 11
| "non-removable" = "no"
| "IOCFPlugInTypes" = {"9dc7b780-9ec0-11d4-a54f-000a27052861"="IOUSBFamily.kext/Conten$
| "bDeviceProtocol" = 0
| "IOUserClientClass" = "IOUSBDeviceUserClientV2"
| "IOPowerManagement" = {"DevicePowerState"=0,"CurrentPowerState"=3,"CapabilityFlags"=$
| "kUSBCurrentConfiguration" = 1
| "Device Speed" = 3
| "USB Vendor Name" = "Kingston"
| "idVendor" = 2385
| "uid" = "USB:09511666B011581A0F09"
| "IOGeneralInterest" = "IOCommand is not serializable"
| "USB Serial Number" = "08606E6D3FE5B011581A0F09"
| "IOClassNameOverride" = "IOUSBDevice"
| }
|
+-o AppleUSBInterface@0 <class AppleUSBInterface, id 0x100000327, registered, matched, $
参考资料
-
官方:USB Device Interface Guide
https://developer.apple.com/library/archive/documentation/DeviceDrivers/Conceptual/USBBook/USBIntro/USBIntro.html -
USB Device Interface Guide
https://developer.apple.com/library/archive/documentation/DeviceDrivers/Conceptual/USBBook/USBOverview/USBOverview.html#//apple_ref/doc/uid/TP40002644-TPXREF101 -
Using USB Device Interfaces
https://developer.apple.com/library/archive/documentation/DeviceDrivers/Conceptual/USBBook/USBDeviceInterfaces/USBDevInterfaces.html#//apple_ref/doc/uid/TP40002645-BBIHCIHB -
Finding USB Devices and Interfaces
https://developer.apple.com/library/archive/documentation/DeviceDrivers/Conceptual/USBBook/USBOverview/USBOverview.html#//apple_ref/doc/uid/TP40002644-BBIDGCHB -
Universal Serial Bus Common Class Specification
https://www.usb.org/sites/default/files/usbccs10.pdf -
HID API for Linux, Mac OS X, and Windows
http://www.signal11.us/oss/hidapi/ -
hidapi API
http://www.signal11.us/oss/hidapi/hidapi/doxygen/html/group__API.html
https://github.com/signal11/hidapi -
USBPrivateDataSample
https://developer.apple.com/library/archive/samplecode/USBPrivateDataSample/Introduction/Intro.html#//apple_ref/doc/uid/DTS10000456-Intro-DontLinkElementID_2