【问题标题】:Need an API that detects when an iPhone is plugged in需要一个 API 来检测何时插入 iPhone
【发布时间】:2011-11-10 05:29:36
【问题描述】:

我正在为 Mac 制作一个应用程序,我需要一个 API 来检测 iPhone 何时插入。谢谢。

编辑:具体来说,我需要一个 API 来检测 iPhone 何时插入 Mac 上的 USB 端口。

【问题讨论】:

  • 约翰,你能澄清你的问题吗?据我了解,您想为 MAC OS X 编写一个应用程序 - 对吗?如果是这样,请记住@Scott 的代码适用于 iOS,而不是 OSX。此外,他的代码没有涵盖 Mac 不是充电设备的情况……它可以是 PC 或其他 USB 主机……更不用说墙上适配器通过 USB 电缆为 iPhone 充电……
  • 是的,我正在为 OS X 编写一个应用程序,我需要一个 API 来检测 iDevice 何时插入 Mac。我的意思是“f0recast”应用程序的一个很好的例子,我找不到任何好的答案。

标签: iphone objective-c ios macos


【解决方案1】:

我没有完整的答案,但可以实现您想要的程序是 USB Prober,它与 Xcode 一起提供,位于 /Developer/Applications/Utilities/USB Prober.app。该程序是开源的,浏览器可查看的存储库为here,整个项目包含在this download 中。实际上,我发现旧版本更有帮助,例如here,尤其是BusProbeClass

它们都依赖于 IOKit,Apple 的文档似乎在数量和质量上都非常缺乏。

阅读量很大,但是如果您查看 + USBProbe,您会看到它获取当前 USB 设备的列表,为变量 deviceIntf 中的每个设备获取 IOUSBDeviceInterfaces,然后将它们推送到有用的地方程序的其余部分。如果您在同一源文件的下方查看+ outputDevice: locationID:deviceNumber:,您会看到GetDescriptor 似乎可以在IOUSBDeviceDescriptor 上使用以获取包括供应商和产品ID 在内的属性,它们的组合保证是USB 实施者论坛的独特之处。

使用供应商和产品 ID,您可以搜索任何特定的 USB 设备。从我的 Mac 系统信息中,我可以告诉你 iPhone 4 的产品 ID 为 0x1297,Apple 的供应商 ID 为 0x05ac。

额外:从剖析代码中,如果你删除了一大堆关于事情是否成功的检查并将其全部压缩为示范性的东西,那么以下至少是对 iPhone 4 是否立即插入的测试(你'需要链接到 Foundation 和 IOKit 框架):

#include <stdio.h>
#import <Foundation/Foundation.h>
#import <IOKit/usb/IOUSBLib.h>
#import <IOKit/IOCFPlugIn.h>
#import <mach/mach_port.h>

int main (int argc, const char * argv[])
{
    // get the port through which to talk to the kernel
    mach_port_t masterDevicePort;
    IOMasterPort(MACH_PORT_NULL, &masterDevicePort);

    // create a dictionary that describes a search
    // for services provided by USB
    CFDictionaryRef matchingDictionary = IOServiceMatching(kIOUSBDeviceClassName);

    // get an iterator for all devices that match
    // the dictionary
    io_iterator_t deviceIterator;
    IOServiceGetMatchingServices(
            masterDevicePort,
            matchingDictionary,
            &deviceIterator);

    // iterate through the iterator...
    io_service_t ioDevice;
    while((ioDevice = IOIteratorNext(deviceIterator)))
    {
        IOUSBDeviceInterface **deviceInterface = NULL;
        IOCFPlugInInterface **ioPlugin = NULL;
        SInt32 score;

        // get a pointer to the device, stored to ioPlugin
        IOCreatePlugInInterfaceForService(
            ioDevice,
            kIOUSBDeviceUserClientTypeID,
            kIOCFPlugInInterfaceID,
            &ioPlugin,
            &score);

        // ask the device for its interface
        (*ioPlugin)->QueryInterface(
            ioPlugin, 
            CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID),
            (void *)&deviceInterface);

        // make and issue a request to get the device descriptor
        IOUSBDeviceDescriptor deviceDescriptor;
        IOUSBDevRequest request;

        request.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
        request.bRequest = kUSBRqGetDescriptor;
        request.wValue = kUSBDeviceDesc << 8;
        request.wIndex = 0;
        request.wLength = sizeof(deviceDescriptor);
        request.pData = &deviceDescriptor;

        (*deviceInterface)->DeviceRequest(deviceInterface, &request);

        // now we have the device descriptor, do a little cleaning up -
        // release the interface and the device
        (*deviceInterface)->Release(deviceInterface);
        IOObjectRelease(ioDevice);

        // ensure that the values returned are in the appropriate
        // byte order for this platform
        CFSwapInt16LittleToHost(deviceDescriptor.idVendor);
        CFSwapInt16LittleToHost(deviceDescriptor.idProduct);

        // check whether we have an iPhone 4 attached
        if(deviceDescriptor.idVendor == 0x05ac && deviceDescriptor.idProduct == 0x1297)
            printf("iPhone 4 is connected!");
    }

    // clean up by releasing the device iterator
    // and returning the communications port
    IOObjectRelease(deviceIterator);
    mach_port_deallocate(mach_task_self(), masterDevicePort);

    return 0;
}

我还没有弄清楚如何观察插入设备的变化。

【讨论】:

  • 这正是我想要的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-21
  • 2011-04-04
  • 2020-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-09
相关资源
最近更新 更多