android - 智能手机和平板电脑 - 插入计算机

标签 android cocoa-touch cocoa connection device

有没有办法确定智能手机或平板电脑(iOS 或 Android)是否已插入计算机?

手机/平板电脑视角(即 isConnectedToComputer)或计算机视角(即 connectedDevices)都可以。

如果从计算机角度来看,则必须通过 Mac OS X 应用程序。 如果从手机/平板电脑的角度来看,它需要与简单的充电区分开来。

总体思路是确定用户何时使用计算机。

注意:为了做出此决定,我需要识别设备。对于 iOS,使用设备 UUID(类似于 APNS 中使用的 UUID)。在 Android 中,硬件 ID 和软件 ID 是组合在一起的。所以如果从计算机的角度来解决,我需要能够获取这些字符串。否则,我需要能够将它们传递到计算机。

奖励:如果 Wi-Fi 同步也能被视为“已连接”,那就太棒了。

最佳答案

简单的解决方案

考虑通过 NSTask 使用 system_profiler SPUSBDataType
连接我的 iPhone 后,结果中包含以下内容:

iPhone:

 Product ID: 0x1292
 Vendor ID: 0x05ac  (Apple Inc.)
 Version: 0.01
 Serial Number: 
 Speed: Up to 480 Mb/sec
 Manufacturer: Apple Inc.
 Location ID: 0xfa140000 / 6
 Current Available (mA): 500
 Current Required (mA): 500
 Extra Operating Current (mA): 500

使用system_profiler SPUSBDataType -xmlXML格式输出结果:

<dict>
    <key>_name</key>
    <string>iPhone</string>
    <key>a_product_id</key>
    <string>0x1292</string>
    <key>b_vendor_id</key>
    <string>apple_vendor_id</string>
    <key>c_bcd_device</key>
    <string> 0.01</string>
    <key>d_serial_num</key>
    <string></string>
    <key>e_device_speed</key>
    <string>high_speed</string>
    <key>f_manufacturer</key>
    <string>Apple Inc.</string>
    <key>g_location_id</key>
    <string>0xfa140000 / 6</string>
    <key>h_bus_power</key>
    <string>500</string>
    <key>j_bus_power_used</key>
    <string>500</string>
    <key>k_extra_current_used</key>
    <string>500</string>
</dict>

只需解析 XML 即可确定感兴趣的设备是否已连接。

关于android - 智能手机和平板电脑 - 插入计算机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8087089/

相关文章:

android - 更改 Android 音频记录默认输入源

android - 用于 Android 的 OpenGL ES,家具建模

c# - Cocoa (MonoMac) & WinForms (or WPF) frontend with common c# backend

android - Android Geocoder 是否仅适用于互联网连接?

iphone - 如何获取一个巨大的视频文件的切片数据

ios - 将 NSTimer 的 fireDate 之前的时间加倍

cocoa - NSUTF8StringEncoding 导致文件在 TextEdit/Pages 中渲染不正确,但在 BBEdit/vi 中则不会

objective-c - 为什么在实现我自己的 KVC setter/getter 方法时会出现未捕获的异常

macos - 为什么 NSTextView 在调整大小时并不总是流畅地换行?

python - Cython not found.. Please install it error in buildozer