개발 관련/SW, App 관련

Unity 안드로이드 USB카메라 연결

by 소서리스25 2025. 3. 4.
반응형

Unity 안드로이드 USB카메라 연결

 

퍼온 자료인데 되는지 안되는지 확인은 못해봄.. 안돼도 참고만 할 뿐..

 

 

1. Unity의 Android 패키징 후 USB 카메라를 연결하고 권한을 허용해야 합니다.

 

2. Unity에서 다음과 같은 설정으로 프로젝트 내보내기

 

 

 

3. Android의 AndroidManifest.xml 설정

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
		xmlns:tools="http://schemas.android.com/tools">
	<application android:extractNativeLibs="true">

	<activity android:name="com.unity3d.player.UnityPlayerActivity" android:theme="@style/UnityThemeSelector" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> 
        </intent-filter>
		<meta-data android:name="unityplayer. UnityActivity" android:value="true" /> 
    	<meta-data android:name="android.notch_support" android:value="true" />
    </activity>
    
	<meta-data android:name="unity.splash-mode" android:value="0" />
	<meta-data android:name="unity.splash-enable" android:value="True" /> 
    <meta-data android:name="unity.launch-fullscreen" android:value="True" /> 
    <meta-data android:name="unity.allow-resizable-window" android:value="False" />
    <meta-data android:name="notch.config" android:value="portrait | landscape" /> 
	</application>
    
    <uses-feature android:glEsVersion="0x00020000" />
	<uses-permission android:name="android.permission.INTERNET" />
	<uses-permission android:name="android.permission.CAMERA" />

	<uses-feature android:name="android.hardware.usb.host" />
	<uses-permission android:name="android.permission.USB_PERMISSION" />
	<uses-feature android:name="android.hardware.camera" android:required="false" />
	<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
	<uses-feature android:name="android.hardware.camera.front" android:required="false" />
	<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
	<uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
	<uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" /> 
</manifest>

 

 

세 가지 주요 라인은 다음과 같습니다.

<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> 
<uses-feature android:name="android.hardware.usb.host" />
<uses-permission android:name="android.permission.USB_PERMISSION" />

 

 

4. UnityPlayerActivity.java를 찾아 Unity의 onResume 메서드에 USB 권한 설정을 추가

private static final String TAG = "unity";
private static final String ACTION_USB_PERMISSION = "com.android.usb.USB_PERMISSION";
private UsbManager usbManager;
private UsbDeviceConnection connection;

private PendingIntent permissionIntent;
private boolean permissionGranted = false;

private BroadcastReceiver usbReceiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();
        Log.d(TAG, "USB장치 연결변경 : " + action);

        if (ACTION_USB_PERMISSION.equals(action))
        {
            synchronized(this)
                {
                unregisterReceiver(usbReceiver);
                UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if (intent.getBoolean Extra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) 
                    {
                    if (usbDevice != null)
                    {
                        // USB장치 접근권한 부여받기
                        connectUsbDevice(usbDevice);
                    }
                }
                    else 
                    {
                    Log.d(TAG, "USB장치 접근권한이 없습니다.");
                    //requestPermission (usbDevice);
                }
            }
        }
    };


    // Resume Unity
    @Override protected void onResume()
    {
        super.onResume();

        if (MultiWindowSupport.getAllowResizableWindow(this))
            return;

        mUnityPlayer.resume();

        openUsbDevice();
    }

    private void openUsbDevice()
    {
        // usb 받기
        usbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);

        // 연결된 USB장치 가져오기
        HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
        Iterator<UsbDevice> deviceIterator deviceList.values().iterator();

        if (deviceIterator.hasNext())
        {
            UsbDevice device = deviceIterator.next();
            permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
            filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
            filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
            Log.e(TAG, "getDevicelist: " + device.getVendorId() + "==" + device.getProductId());
            //등록
            registerReceiver(usbReceiver, filter);
            usbManager.requestPermission(device, permissionIntent);
        }
        else
        {
            Log.d(TAG, "USB를 찾을 수 없습니다.");
            Toast.makeText(this, "USB를 찾을 수 없습니다.", Toast.LENGTH_SHORT).show();
        }
    }

    private void connectUsbDevice(UsbDevice usbDevice)
    {
        if (usbDevice == null)
        {
            return;
        }

        connection = usbManager.openDevice(usbDevice);
        if (connection != null)
        {
            // USB 작업
            Log.d(TAG, "USB연결");
            Toast.makeText(this, "USB연결", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Log.d(TAG, "USB를 연결할 수 없습니다.");
            Toast.makeText(this, "USB를 연결할 수 없습니다.", Toast.LENGTH_SHORT).show();
        }
    }
}

 

반응형

댓글