BLE on WP8

For the last 9 months or so I have been chomping at the bit wanting to try my hand at Bluetooth Low Energy on a WP8 device. Up until now BLE has been missing from WP8 even thou the hardware has supported it for quite some time. With the release of Nokia’s Cyan update BLE is now enable on Nokia device that have BLE radios…

Sweet :slight_smile:

Now here’s my first stumbling block…My new mate in Italy had sent me a Nokia 620 in preparation for some BLE magic for the Cyan update as my HTC 8X doesn’t have BLE hardware…

Meah…As I write this the Cyan update has only available for the Nokia 625 and Nokia 925 in the UK with the other models going thru release testing at the moment…

Then it dawned on me…I was in Cambridge at day job number 2 yesterday and being a large corporate they have a wicked IT department with all the latest toys…So I canter down to the 1st floor and start off on this sob story about how left out I am and if only I had a Cyan enable device to test out this mythical BLE magic on a phone…Well there was a shiny Nokia 930 sitting on his desk so I yelled out “Look at that out the window…” as soon as he turned to look out the window I grabbed the 930 and legged it out the door and back to my office on the 3rd floor giggling like a little school girl…

Ok the real story was…Here you go, give it back next week…Either way I have a sweet Nokia 930 to play with for the next week…

So how hard is it to get a WP8 BLE enabled device to talk to a NETMF BLE enabled device?

One word… Trivial :slight_smile:

The exact same code I used for my Win8.1 store app from last year works an absolute treat on WP8

As you can see it’s pretty vanilla.

using System;
using System.Collections.Generic;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Enumeration;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Navigation;

namespace WP_App
{
    public sealed partial class MainPage : Page
    {
        private DateTime _sliderLastChanged = DateTime.Now;
        private readonly Guid _bleAddress = new Guid("2AC94B65-C8F4-48A4-804A-C03BC6960B80");
        private readonly Guid _writeGuid = new Guid("50E03F22-B496-4A73-9E85-335482ED4B12");
        private readonly Guid _readGuid = new Guid("4FD800F8-D3B6-48F9-B232-29E95984F76D");
        private GattDeviceService _bluetoothGattDeviceService;
        private IReadOnlyList<GattCharacteristic> _writeCharacteristic;
        public MainPage()
        {
            this.InitializeComponent();
            this.NavigationCacheMode = NavigationCacheMode.Required;
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }
        private async void UxButtonDiscover_Click(object sender, RoutedEventArgs e)
        {
            UxListBoxDevices.Items.Clear();
            var devices = await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(_bleAddress));
            if (devices.Count > 0)
            {
                foreach (var device in devices)
                {
                    UxListBoxDevices.Items.Add(device);
                }
            }
        }

        private async void UxListBoxDevices_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var device = UxListBoxDevices.SelectedItem as DeviceInformation;
            _bluetoothGattDeviceService = await GattDeviceService.FromIdAsync(device.Id);
            _writeCharacteristic = _bluetoothGattDeviceService.GetCharacteristics(_writeGuid);
            if (_bluetoothGattDeviceService != null && _writeCharacteristic != null)
            {
                GattCharacteristic incominfData = _bluetoothGattDeviceService.GetCharacteristics(_readGuid)[0];
                incominfData.ValueChanged += incominfData_ValueChanged;
                await incominfData.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
            }
        }

        void incominfData_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
        {
            //incoming data
        }
        private async void SendData(string val)
        {
            var writer = new DataWriter();
            writer.WriteString(val + "\r\n");
            await _writeCharacteristic[0].WriteValueAsync(writer.DetachBuffer());
        }
        private void UxSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
        {
            DateTime now = DateTime.Now;
            TimeSpan t = now - _sliderLastChanged;
            if (t.Milliseconds > 100)
            {
                _sliderLastChanged = now;
                SendData(UxSlider.Value.ToString());
            }
        }
    }
}

On the NETMF side I have a mainboard running NETMF 4.3.1 with a Bluegiga BLE113 based xBee which basically acts as a serial pipe - so serial in - serial out. The Bee advertises it’s GATT characteristics which my WP8 is able to talk to. Simple as that…

4 Likes

@ Justin - Awesome. So, I have a Lumia 1520 with Cyan and just ran your code. I know very little about BLE, but I just yesterday received an iSwimband for the kids and it didn’t work with my iPad because the hardware is 1 generation too old… Then I saw this post. So I put the iSwimband into pairing mode, then connected it to my phone. Then I ran you app and I see nothing in the listbox when I tap discover. So now I am taking a step back and actually looking at the code. So what are _bleAddress, _writeGuid, and _readGuid.

Are these device specific? The iSwimband is a Broadcom device of some sort: http://fccid.net/pdf.php?id=2305812

Mine has what looks like a MAC address sticker on it… perhaps I need to use that somehow?

I tried changing to the default constructor, but got nothing on this line: var devices = await DeviceInformation.FindAllAsync()

Anyway… off to the pool party… I guess I’ll have to just pay attention to the kids… darn it! :stuck_out_tongue:

@ Valkyrie-MT - couple of things you need change…

You will need to change the address in the manifest…

And those 3 UUID’s are specific to my module.

The armband will have different ones…

Will be simple to sort out :wink:

@ Justin - this line



always return null. I'm connected to my Gadgeteer 8078383D with my WP8.1 Nokia but can't get past this point. Any ideas?

device.Id returns this
[quote]\\?\BTHLEDevice#{2ac94b65-c8f4-48a4-804a-c03bc6960b80}_00078078383d#7&10a2acde&0&0013#{6e3bb679-4372-40c8-9eaa-4509df260cd8}[/quote]

Also, what xaml did you use to bind the UXList to the Device.Name property?

Cheers,

J.

If i was a gambling man - which i’m not…

I would bet all the tea in China you dont have this in your Package.appxmanifest…

 <Capabilities>
    <m2:DeviceCapability Name="bluetooth.genericAttributeProfile">
      <m2:Device Id="any">
        <m2:Function Type="serviceId:2AC94B65-C8F4-48A4-804A-C03BC6960B80" />
      </m2:Device>
    </m2:DeviceCapability>
  </Capabilities>

Device.Id is correct.

        <ListBox x:Name="UxListBoxDevices" HorizontalAlignment="Left" Height="122" Margin="33,90,0,0" VerticalAlignment="Top" Width="210" FontSize="18" SelectionChanged="UxListBoxDevices_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
1 Like

Perfect 8). Same about not being a gambler thou…

Any chance of the binding xaml? I see Windows.Devices.Enumeration.DeviceInformation in my list.

J. :slight_smile:

Check your email geezer… 8)

@ Justin - Perfect

@ Jason - picture of results else it doesnt exist… ::slight_smile:

Here ya go

3 Likes

Nice work Jason :slight_smile:

And what i find classic is you have used the excuse of the kids to cough play with your toys!!

1 Like

For someone who hasn’t known me that long, you sure are insightful :whistle:

What do you mean with “kids toys” :whistle:

3 Likes

Riding on the success of their Lego traffic lights, my eldest daughter (9) decided that what her next Lego creation (a 3-storey house) needs in LED lights in each room that she can control from my WP8.

I have a feeling that Lego might feature quite highly in my BLE devilment for some time to come ::slight_smile:

3 Likes

Wicked, full R & D approval!!

1 Like

Just need to secure some more funding for extra resources, apparently. Like we don’t own enough Lego already. :wall:

Hahahaha Pandora’s box has been opened :smiley:

@ Jason, Hahahahahahahahahahaha Time to buy a bigger house.

Sounds like “playing with your kids legos” led to “loosing your phone to your kids” :clap: