USB HOST

 

USB HOST:指两台手机通过OTG线连接在一起,一个主机,一个从机,主机的作用可以理解成PC端,从机可以选择USB  mode

 

主机连接分析:

 

1.主要类:

 

UsbHostManager .java  : 在host模式下管理USB状态

UsbSettingsManager.java  : 通知用户

UsbDevice.java  : 主要是usb设备的一些接口,基本上是获取的设备描述符

MtpNotificationManager :Manager for MTP storage notification

UsbManager :This is a wrapper class for the USB Manager tosupport USB accessories

2.JNI:

UsbHostManager是通过JNI由native层调用的

                     Native àJNIàJava

Onload.cpp — JNI_OnLoad()– register_android_server_UsbHostManager()

3.USB host 连接:

com_android_server_UsbHostManager.cpp – >

      static intusb_device_added(const char *devname, void* client_data) {

                     structusb_descriptor_header* desc;

                 struct usb_descriptor_iter iter;

           ……

                     jbooleanresult = env->CallBooleanMethod(thiz, method_beginUsbDeviceAdded,

            deviceName,usb_device_get_vendor_id(device), usb_device_get_product_id(device),

            deviceDesc->bDeviceClass,deviceDesc->bDeviceSubClass, deviceDesc->bDeviceProtocol,

            manufacturerName,productName, version, serialNumber);

           ……

           env->CallVoidMethod(thiz, method_endUsbDeviceAdded);

           ……

      }

 

在usb_device_added()中会调用beginUsbDeviceAdded endUsbDeviceAdded

 

会将连接的从机的设备信息传递给beginUsbDeviceAdded,从而使上层获取到从机设备信息,而endUsbDeviceAdded 的作用是通知用户USB 连接成功了

 

// beginUsbDeviceAdded方法:

 

UsbHostManager. Java — >

      private void beginUsbDeviceAdded() {

                     ……

                     //创建UsbDevice对象

                     mNewDevice =new UsbDevice(deviceName, vendorID, productID,

                    deviceClass,deviceSubclass, deviceProtocol,

                   manufacturerName, productName, versionString, serialNumber);

                     ……

             }

// endUsbDeviceAdded方法:

UsbHostManager. Java — >

      private voidendUsbDeviceAdded() {

                     if (DEBUG) {

             Slog.d(TAG,”usb:UsbHostManager.endUsbDeviceAdded()”);

             }

           ……

           synchronized (mLock) {

            if (mNewDevice !=null) {

                mNewDevice.setConfigurations(

                       mNewConfigurations.toArray(newUsbConfiguration[mNewConfigurations.size()]));

               mDevices.put(mNewDevice.getDeviceName(), mNewDevice);

                Slog.d(TAG,”Added device ” + mNewDevice);

                getCurrentSettings().deviceAttached(mNewDevice);

               mUsbAlsaManager.usbDeviceAdded(mNewDevice);

            }

            ……

      }

 

getCurrentSettings()就是UsbSettingsManager对象

 

UsbSettingsManager.java — >

     public voiddeviceAttached(UsbDevice device) {

        finalIntent intent = createDeviceAttachedIntent(device);

 

        //Send broadcast to running activity with registered intent

        mUserContext.sendBroadcast(intent);

 

 if(MtpNotificationManager.shouldShowNotification(mPackageManager, device)) {

            // 发送Notification,这里的device就是beginUsbDeviceAdded中创建的mNewDevice对象

           mMtpNotificationManager.showNotification(device);

        } else {

           resolveActivity(intent, device);

        }

}

 

private static IntentcreateDeviceAttachedIntent(UsbDevice device) {

       Intent intent = new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED);

       intent.putExtra(UsbManager.EXTRA_DEVICE, device);

       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

       return intent;

}

 

//应用如果想要监听USBhost状态,需要注册静态广播监听action:

UsbManager.ACTION_USB_DEVICE_ATTACHED

即:android.hardware.usb.action.USB_DEVICE_ATTACHED

 

4.USB host断开连接

 

 

//com_android_server_UsbHostManager.cpp – > usb_device_removed()

 

static int usb_device_removed(const char *devname, void* client_data) {

    JNIEnv* env =AndroidRuntime::getJNIEnv();

    jobject thiz =(jobject)client_data;

 

    jstring deviceName =env->NewStringUTF(devname);

    env->CallVoidMethod(thiz,method_usbDeviceRemoved, deviceName);

   env->DeleteLocalRef(deviceName);

   checkAndClearExceptionFromCallback(env, __FUNCTION__);

    return 0;

}

 

// UsbHostManager.java — > usbDeviceRemoved()

 

private void usbDeviceRemoved(String deviceName) {

    synchronized (mLock) {

        UsbDevice device =mDevices.remove(deviceName);

        if (device != null) {

            mUsbAlsaManager.usbDeviceRemoved(device);

             getCurrentSettings().deviceDetached(device);

        }

   }

}

 

// UsbSettingsManager.java — > deviceDetached()

public void deviceDetached(UsbDevice device) {

    // clear temporary permissionsfor the device

   mDevicePermissionMap.remove(device.getDeviceName());

    //发送USB host断开广播    

    Intent intent = newIntent(UsbManager.ACTION_USB_DEVICE_DETACHED);

   intent.putExtra(UsbManager.EXTRA_DEVICE, device);

    if (DEBUG) Slog.d(TAG,”usbDeviceRemoved, sending ” + intent);

   mContext.sendBroadcastAsUser(intent, UserHandle.ALL);

    //取消  USBhost通知

   mMtpNotificationManager.hideNotification(device.getDeviceId());

}

//应用如果想要监听USBhost断开状态,需要注册静态广播监听action:

UsbManager. ACTION_USB_DEVICE_DETACHED

即:android.hardware.usb.action.USB_DEVICE_DETACHED

 

 

 

版权声明:本文为匿名原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: