using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class AndroidUsbPermission {
private static AndroidUsbPermission m_instance = null;
public static AndroidUsbPermission GetInstance() {
if (m_instance == null)
{
m_instance = new AndroidUsbPermission();
}
return m_instance;
}
private AndroidUsbPermission() {
initAndroid();
}
#region usb permissions
private bool m_permissionOK = false;
AndroidJavaObject m_usbPermissionManager = null;
private const int Vive_VendorId = 0x28de;
private const int Vive_Tracker_ProductId = 0x2300;//tracker、controller直连
private const int Vive_Dongle_ProductId = 0x2101;//dongle的产品号
private void initAndroid()
{
AndroidJavaClass usb_ajc = new AndroidJavaClass("com.lt.usbhostmanager.UsbHostPermissionManager");
if (usb_ajc != null)
{
m_usbPermissionManager = usb_ajc.CallStatic<AndroidJavaObject>("getInstance");
m_usbPermissionManager.Call("setActivity", getUnityActivity());
}
}
private int usb_ret = -99;
private List<int> m_trackerFds = new List<int>();
private List<int> m_dongleFds = new List<int>();
public void requestUsbHostPermission()
{
if (m_usbPermissionManager == null)
{
return;
}
m_permissionOK = false;
m_totalUsbNum = 0;
m_trackerFds.Clear();
m_dongleFds.Clear();
usb_ret = m_usbPermissionManager.Call<int>("requestPermissions", Vive_VendorId, Vive_Tracker_ProductId, Vive_Dongle_ProductId, new UsbNumCallback(setTotalUsbNumFromAndroid), new UsbPermissionCallback(setUsbPermissionFromAndroid));
}
private void doToast(string msg)
{
if (m_usbPermissionManager == null)
{
return;
}
m_usbPermissionManager.Call("doToast", msg);
}
private AndroidJavaObject getUnityActivity()
{
//unity的manifest文件路径
//C:\Program Files\Unity2018.2.3f1\Editor\Data\PlaybackEngines\AndroidPlayer\Apk
//获取unity activity
//通过报名获取java class
AndroidJavaClass unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
//获取当前的activity
AndroidJavaObject unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
return unityActivity;
}
private class UsbNumCallback : AndroidJavaProxy
{
private UnityAction<int> unity_callback;
public UsbNumCallback(UnityAction<int> unity_callback) : base(new AndroidJavaClass("com.lt.usbhostmanager.UsbNumCallback"))
{
this.unity_callback = unity_callback;
}
public void onCallback(int totalNum)
{
if (unity_callback != null)
{
unity_callback(totalNum);
}
}
}
private class UsbPermissionCallback : AndroidJavaProxy
{
private object m_obj = new object();
private UnityAction<int, int, int, bool> unity_callback;
public UsbPermissionCallback(UnityAction<int, int, int, bool> unity_callback) : base(new AndroidJavaClass("com.lt.usbhostmanager.UsbPermissionCallback"))
{
this.unity_callback = unity_callback;
}
public void onCallback(int vendorId, int productId, int fd, bool result)
{
lock (m_obj)
{
if (unity_callback != null)
{
unity_callback(vendorId, productId, fd, result);
}
}
}
}
private int m_totalUsbNum;
private void setTotalUsbNumFromAndroid(int totalNum)
{
m_totalUsbNum = totalNum;
}
private void setUsbPermissionFromAndroid(int vendorId, int productId, int fd, bool result)
{
if (result)
{
if (vendorId == Vive_VendorId)
{
if (productId == Vive_Tracker_ProductId)
{
m_trackerFds.Add(fd);
}
else if (productId == Vive_Dongle_ProductId)
{
m_dongleFds.Add(fd);
}
}
}
//响应数等于usb设备数,可以进行读数据,这里是异步所以只设置标记
if (m_trackerFds.Count + m_dongleFds.Count == m_totalUsbNum)
{
m_permissionOK = true;
}
}
public bool IsPermissionOK()
{
return m_permissionOK;
}
public int[] getTrackerFds()
{
return m_trackerFds.ToArray();
}
public int[] getDongleFds()
{
return m_dongleFds.ToArray();
}
public string getDebug()
{
string msg_android = "usb_ret=" + usb_ret + "\n";
msg_android += "totalUsbNum:" + m_totalUsbNum + "\n";
//
string tracker_fd_msg = "tracker fds: ";
for (int k = 0; k < m_trackerFds.Count; k++)
{
tracker_fd_msg += m_trackerFds[k] + ", ";
}
tracker_fd_msg += "\n";
msg_android += tracker_fd_msg;
//
string dongle_fd_msg = "dongle fds: ";
for (int k = 0; k < m_dongleFds.Count; k++)
{
dongle_fd_msg += m_dongleFds[k] + ", ";
}
dongle_fd_msg += "\n";
msg_android += dongle_fd_msg;
//
return msg_android;
}
#endregion
https://blog.csdn.net/osuckseed/article/details/101302796
//string[] permissions = new string[] {
// "android.permission.INTERNET",
// "android.permission.READ_PHONE_STATE",
// "android.permission.READ_EXTERNAL_STORAGE",
// "android.permission.WRITE_EXTERNAL_STORAGE",
// "android.permission.ACCESS_WIFI_STATE",
// "android.permission.ACCESS_NETWORK_STATE",
// "android.permission.GET_TASKS",
// "android.permission.REQUEST_INSTALL_PACKAGES",
// "android.permission.WAKE_LOCK",
// "android.permission.SYSTEM_ALERT_WINDOW",
// "android.permission.CHANGE_WIFI_STATE",
// "android.permission.CHANGE_NETWORK_STATE",
// "android.permission.ACCESS_COARSE_LOCATION",
// "android.permission.ACCESS_FINE_LOCATION",
// "android.permission.SYSTEM_OVERLAY_WINDOW",
// "android.permission.ACCESS_COARSE_UPDATES",
// "android.permission.WRITE_SETTINGS",
// "android.permission.BATTERY_STATS",
// "android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
//};
}
|