Jimmy Engström

NAVIGATION

Let us AllJoyn in the fun of IoT

2016-08-29 10:00:00 +0000

This is my presentation from NDC Oslo 2016. NDC was fantastic, really good conference for both speakers and attendees, I hope to come back next year.

Let us AllJoyn in the fun of IoT - Jimmy Engström from NDC Conferences on Vimeo.

There are billions of IoT Devices and almost as many ways to communicate with them. Alljoyn is a standardized way to talk to IoT devices. With the help of this standard the devices themself can tell you about and expose what functionality you can use. All these things are implemented in Windows 10, we will look at how you can find devices, how to communicate with them and how AllJoyn works.

Setting up a HoloLens Unity project

2016-07-27 10:00:00 +0000

You can create apps for HoloLens by using UWP (ordinary 2D apps), you can also make 3D apps by using tools like Unity.
This post will cover what you need to do to setup a Unity project to work on HoloLens.
 
I have had the opportunity to work with HoloLens for a while now and I should share some of my tips and tricks.

The Tools

You need Unity installed on your computer, follow the instructions here:
http://unity3d.com/pages/windows/hololens
 
While you're at it, I would also recommend installing Visual Studio 2015 and the HoloLens emulator (not needed for this blog post).
https://developer.microsoft.com/en-us/windows/holographic/install_the_tools

Setting things up

Let's take a look at what you need to do to set everything up.
I will show a really simple way to do that.

Create new project

Start with creating a new Unity project.
clip_image001.png
 
Name it something, in my case I named it DemoProject
 
clip_image002.png
 
 
Unity will now create our world.

HoloToolkit

Microsoft also provides HoloToolkit, a library containing a lot of useful components you can use when building HoloLens apps, you also get a couple of menu items that automatically make the changes you need.
You can download it from HoloToolkit.azurewebsites.net (or from github).
This will show a list of files to be imported, just click Import and the installation will take care of everything,
 
clip_image003.gif
 
Now you should see a menu item called HoloToolkit.
clip_image004.png
 
First delete the camera and then add the Camera.prefab in the HoloToolkit/Utilities/Prefabs folder, this camera is customized for holographic development.
In the HoloLens emulator you can move around using the aswd keys.
To enable the same behavior in the Unity player you can add ManualCameraControl.cs (HoloToolkit/Utilities/Scripts) to your camera.
To fix the scene (set the correct position of the camera amongst other things) click "Apply HoloLens Scene Settings".
Now save your scene and place it in a folder called Scenes (or whatever location you prefer).
Now click the "Apply HoloLens Project Settings" it will make sure the near plane is set to a good value and make sure Holographic is available in the project.
You will get a couple of questions you can just select yes and then restart the project (Unity will do that automatically).
 
Now you are all done, and it's time to create your holograms, but that will be the subject of another blog post.
 
If you want to know in detail what changes these scripts made , here is an excellent blog post .
You can also read more here
 

Announcing http://holotoolkit.azurewebsites.net

2016-07-10 17:00:00 +0000

Microsoft has really done some awesome stuff with the HoloLens and the tooling for developing for the HoloLens.
I've been developing for the HoloLens for a while now and one of the first thing I do when starting up a new project is making sure my HoloToolkit GIT clone is up to date and then copying the files to the right place.
I wanted to make this process a little bit easier so I created an Azure script that automatically downloads the source and makes a Unity package out of it, every time something is checked in into the HoloToolkit-Unity repo.

You can get the HoloToolkit as a UnityPackage here:
http://holotoolkit.azurewebsites.net/

Enjoy =)

Download all the videos from Build 2015

2015-05-11 17:25:00 +0000

Build 2015 was epic, there was so much content so Microsoft even recorded some content before build (now available at Channel9).
I usually download everything to my laptop and watch it to and from work, this script will download all videos from Build 2015 =)

This is an updated version of my script I previously blogged about here.

cd "d:\build15"

$pattern = "[{0},\:,\']" -f ([Regex]::Escape([String][System.IO.Path]::GetInvalidPathChars()))             
[Environment]::CurrentDirectory=(Get-Location -PSProvider FileSystem).ProviderPath
$a = ([xml](new-object net.webclient).downloadstring("http://s.ch9.ms/Events/Build/2015/RSS/mp4high"))
$a.rss.channel.item | foreach{ 
    $url = New-Object System.Uri($_.enclosure.url)
    $file = [Regex]::Replace($_.Title + " " + $url.Segments[-1] , $pattern, ' ') 
    Write-host  ("{0}  -  {1}" -f "Downloadning" ,$file)
    if (!(test-path $file))
    {
            (New-Object System.Net.WebClient).DownloadFile($url, $file)
    }
}

 

Just copy the code, paste it into a file called “Build2015.ps1”, create a directory (in my case “c:\Build15”) and change the path in the first line of the script.

Right click on the file and choose run with PowerShell.

 

In case you get a problem similar like “Build2015.ps1 cannot be loaded because running scripts is disabled on this system.”

Start PowerShell as an administrator and run “set-executionpolicy unrestricted”, this is probably a bad thing to do for security.

Build2015.ps1 (677.00 bytes)

Universal apps and Bluetooth LE – Battery Service

2014-06-09 00:32:00 +0000

Some devices exposes a battery service that can supply us with the current (no pun intended) battery level.
I started this blog series working with only Windows phone in mind, but then I realized if it is possible to achieve the same results with an universal app of course that’s the route I should take.

Capabilities

To be able to communicate with Bluetooth low energy (or Bluetooth Smart, as it’s also called) you need to add a capability to your app.
This can’t be done from a GUI, you need to edit the package.appmanifest manually and add the following lines of code just above </Package>.
Don’t forget to do that in both your Windows 8 and Windows Phone projects.

  <Capabilities>
    <m2:DeviceCapability Name="bluetooth.genericAttributeProfile">
      <m2:Device Id="any">
        <m2:Function Type="name:battery"/>
      </m2:Device>
    </m2:DeviceCapability>
  </Capabilities>

Now you are are all set to start coding =)

 

Battery Level Service

The battery service must implement read, and notify is optional.
What that means is that you can always assume that you will be able to read the battery level (if your device implements the battery service, but you have to check if it supports Notify  (a good developer should always check Ler med tungan ute ).

image

The value it returns is a byte with a value from 0 to 100 representing a percentage of the current charge (0 being fully discharged).

The property

This is just a standard property with a backing store.

 private int _BatteryLevel;
        public int BatteryLevel
        {
            get
            {
                return _BatteryLevel;
            }
            set
            {
                _BatteryLevel = value;
                OnPropertyChanged();
            }
        }

The interesting part is how we handle OnPropertyChanged to try to avoid “The application called an interface that was marshalled for a different thread”.
I found a very neat piece of code here.


My implementation looks like this:

        public event PropertyChangedEventHandler PropertyChanged;

        private readonly CoreDispatcher _dispatcher;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            if (_dispatcher == null || _dispatcher.HasThreadAccess)
            {
                var eventHandler = this.PropertyChanged;
                if (eventHandler != null)
                {
                    eventHandler(this,
                        new PropertyChangedEventArgs(propertyName));
                }
            }
            else
            {
                IAsyncAction doNotAwait =
                    _dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                    () => OnPropertyChanged(propertyName));
            }
        }

 

 public BatteryServicePage()
        {
            if (!DesignMode.DesignModeEnabled)
            {
                _dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
            }
          ........
        }


Reading a value

I have written a simple method that reads a value from the service and if it supports notification it will also start a subscription on those notifications.

 public async Task<byte[]> GetValue(Guid gattCharacteristicUuids)
        {
            try
            {
                var gattDeviceService = await GattDeviceService.FromIdAsync(Device.Id);
                if (gattDeviceService != null)
                {
                    var characteristics = gattDeviceService.GetCharacteristics(gattCharacteristicUuids).First();

                    //If the characteristic supports Notify then tell it to notify us.
                    try
                    {
                        if (characteristics.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Notify))
                        {
                            characteristics.ValueChanged += characteristics_ValueChanged;
                            await characteristics.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
                        }
                    }
                    catch { }

                    //Read
                    if (characteristics.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Read))
                    {
                        var result = await characteristics.ReadValueAsync(BluetoothCacheMode.Uncached);

                        if (result.Status == GattCommunicationStatus.Success)
                        {
                            byte[] forceData = new byte[result.Value.Length];
                            DataReader.FromBuffer(result.Value).ReadBytes(forceData);
                            return forceData;
                        }
                        else
                        {
                            await new MessageDialog(result.Status.ToString()).ShowAsync();
                        }
                    }
                }
                else 
                {
                    await new MessageDialog("Access to the device has been denied =(").ShowAsync();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return null;
        }

 

You can simply call the method like this:

BatteryLevel = Convert.ToInt32((await GetValue(GattCharacteristicUuids.BatteryLevel))[0]);

 

You also need a callback method that can handle the notifications.

  void characteristics_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
        {
            byte[] data = new byte[args.CharacteristicValue.Length];
            Windows.Storage.Streams.DataReader.FromBuffer(args.CharacteristicValue).ReadBytes(data);

           //Update properties
            if (sender.Uuid == GattCharacteristicUuids.BatteryLevel)
            {
                BatteryLevel = Convert.ToInt32(data[0]);
            }
        }

 

Now your app can retrieve battery level from a Bluetooth LE device. How awesome is that?

BatteryDemo.zip (289.18 kb)