LoginSignup
0
0

Viveデバイスの情報を取得する

Last updated at Posted at 2023-08-07

UnityでVive Tracker・Vive ControllerのIDや接続状態、電池残量など情報をしたかった

接続状態を取得する

OpenVR.System.GetTrackedDeviceActivityLevelの第一引数に「接続されているデバイスの番号」を代入すると取得できる

ViveDevice.cs
using Valve.VR;
public class ViveDevices : MonoBehaviour
{
    void Update()
    {
        if(OpenVR.System.GetTrackedDeviceActivityLevel(0) == EDeviceActivityLevel.k_EDeviceActivityLevel_Idle)
        {
            // Idle状態(SteamVRでデバイスが点滅している状態)
        }
        else
        {    
            // Active状態
        }
}

IDを取得する

OpenVR.System.GetStringTrackedDevicePropertyの第一引数に「接続されているデバイスの番号」
第二引数に「ETrackedDeviceProperty.Prop_SerialNumber_String」を代入すると取得できる

ViveDevice.cs
using Valve.VR;
public class ViveDevices : MonoBehaviour
{
    void Start()
    {
        string serialNumber = GetStringProperty(0, ETrackedDeviceProperty.Prop_SerialNumber_String);
    }
    
    string GetStringProperty(uint i, ETrackedDeviceProperty property)
    {
        ETrackedPropertyError error = ETrackedPropertyError.TrackedProp_Success;
        uint bufSize = OpenVR.System.GetStringTrackedDeviceProperty(i, property, null, 0, ref error);
        if (error != ETrackedPropertyError.TrackedProp_BufferTooSmall)
        {
            return null;
        }
        System.Text.StringBuilder buff = new System.Text.StringBuilder();
        buff.Length = (int)bufSize;
        OpenVR.System.GetStringTrackedDeviceProperty(i, property, buff, bufSize, ref error);
        if (error != ETrackedPropertyError.TrackedProp_Success)
        {
            return null;
        }

        return buff.ToString();
    }
}

電池残量を取得する

OpenVR.System.GetFloatTrackedDevicePropertyの第一引数に「接続されているデバイスの番号」
第二引数に「ETrackedDeviceProperty.Prop_DeviceBatteryPercentage_Float」を代入すると取得できる

ViveDevice.cs
using Valve.VR;
public class ViveDevices : MonoBehaviour
{
    void Start()
    {
        float trackerDevicesBattery = GetFloatProperty(0, ETrackedDeviceProperty.Prop_DeviceBatteryPercentage_Float);
    }
    
    float GetFloatProperty(uint i, ETrackedDeviceProperty property)
    {
        ETrackedPropertyError error = ETrackedPropertyError.TrackedProp_Success;
        return OpenVR.System.GetFloatTrackedDeviceProperty(i, property, ref error);
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0