需要一个能够检测iPhone插入时间的API [英] Need an API that detects when an iPhone is plugged in

查看:80
本文介绍了需要一个能够检测iPhone插入时间的API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Mac制作应用程序,我需要一个能够检测到iPhone插入的API。谢谢。

I'm making an application for Mac, and I need an API that detects when an iPhone is plugged in. Thanks.

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

EDIT : To clarify, specifically, I need an API that detects when an iPhone is plugged into a USB port on a Mac.

推荐答案

我没有一个完整的答案,但实现你想要的程序是USB Prober,随Xcode提供位于 / Developer / Applications / Utilities / USB Prober.app 。该程序是开源的,浏览器可查看的存储库是这里整个项目包含在此下载。我实际上发现旧版本更有帮助,如此处,尤其是 BusProbeClass

I don't have a complete answer, but a program that achieves what you want is USB Prober, supplied with Xcode and located at /Developer/Applications/Utilities/USB Prober.app. That program is open source, with the browser viewable repository being here and the whole project being included in this download. I actually found an older version to be more helpful, as available here, especially BusProbeClass.

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

They all rest on IOKit for which Apple's documentation seems to be very lacking in both quantity and quality.

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

That's heavy reading, but if you check out + USBProbe then you'll see it gets a list of current USB devices, gets IOUSBDeviceInterfaces for each in the variable deviceIntf and then pushes them to somewhere useful for the rest of the program. If you check out + outputDevice: locationID:deviceNumber: lower down in the same source file, you'll see that GetDescriptor can seemingly be used on an IOUSBDeviceDescriptor to get properties including the vendor and product ID, the combination of which is guaranteed to be unique by the USB Implementer's Forum.

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

Using the vendor and product ID, you can search for any specific USB device. From my Mac's System Information I can tell you that an iPhone 4 has a product ID of 0x1297 and Apple's vendor ID is 0x05ac.

额外:解析该代码,如果你删除一大堆检查事项是否成功并将其全部压缩到演示内容然后以下至少是对现在是否插入iPhone 4的测试(您需要链接到Foundation和IOKit框架) :

Extra: from dissecting that code, if you remove a whole bunch of checks that things are succeeding and compress it all down to demonstrative stuff then the following is at least a test for whether an iPhone 4 is plugged in right now (you'll need to link to the Foundation and IOKit frameworks):

#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;
}

我还没有想出如何观察插件的变化设备。

I haven't yet figured out how to observe for changes in plugged-in devices.

这篇关于需要一个能够检测iPhone插入时间的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆