Unity XR.Settings.LoadDeviceByName() not loading device - android

I have tried to use XRSettings.LoadDeviceByName() but it never work. I have already check if the parameter device name is same as the current device name but nothing changes. Also, I have added the None in the sdk list
The XRSettings.loadedDeviceName is cardboard when initiating.
IEnumerator testThis() {
if (string.Compare(XRSettings.loadedDeviceName, "", true) != 0) {
XRSettings.LoadDeviceByName("none");
some_text.text = XRSettings.loadedDeviceName;
yield return new WaitForSeconds(1);
XRSettings.enabled = true;
some_text.text = "Device name " + XRSettings.loadedDeviceName;
}
}
IEnumerator temp() {
some_text.text = "Device name " + XRSettings.loadedDeviceName;
yield return new WaitForSeconds(10);
StartCoroutine(testThis());
}
No matter how I detect, the device name is still cardboard.
https://docs.unity3d.com/ScriptReference/XR.XRSettings.LoadDeviceByName.html
What I have done:
Make None as one of the Virtual Reality SDKs
Use XR-Plugin Management
2.1 Disable Virtual Reality Supported
2.2 Convert camera to XR-Rig
2.3 Use the code about turning VR off in XR-Plugin Management
2.4 Remarks: all the procedure in step 2 is work in iOS build but fail in android build.
2.5 https://docs.unity3d.com/Packages/com.unity.xr.management#4.0/manual/index.html
Tried XRSettings.LoadDeviceByName("none"); XRSettings.LoadDeviceByName(""); XRSettings.LoadDeviceByName("None");

Make sure that ''none'' is first in your VR SDK's, Then Cardboard.
I see a problem with your compare, there is no value
if (string.Compare(XRSettings.loadedDeviceName, "", true) != 0) {
If I understand correctly what you want to do, this code down here will receive a device name and will trigger the LoadDevice couroutine with this name.
public void VerifyDeviceName(string newDeviceName)
{
if (newDeviceName == "none")
{
Debug.Log("Will load none device");
StartCoroutine(LoadDevice(newDeviceName));
}
if (newDeviceName == "cardboard")
{
Debug.Log("Will load cardboard device");
StartCoroutine(LoadDevice(newDeviceName));
}
else
{
Debug.Log("Can't find device with name " + newDeviceName);
}
}
IEnumerator LoadDevice(string newDeviceName)
{
XRSettings.LoadDeviceByName(newDeviceName);
yield return new WaitForSeconds(10);
XRSettings.enabled = true;
Debug.Log("Loaded " + newDeviceName);
}
Note; This will obviously not work in editor you wil get a message saying you cannot trigger on/off vr in editor mode.

I just got through this by brute forcing it. The secret is not to Load device by name. If your trying to load "none" you should just initialize and deinitialize your XR loader.
Disable "Initialize XR on Startup" in Project Settings -> XR Plugin Management
In the code use these two methods
IEnumerator StartXR(){
yield return XRGeneralSettings.Instance.Manager.InitializeLoader();
if(XRGeneralSettings.Instance.Manager.activeLoader == null){
Debug.Log("initialization FAILED");
}
else{
XRGeneralSettings.Instance.Manager.activeLoader.Start();
}
}
void StopXR(){
XRGeneralSettings.Instance.Manager.activeLoader.Stop();
XRGeneralSettings.Instance.Manager.activeLoader.Deinitialize();
}
(Sorry I am terrable at code snippits! I'm new to answering)
Then call these as you need them. It should turn on and turn off XR in your project

Related

How to turn on the torch/flashlight with GooglePlay Services Vision API Xamarin Android

I have been trying to implement the flashlight/torch feature of the camera using the GooglePlay Services Vision API (using Nuget from Visual Studio) for the past few days without success. I have noticed that there is a GitHub implementation of this API which has such functionality but that is only available to Java users.
I was wondering if there is anything related to C# Xamarin users.
The Camera object is not made available on this API therefore I am not able to alter the Camera parameters needed to activate the flashlight.
I would like to be sure if that functionality is not available so I don't waste more time over this. It just might be the case that the Xamarin developers have not attended to this functionality and they might in a near future.
UPDATE
https://github.com/googlesamples/android-vision/blob/master/visionSamples/barcode-reader/app/src/main/java/com/google/android/gms/samples/vision/barcodereader/BarcodeCaptureActivity.java
In there you can see that on line 214 we have such method call:
mCameraSource = builder.setFlashMode(useFlash ? Camera.Parameters.FLASH_MODE_TORCH : null).build();
SetFlashMode is not a method of the CameraSource in Nuget, but it is on the GitHub (open source version).
Xamarin Vision Library Didn't expose the method to set Flash Mode.
WorkAround.
Using Reflection. You can get the Camera Object from CameraSouce and add the flash parameter then set the updated parameters to the camera.
This should be called after surfaceview has been created
Code
public Camera getCameraObject (CameraSource _camSource)
{
Field [] cFields = _camSource.Class.GetDeclaredFields ();
Camera _cam = null;
try {
foreach (Field item in cFields) {
if (item.Name.Equals ("zzbNN")) {
Console.WriteLine ("Camera");
item.Accessible = true;
try {
_cam = (Camera)item.Get (_camSource);
} catch (Exception e) {
Logger.LogException (this, e);
}
}
}
} catch (Exception e) {
Logger.LogException (this, e);
}
return _cam;
}
public void setFlash (bool isEnable)
{
try {
isTorch = !isEnable;
var _cam = getCameraObject (mCameraSource);
if (_cam == null) return;
var _pareMeters = _cam.GetParameters ();
var _listOfSuppo = _cam.GetParameters ().SupportedFlashModes;
_pareMeters.FlashMode = isTorch ? _listOfSuppo [0] : _listOfSuppo [3];
_cam.SetParameters (_pareMeters);
} catch (Exception e) {
Logger.LogException (this, e);
}
}
Basically, anything you can do with Android can be done with Xamarin.Android. All the underlying APIs area available.
Since you have existing Java code, you can create a binding project that enables you to call the code from your Xamarin.Android project. Here's a good article on how to get started: Binding a Java Library
On the other hand, I don't think you need a library to do what you want to. If you only want torch/flashlight functionality, you just need to adapt the Java code from this answer to work in Xamarin.Android with C#.

How to access texture/models from cloud/WEB at run time in android app using Unity 3D?

I have been trying to download and load the texture or other models from a WEB at run time by using WWW class.
The app works well on Unity Editor but when I deploy it on android device it doesn't load/download the texture.
Please somebody guide me.
I have used the following code in script.
void Start()
{
StartCoroutine(loadMovie());
}
IEnumerator loadTexture()
{
WWW www = new WWW("http://images.earthcam.com/ec_metros/ourcams/fridays.jpg");
yield return www;
// Debug.Log(www.movie.duration + " <--- wwww");
if (www.error != null)
{
Debug.Log("Error: Can't load texture! - " + www.error);
yield break;
}
else
{
Texture2D texture = www.texture as Texture2D;
Debug.Log("Texture loaded");
Debug.Log(www.texture);
gameObject.GetComponent<Renderer>().material.mainTexture = texture;
}
}
First of all, remove Debug.Log(www.texture); from the code, then re-try your code.
You have IEnumerator loadTexture() but you are calling StartCoroutine(loadMovie()); . This means that you are likely calling another function or maybe that's a type in your question.
void Start()
{
StartCoroutine(loadTexture());
}
IEnumerator loadTexture()
{
WWW www = new WWW("http://images.earthcam.com/ec_metros/ourcams/fridays.jpg");
//Wait for download to finish
while(!www.isDone){
Debug.Log("Progress: "+www.progress);
yield return null;
}
if ((www==null) || (www.error != null))
{
Debug.Log("Error: Can't load texture! - " + www.error);
yield break;
}
gameObject.GetComponent<Renderer>().material.mainTexture = www.texture;
}
Not compiled but should work.
EDIT:
From the file you sent, change the Minimum API Level to 4.1.

Unity3d Input.location from documentation not available in code

I am working on a Unity3d for Android project. Using the documentation from Unity:
http://unity3d.com/support/documentation/ScriptReference/Input-location.html
I should be able to use Input.location to get access to GPS location data. But instead I get an error basically telling me the Input.location is not part of Unity.
Assets/Scripts/Prototype1.js(27,29): BCE0019: 'location' is not a
member of 'UnityEngine.Input'.
I've checked for updates and it tells me the system is fully up to date. I'm running version 3.4.2f3
Is the documentation outdated? Is there a different reference to the LocationService? How can I get the location data?
The files at unity3d.com contain the documentation for the most recent release Unity3D 3.5. If you look at your documentation in the local file system you won't find Input.location. Seems like they have changed the interface in Unity3D 3.5.
I haven't used GPS until now, maybe this thread and the links provided can throw some light on this:
How to import GPS location coordinates from Android device?
I found documentation to the old code, based in iPhoneSettings. This is the code I'm using now, it's not complete (doesn't respond to time out of the location service and other such well rounded behavior), but demonstrates the code.
function Start () {
locationStatus = LocationServiceStatus.Stopped;
StartCoroutine(startLocationService());
}
function Update () {
if(locationStatus == LocationServiceStatus.Running)
{
/*
lat = Input.location.latitude;
lon = Input.location.longitude;
alt = Input.location.altitude;
*/
lat = iPhoneInput.lastLocation.latitude;
lon = iPhoneInput.lastLocation.longitude;
alt = iPhoneInput.lastLocation.altitude;
}
}
function startLocationService()
{
//Input.location.Start(accuracy, distance);
iPhoneSettings.StartLocationServiceUpdates(accuracy, distance);
//while(Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) {
while(iPhoneSettings.locationServiceStatus == LocationServiceStatus.Initializing && maxWait > 0)
{
yield WaitForSeconds(1);
maxWait--;
}
//locationStatus = Input.location.status;
locationStatus = iPhoneSettings.locationServiceStatus;
}

unable to connect to JSON service in android application of Titanium studio

i am trying to do login application which takes id and password..when i click on logi button then it will connect to our local server by JSON..with the specified URL..the code is..
var loginReq = Titanium.Network.createHTTPClient();
loginReq.onload = function()
{
var json = this.responseText; alert(json);
var response = JSON.parse(json);
if (response.data.status == "success")
{ alert("Welcome ");
}
else
{ alert(response.data.status);
}
};
loginReq.onerror = function(event)
{
alert(event.toSource());
//alert("Network error");
};
loginBtn.addEventListener('click',function(e)
{ if (username.value != '' && password.value != '')
{
var url = 'our local url action=login&id='+username.value+'&pwd='+password.value;
loginReq.open("POST",url);
loginReq.send();
}
else
{
alert("Username/Password are required");
}
});
Here it is not connecting our URl..so it is entering into loginReq.onerror function...instead of loginReq.onload function..why it is throwing run time error.. The same code working fine with Iphone..
The Run Time Error is..
TypeError:Cannot call property toSource in object{'source':[Ti.Network.HttpClient],specified url} is not a function,it is a object.
This is wat the error..please let me Know...
Apparently the toSource() function does not exist in android, as it is an object. Try debugging and see what the object event contains.
You could do that by adding a line above the alert line, and adding a debug line to it.
Look in debug mode and see all variables
"toSource()" is not a documented function for either platform, and I also do not see it in the source for Titanium Mobile. If you aren't getting the error on iOS, I'm guessing it is because the error handler isn't getting called. Perhaps your emulator or device does not have internet access, whereas your iOS simulator or device does?
Regardless, error handling in the HTTPClient normally looks something like this:
loginReq.onerror = function(e)
{
Ti.API.info("ERROR " + e.error);
alert(e.error);
};

android is it possible for an app to detect when it is running on emulator vs device

I develop a game that is heavily dependent upon timing, when I run it in the emulator it executes understandably slower than when I run it on my phone. This forces me to up all the "stats" in my game, so that I can actually "beat it" when I am developing - when Debugging, the game is unwinnable.
Is there a call, or variable, or something that I can use to determine whether I am currently running on the Emulator and not a device.
I've considered trying to detect if Framerate is low.
I've considered trying to read the "device name" from some sort of build in system field.
But neither seems like a very good method to pursue.
Any help would be great.
Use the Build.DEVICE value and compare to "sdk".
First idea:check the network operator, on the emulator, it's always equal to "Android". Not documented and just a guess that it will work everytime!
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String networkOperator = tm.getNetworkOperatorName();
if("Android".equals(networkOperator)) {
// Emulator
}
else {
// Device
}
Second idea: the manufacturer:
public boolean isEmulator() {
return Build.MANUFACTURER.equals("unknown");
}
Third idea: seem th best one, check the debug key:
static final String DEBUGKEY =
"get the debug key from logcat after calling the function below once from the emulator";
public static boolean signedWithDebugKey(Context context, Class<?> cls)
{
boolean result = false;
try {
ComponentName comp = new ComponentName(context, cls);
PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(),PackageManager.GET_SIGNATURES);
Signature sigs[] = pinfo.signatures;
for ( int i = 0; i < sigs.length;i++)
Log.d(TAG,sigs[i].toCharsString());
if (DEBUGKEY.equals(sigs[0].toCharsString())) {
result = true;
Log.d(TAG,"package has been signed with the debug key");
} else {
Log.d(TAG,"package signed with a key other than the debug key");
}
} catch (android.content.pm.PackageManager.NameNotFoundException e) {
return false;
}
return result;
}
from here: How can I detect when an Android application is running in the emulator?
If you're using Google APIs, you want:
"google_sdk".equals( Build.PRODUCT );
If not, you'll want to use:
"sdk".equals( Build.PRODUCT );
The older (since deprecated) way to do this was to check ANDROID_ID, which is null on the AVD, but this doesn't work on API 7 and above:
// ONLY WORKS ON 2.1 AND BELOW
String android_id = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
if (android_id == null) {
// Emulator!
} else {
// Device
}
Dan S gave a good answer about how to detect when running on the emulator. However, a few tips:
Instead of relying on something in the SDK, why not just set up your own flag? Just keep a public final static boolean isEmulator that you change from true to false depending on the environment, and build your code with ifs and elses around it. The Build.DEVICE method is not 100% safe, since some rooted devices might have that borked.
Low framerate detection could be a good thing to implement. Given the wide range of Android devices, it might prove helpful on lower-end ones.

Categories

Resources