| @@ -2,19 +2,25 @@ | ||
| 2 | 2 | <manifest xmlns:android="http://schemas.android.com/apk/res/android" | 
| 3 | 3 | package="com.strickling.usbcamera" | 
| 4 | 4 | android:versionCode="1" | 
| 5 | - android:versionName="1.0" > | |
| 6 | - | |
| 7 | - <uses-feature android:name="android.hardware.usb.host" /> | |
| 5 | + android:versionName="1.0"> | |
| 6 | + | |
| 7 | + <uses-feature android:name="android.hardware.usb.host" /> | |
| 8 | 8 | <uses-sdk android:minSdkVersion="19" /> | 
| 9 | 9 |  | 
| 10 | 10 | <application | 
| 11 | 11 | android:icon="@drawable/ic_launcher" | 
| 12 | - android:label="@string/app_name" > | |
| 12 | + android:label="@string/app_name"> | |
| 13 | 13 | <activity | 
| 14 | 14 | android:name="com.ptplib.usbcamera.test.USBCameraTest" | 
| 15 | - android:label="@string/app_name" > | |
| 15 | + android:label="@string/app_name"> | |
| 16 | + | |
| 17 | + </activity> | |
| 18 | + <activity | |
| 19 | + android:name="com.ptplib.usbcamera.test.MyTestActivity" | |
| 20 | + android:screenOrientation="portrait"> | |
| 16 | 21 | <intent-filter> | 
| 17 | 22 | <action android:name="android.intent.action.MAIN" /> | 
| 23 | + | |
| 18 | 24 | <category android:name="android.intent.category.DEFAULT" /> | 
| 19 | 25 | <category android:name="android.intent.category.LAUNCHER" /> | 
| 20 | 26 | </intent-filter> | 
| @@ -22,9 +28,11 @@ | ||
| 22 | 28 | <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> | 
| 23 | 29 | </intent-filter> | 
| 24 | 30 |  | 
| 25 | - <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" | |
| 31 | + <meta-data | |
| 32 | + android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" | |
| 26 | 33 | android:resource="@xml/device_filter" /> | 
| 27 | 34 | </activity> | 
| 35 | + | |
| 28 | 36 | </application> | 
| 29 | 37 |  | 
| 30 | 38 | </manifest> | 
| @@ -0,0 +1,155 @@ | ||
| 1 | +package com.ptplib.usbcamera.test; | |
| 2 | + | |
| 3 | +import android.app.Activity; | |
| 4 | +import android.content.BroadcastReceiver; | |
| 5 | +import android.content.Context; | |
| 6 | +import android.content.Intent; | |
| 7 | +import android.content.IntentFilter; | |
| 8 | +import android.hardware.usb.UsbDevice; | |
| 9 | +import android.hardware.usb.UsbManager; | |
| 10 | +import android.os.Bundle; | |
| 11 | +import android.util.Log; | |
| 12 | +import android.view.View; | |
| 13 | +import android.widget.ImageView; | |
| 14 | +import android.widget.ListView; | |
| 15 | + | |
| 16 | +import com.ptplib.usbcamera.BaselineInitiator; | |
| 17 | +import com.ptplib.usbcamera.PTPException; | |
| 18 | +import com.ptplib.usbcamera.eos.EosInitiator; | |
| 19 | +import com.ptplib.usbcamera.nikon.NikonInitiator; | |
| 20 | +import com.strickling.usbcamera.R; | |
| 21 | + | |
| 22 | +public class MyTestActivity extends Activity { | |
| 23 | + | |
| 24 | + private ImageView photoIv; | |
| 25 | + private ListView infoListView; | |
| 26 | + private MyTestAdapter adapter; | |
| 27 | + | |
| 28 | + private UsbManager mUsbManager; | |
| 29 | + private BaselineInitiator bi; | |
| 30 | + | |
| 31 | + private static final String TAG = "USBTest"; | |
| 32 | + | |
| 33 | + @Override | |
| 34 | +    protected void onCreate(Bundle savedInstanceState) { | |
| 35 | + super.onCreate(savedInstanceState); | |
| 36 | + setContentView(R.layout.activity_my_test); | |
| 37 | + | |
| 38 | + mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE); | |
| 39 | + | |
| 40 | + photoIv = (ImageView)findViewById(R.id.iv_latest_photo); | |
| 41 | + infoListView = (ListView)findViewById(R.id.list_info); | |
| 42 | + adapter = new MyTestAdapter(this); | |
| 43 | + infoListView.setAdapter(adapter); | |
| 44 | + | |
| 45 | +        findViewById(R.id.btn_get_devece_info).setOnClickListener(new View.OnClickListener() { | |
| 46 | + @Override | |
| 47 | +            public void onClick(View v) { | |
| 48 | + | |
| 49 | + } | |
| 50 | + }); | |
| 51 | + | |
| 52 | +        findViewById(R.id.btn_get_photo).setOnClickListener(new View.OnClickListener() { | |
| 53 | + @Override | |
| 54 | +            public void onClick(View v) { | |
| 55 | + | |
| 56 | + } | |
| 57 | + }); | |
| 58 | + } | |
| 59 | + | |
| 60 | + @Override | |
| 61 | +    protected void onResume() { | |
| 62 | + super.onResume(); | |
| 63 | + registerUSBReceiver(); | |
| 64 | + initDevice (searchDevice ()); | |
| 65 | + } | |
| 66 | + | |
| 67 | + @Override | |
| 68 | +    protected void onPause() { | |
| 69 | + super.onPause(); | |
| 70 | + unregisterReceiver(mUsbReceiver); | |
| 71 | + detachDevice(); | |
| 72 | + } | |
| 73 | + | |
| 74 | +    BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { | |
| 75 | +        public void onReceive(Context context, Intent intent) { | |
| 76 | + String action = intent.getAction(); | |
| 77 | + UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); | |
| 78 | +            if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) { | |
| 79 | +                adapter.addInfo("监听到有USB设备插入 "+ device.getDeviceName()); | |
| 80 | + initDevice (device); | |
| 81 | +            } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) { | |
| 82 | +                adapter.addInfo("监听到有USB设备卸载 "+ device.getDeviceName()); | |
| 83 | + if (bi != null && bi.getSession() != null) bi.getSession().close(); | |
| 84 | + detachDevice (); | |
| 85 | + } | |
| 86 | + } | |
| 87 | + }; | |
| 88 | + | |
| 89 | + // search connected devices, returns only protocol 0 devices | |
| 90 | +    public UsbDevice searchDevice () { | |
| 91 | + UsbDevice device = null; | |
| 92 | +        adapter.addInfo("开始搜素USB设备---------- "); | |
| 93 | +        for (UsbDevice lDevice :  mUsbManager.getDeviceList().values()) { | |
| 94 | +            adapter.addInfo("搜素到USB设备 "+ lDevice.getDeviceName() + " 支持协议为"+ lDevice.getDeviceProtocol()); | |
| 95 | +            if (lDevice.getDeviceProtocol() == 0) { | |
| 96 | + device = lDevice; | |
| 97 | +                adapter.addInfo("搜素到支持PTP的USB设备 "+ lDevice.getDeviceName() + " 支持协议为"+ lDevice.getDeviceProtocol()); | |
| 98 | + } | |
| 99 | + } | |
| 100 | +        adapter.addInfo("搜素USB设备完成---------- "); | |
| 101 | + return device; | |
| 102 | + } | |
| 103 | + | |
| 104 | +    public void initDevice (UsbDevice device) { | |
| 105 | +        if (device != null){ | |
| 106 | +            adapter.addInfo("初始化设备 "+ device.getDeviceName()); | |
| 107 | +            try { | |
| 108 | + bi = new BaselineInitiator (device, mUsbManager.openDevice(device)); | |
| 109 | + // Select appropriate deviceInitiator, VIDs see http://www.linux-usb.org/usb.ids | |
| 110 | +                if (bi.getDevice().getVendorId() == EosInitiator.CANON_VID) { | |
| 111 | +                    try { | |
| 112 | + bi.getClearStatus(); | |
| 113 | + bi.close(); | |
| 114 | +                    } catch (PTPException e) {e.printStackTrace(); | |
| 115 | +                        adapter.addInfo("初始化设备异常 "+ e); | |
| 116 | + } | |
| 117 | +                    adapter.addInfo("初始化设备成功, 是Cannon"); | |
| 118 | + bi = new EosInitiator (device, mUsbManager.openDevice(device)); | |
| 119 | + } | |
| 120 | +                else if (device.getVendorId() == NikonInitiator.NIKON_VID) { | |
| 121 | +                    try { | |
| 122 | + bi.getClearStatus(); | |
| 123 | + bi.close(); | |
| 124 | +                    } catch (PTPException e) {e.printStackTrace(); | |
| 125 | +                        adapter.addInfo("初始化设备异常 "+ e); | |
| 126 | + } | |
| 127 | +                    adapter.addInfo("初始化设备成功, 是Nikon"); | |
| 128 | + bi = new NikonInitiator (device, mUsbManager.openDevice(device)); | |
| 129 | + } | |
| 130 | +                adapter.addInfo("开始建立session"); | |
| 131 | + bi.openSession(); | |
| 132 | +            } catch (PTPException e) { | |
| 133 | + e.printStackTrace(); | |
| 134 | +                adapter.addInfo("初始化设备发生错误 "+ e); | |
| 135 | + } | |
| 136 | + } | |
| 137 | + } | |
| 138 | + | |
| 139 | +    public void detachDevice () { | |
| 140 | +        if (bi != null) { | |
| 141 | + if (bi.getDevice() != null) Log.d(TAG, "detachDevice: " +bi.getDevice().getDeviceName()); | |
| 142 | +            try { | |
| 143 | + bi.close(); | |
| 144 | +            } catch (PTPException e) { | |
| 145 | + e.printStackTrace(); | |
| 146 | + } | |
| 147 | + } | |
| 148 | + } | |
| 149 | +    private void registerUSBReceiver(){ | |
| 150 | + IntentFilter filter = new IntentFilter(); | |
| 151 | + filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); | |
| 152 | + filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); | |
| 153 | + registerReceiver(mUsbReceiver, filter); | |
| 154 | + } | |
| 155 | +} | 
| @@ -0,0 +1,67 @@ | ||
| 1 | +package com.ptplib.usbcamera.test; | |
| 2 | + | |
| 3 | +import android.content.Context; | |
| 4 | +import android.view.LayoutInflater; | |
| 5 | +import android.view.View; | |
| 6 | +import android.view.ViewGroup; | |
| 7 | +import android.widget.BaseAdapter; | |
| 8 | +import android.widget.TextView; | |
| 9 | + | |
| 10 | +import com.strickling.usbcamera.R; | |
| 11 | + | |
| 12 | +import java.text.SimpleDateFormat; | |
| 13 | +import java.util.ArrayList; | |
| 14 | +import java.util.Date; | |
| 15 | + | |
| 16 | +public class MyTestAdapter extends BaseAdapter { | |
| 17 | + | |
| 18 | + private ArrayList<String> infoList; | |
| 19 | + private Context context; | |
| 20 | + private LayoutInflater inflater; | |
| 21 | +    private SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss\t"); | |
| 22 | + | |
| 23 | +    public MyTestAdapter(Context context) { | |
| 24 | + this.context = context; | |
| 25 | + infoList = new ArrayList<>(); | |
| 26 | + inflater = LayoutInflater.from(context); | |
| 27 | + } | |
| 28 | + | |
| 29 | + @Override | |
| 30 | +    public int getCount() { | |
| 31 | + return infoList.size(); | |
| 32 | + } | |
| 33 | + | |
| 34 | + @Override | |
| 35 | +    public Object getItem(int position) { | |
| 36 | + return infoList.get(position); | |
| 37 | + } | |
| 38 | + | |
| 39 | + @Override | |
| 40 | +    public long getItemId(int position) { | |
| 41 | + return position; | |
| 42 | + } | |
| 43 | + | |
| 44 | +    public void addInfo(String info){ | |
| 45 | + infoList.add(formatter.format(new Date())+info); | |
| 46 | + notifyDataSetChanged(); | |
| 47 | + } | |
| 48 | + | |
| 49 | + @Override | |
| 50 | +    public View getView(int position, View convertView, ViewGroup parent) { | |
| 51 | + ViewHolder holder; | |
| 52 | +        if (convertView == null) { | |
| 53 | + convertView = inflater.inflate(R.layout.item_test_info, null); | |
| 54 | + holder = new ViewHolder(); | |
| 55 | + holder.info = (TextView) convertView.findViewById(R.id.tv_info); | |
| 56 | + convertView.setTag(holder); | |
| 57 | +        } else { | |
| 58 | + holder = (ViewHolder) convertView.getTag(); | |
| 59 | + } | |
| 60 | + holder.info.setText(infoList.get(position)); | |
| 61 | + return convertView; | |
| 62 | + } | |
| 63 | + | |
| 64 | +    final class ViewHolder { | |
| 65 | + public TextView info; | |
| 66 | + } | |
| 67 | +} | 
| @@ -0,0 +1,34 @@ | ||
| 1 | +<?xml version="1.0" encoding="utf-8"?> | |
| 2 | +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| 3 | + android:orientation="vertical" android:layout_width="match_parent" | |
| 4 | + android:layout_height="match_parent"> | |
| 5 | + | |
| 6 | + <Button | |
| 7 | + android:id="@+id/btn_get_devece_info" | |
| 8 | + android:layout_width="match_parent" | |
| 9 | + android:layout_height="48dp" | |
| 10 | + android:layout_margin="4dp" | |
| 11 | + android:gravity="center" | |
| 12 | + android:text="获取设备信息"/> | |
| 13 | + | |
| 14 | + <ListView | |
| 15 | + android:id="@+id/list_info" | |
| 16 | + android:layout_width="match_parent" | |
| 17 | + android:divider="@android:color/transparent" | |
| 18 | + android:layout_height="200dp" /> | |
| 19 | + <Button | |
| 20 | + android:id="@+id/btn_get_photo" | |
| 21 | + android:layout_width="match_parent" | |
| 22 | + android:layout_height="48dp" | |
| 23 | + android:layout_margin="4dp" | |
| 24 | + android:gravity="center" | |
| 25 | + android:text="获取最新照片"/> | |
| 26 | + | |
| 27 | + | |
| 28 | + <ImageView | |
| 29 | + android:id="@+id/iv_latest_photo" | |
| 30 | + android:layout_width="match_parent" | |
| 31 | + android:layout_height="match_parent" | |
| 32 | + android:scaleType="centerInside"/> | |
| 33 | + | |
| 34 | +</LinearLayout> | 
| @@ -0,0 +1,14 @@ | ||
| 1 | +<?xml version="1.0" encoding="utf-8"?> | |
| 2 | + | |
| 3 | +<TextView xmlns:android="http://schemas.android.com/apk/res/android" | |
| 4 | + android:id="@+id/tv_info" | |
| 5 | + android:layout_width="match_parent" | |
| 6 | + android:layout_height="wrap_content" | |
| 7 | + android:textSize="14sp" | |
| 8 | + android:textColor="@android:color/black" | |
| 9 | + android:gravity="center_vertical" | |
| 10 | + android:paddingLeft="8dp" | |
| 11 | + android:paddingTop="4dp" | |
| 12 | + android:paddingBottom="4dp" | |
| 13 | + android:paddingRight="8dp" | |
| 14 | + android:minHeight="20dp" /> | 
| @@ -3,7 +3,7 @@ | ||
| 3 | 3 |  | 
| 4 | 4 | <string name="desc">Description of an image</string> | 
| 5 | 5 | <string name="hello">Hello World, USBCamera Activity!</string> | 
| 6 | - <string name="app_name">USBCameraTest</string> | |
| 6 | + <string name="app_name">Test</string> | |
| 7 | 7 | <string name="Button1">Get DeviceInfo</string> | 
| 8 | 8 | <string name="Button2">Detailed get DeviceInfo</string> | 
| 9 | 9 | <string name="Button3">Take Picture</string> |