How to detect BLE beacons on Unity3D HoloLens app? - c#

I have built an app in Unity3D which should be able to detect Bluetooth Low-Energy beacons on the Microsoft HoloLens. Here is the Unity C# script code that I used to get this done.
using UnityEngine;
using Windows.Devices.Bluetooth.Advertisement;
public class BeaconDetector : MonoBehaviour
{
private BluetoothLEAdvertisementWatcher _watcher;
void Start()
{
_watcher = new BluetoothLEAdvertisementWatcher();
_watcher.Received += WatcherOnReceived;
_watcher.Start();
}
//This method should be called when a beacon is detected
void WatcherOnReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
//Just a simple check if this method is even called
Debug.Log("Beacon detected!");
}
}
The app builds and runs on the HoloLens nicely, but (even after waiting a couple of minutes) I do not get the desired output line in my debug log. This means to me that the WatcherOnReceived() method is never called, which ultimately means that no beacon is detected.
I use some Sensoro SmartBeacon-4AA's which can transmit both iBeacon and Eddystone signals.
I have been trying this for a couple of weeks right now, did several tutorials along the way but I still can not figure out why this is not working for me.

It was a permission problem after all. It is possible to solve this issue in both Unity (pre-build) and Visual Studio (post-build). Here are both solutions:
Solution A: in Unity
Head to Edit > Project Settings > Player.
In the Inspector, open up the tab Publishing Settings.
Under the Capabilities section, make sure that Bluetooth is enabled.
Click on the .gif to expand
Solution B: in Visual Studio (2017)
In your Solution Explorer (Ctrl+Alt+L), expand your project.
Double-click the Package.appxmanifest.
Go to the Capabilities tab.
Again, make sure that Bluetooth is enabled.
Click on the .gif to expand
This will give your app the right to use Bluetooth on the HoloLens.

Related

ANDROID - How to force ANR with Unity

I'm making a game with Unity for Android. I want to generate a ANR for testing purposes, and when I say ANR I mean that Android displays that window indicating the app is not responding, I want that window appearing.
I tried using a Thread.Sleep(); and a while (true) { }, but had no effect. The game freezes and is unresponsive, but the Android window saying "App not responding" is not appearing.
Also, I want to generate it with C#. I know using Java files to generate an ANR directly on the Android UI thread works, but I want to provoke it just using Unity code.
Hope someone can help!

Keep Unity program working in background after device goes to sleep?

I'm working on a Unity project that uses the device camera for AR purposes (I'm using AR Foundation). I currently have it set so that the device won't go to sleep (because I want it to operate for long-periods without user-interaction). Specifically, Screen.sleepTImeout = SleepTimeout.NeverSleep. If I didn't have this, then the device would go to sleep, and the program would stop functioning. Does anyone know if there is a way to keep the program working in the background after the device goes to sleep...similar to how music apps keep playing music? I've come up empty handed after a few hours of searching for a solution. Any help would be greatly appreciated!!!
Some people aren't finding this in more recent versions of Unity. But it's still there: In Player settings, under the section "Resolution and Presentation", you find "Resolution" and below that, there's "Run In Background".
This is only available for Web players and standalones (Anything Except IOS And Android Pretty Much), though (you won't find it when you have the iOS, Android or Flash tab selected.

C++ Unity Plugin how to release USB audio device

I've created a C++ Unity plugin that sends audio to a speaker via USB.
Everything works fine the first time I hit play on Unity, however, after the first playback if I hit play once more then unity crashes. In addition, I have to occasionally reset my computer to get the plugin working.
I'm thinking that I need to add the proper command to my "OnDestroy" function in my .CS Unity marshaling script. Here's what I have right now...
private void OnDestroy()
{
Audio.stop_sound();
Debug.Log("stop called");
}
stop_sound is a simple function that exhibits a stop to the callback.
Any ideas?

Unity Game Not working even after building

My game is not working even after successfully building in the PC, Mac standalone.
I tried to build it both in 32 bit and 64 bit but the .exe is still not opening. The game is running in unity but not after building it. I've also tried to change the compatibility settings in the properties but nothing works.
Here's the Game link:
https://drive.google.com/open?id=0B7LawZAwfUJUUXNyU3hXS3ByQTQ
Here's My output_log.txt:
Mono path[0] = 'H:/Projects/Unity/FirstOne/Final/Roll A Ball ;)_Data/Managed'
Mono path[1] = 'H:/Projects/Unity/FirstOne/Final/Roll A Ball ;)_Data/Mono'
Mono config path = 'H:/Projects/Unity/FirstOne/Final/Roll A Ball ;)_Data/Mono/etc'
PlayerConnection initialized from H:/Projects/Unity/FirstOne/Final/Roll A Ball ;)_Data (debug = 0)
PlayerConnection initialized network socket : 0.0.0.0 55357
Multi-casting "[IP] 192.168.1.110 [Port] 55357 [Flags] 3 [Guid] 3232448567 [EditorId] 4073115951 [Version] 1048832 [Id] WindowsPlayer(Aashish-PC) [Debug] 1" to [225.0.0.222:54997]...
Waiting for connection from host on [0.0.0.0:55357]...
PlayerConnection accepted from [192.168.1.110] handle:0x344
Started listening to [0.0.0.0:55357]
Using monoOptions --debugger-agent=transport=dt_socket,embedding=1,defer=y,address=0.0.0.0:56567
When you build your game you get a folder called YourGame_Data, inside there's an output_log.txt. Look for logs or errors there to debug your build.
Edit: It seems in the end, the problem was the ;) in the game name.
When you build the .exe , it comes with a data folder for .exe to use. They need to be in the same folder.
Also someone says here looks like the same problem
So i fixed the problem. Now the Built game started immediatly.
I Unticked "Display Resolution Dialog".
And after, built the game two times to make sure it was the problem. And it was
Also can you add more details like;
-Does it give an error code?
-Does it stuck in loading screen?

Windows Phone 8 Accelerometer events

I'm making my first game for Windows Phone (XNA). I use Accelerometer to change the position of a crosshair on the screen:
Here is the code in my Initialize() function (note that Accelerometer is local variable declared only in this function):
Accelerometer accelerometer = new Accelerometer();
accelerometer.CurrentValueChanged += accelerometer_CurrentValueChanged;
accelerometer.Start();
And the event handler:
void accelerometer_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
{
lock (accelerometerVectorLock)
{
accelerometerVector = new Vector3(
(float)e.SensorReading.Acceleration.X,
(float)e.SensorReading.Acceleration.Y,
(float)e.SensorReading.Acceleration.Z);
}
}
This works fine on Windows Phone Emulator, and on my Nokia Lumia 520 connected to the computer and launching from Visual Studio, however when I launch the game in the phone (not connected to the computer), the accelerometer_CurrentValueChanged event appears to be called only once, on application startup.
My solution was to make the accelerometer a member of my Game class, then code in Initialize() like this:
accelerometer = new Accelerometer();
accelerometer.CurrentValueChanged += accelerometer_CurrentValueChanged;
accelerometer.Start();
So my question is, why does this solution work? And why is there a difference between application launched from VS and normally, even on the same device?
Why this solution works?
This solution works because you're keeping a reference to the accelerometer. Windows Phone applications, like all .NET applications, use an automated system for memory management. A background process, called garbage collector, inspects the objects in a regular basis, detects those who are not referenced anymore, and clean them. If you declare accelerometer as a local variable, it won't be referenced anymore when the function exits, and will therefore be cleaned. When you declare it as a member of your class, it will be alive for as long as your class lives.
Why the difference between application launched from VS and normally, on the same device?
When launching the code from Visual Studio, a debugger is attached. To help you debugging, it has some impacts on the way the code executes. Notably, it makes the garbage collector way less aggressive. It explains why you didn't have this issue when testing with the debugger attached. Note that you can achieve the same result by pressing Control + F5 in Visual Studio: it'll start the application without attaching the debugger.

Categories