This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

cc2650 - reading data from movement sensor in C# , for windows phone 8.1

Other Parts Discussed in Thread: CC2650

Hello,

I am working on my bachelor thesis project that contains windows phone 8.1 application (C#), which is supposed to read data form sensor tag version 2 (cc2650).

I tried to read data from movement / accelerometer sensor, but it is not working very well... as model sample i used some project awailable from e2e.ti ( https://e2e.ti.com/support/wireless_connectivity/bluetooth_low_energy/f/538/t/475313  - but it self it is not working ,i can't write data to screen, app crashes ) and on sensor tag wiki ( https://github.com/clovett/SensorTag-for-Windows quite large, not so obvious for me).

In my code are, two version of reading, each from different sample project.

I am receiving some data, but i don't know how to manage them, or if they are correct (the values are really small, and they are behave really strange...) I am not sure if access is good or needs some more configuration.

I can't find any others samples of code in C# , or how to use informations about using and manage GATTserver, that are on sensor tag wiki to my code or how to get and use some library (if it is necessary).

Thank you for any help.
Sarka Mikolajkova

Here is my code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Popups;
using Windows.UI.Xaml.Navigation;
using Windows.Devices.Bluetooth;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using System.Text;
using System.Diagnostics;
using System.Threading;
using Windows.Storage.Streams;

namespace BLE
{

public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();

this.NavigationCacheMode = NavigationCacheMode.Required;


}

protected async override void OnNavigatedTo(NavigationEventArgs e)
{

////Find the devices that expose the service

var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(new Guid("F000AA80-0451-4000-B000-000000000000")));

////Connect to the service
var accService = await GattDeviceService.FromIdAsync(devices[0].Id);

//Get the accelerometer data characteristic
var accData = accService.GetCharacteristics(new Guid("F000AA81-0451-4000-B000-000000000000"))[0];
//Subcribe value changed

//accData.ValueChanged += AccData_ValueChanged;
accData.ValueChanged += test;

//Set configuration to notify
await accData.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);

//Get the accelerometer configuration characteristic
var accConfig = accService.GetCharacteristics(new Guid("F000AA82-0451-4000-B000-000000000000"))[0];

GattReadResult Resultat = await accConfig.ReadValueAsync();
var Output = Resultat.Value.ToArray();

Debug.WriteLine("Acc: " + Output.Count());
Debug.WriteLine("Registre 0:" + Output[0].ToString());
Debug.WriteLine("Registre 1:" + Output[1].ToString());

Output[0] = 0x7F;

await accConfig.WriteValueAsync(Output.AsBuffer());

}
GattCharacteristic NevimJaksender;
DispatcherTimer _timer;
double AccelX;

private void StartTimer()
{
if (_timer == null)
{
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(50);
_timer.Tick += OnTimerTick;
_timer.Start();
}
}

async void OnTimerTick(object sender, object e)
{

// part of first metod

var values = (await NevimJaksender.ReadValueAsync()).Value.ToArray();

float x = values[0];
float y = values[1];
float z = values[2];

textBlock.Text = string.Format("X: {0}, Y: {1}, Z: {2}", x, y, z);


Debug.WriteLine("X: {0}, Y: {1}, Z: {2}", x, y, z);


double xG= (double)(x * 1.0) / (32768 / 2);
double yG = (double)(y * 1.0) / (32768 / 2);
double zG = (double)(z * 1.0) / (32768 / 2);

textBlock1.Text = string.Format("G" + " X: {0}", xG);
textBlock2.Text = string.Format("G" + " Y: {0}", yG);
textBlock3.Text = string.Format("G" + " Z: {0}", zG);
Debug.WriteLine("Xg: {0}, Yg: {1}, Zg: {2}", xG, yG, zG);

//writening of the second metod
textBlock4.Text = AccelX.ToString();

}

protected short ReadBigEndian16bit(DataReader reader)
{
byte lo = reader.ReadByte();
byte hi = reader.ReadByte();
return (short)(((short)hi << 8) + (short)lo);
}

private void test(GattCharacteristic sender, GattValueChangedEventArgs args)
{
//second metod
uint dataLength = args.CharacteristicValue.Length;

DataReader reader = DataReader.FromBuffer(args.CharacteristicValue);
if (dataLength == 18)
{

short gx = ReadBigEndian16bit(reader);
short gy = ReadBigEndian16bit(reader);
short gz = ReadBigEndian16bit(reader);
short ax = ReadBigEndian16bit(reader);
short ay = ReadBigEndian16bit(reader);
short az = ReadBigEndian16bit(reader);
short mx = ReadBigEndian16bit(reader);
short my = ReadBigEndian16bit(reader);
short mz = ReadBigEndian16bit(reader);
//AccelX = ((double)ax / 32768);
AccelX = ((double)ax / 32768);

}
//end of second metod


//first metod
NevimJaksender = sender;
var nowait = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() =>
{

StartTimer();

}));
//end of second metod

}
}
}

Package.appxmanifest:

<?xml version="1.0" encoding="utf-8"?>

<Package xmlns="schemas.microsoft.com/.../manifest" xmlns:m2="schemas.microsoft.com/.../manifest" xmlns:m3="schemas.microsoft.com/.../manifest" xmlns:mp="schemas.microsoft.com/.../manifest">

<Identity Name="b9c95f8b-f25b-4875-9862-972d636d517d"
Publisher="CN=Šárka"
Version="1.0.0.0" />

<mp:PhoneIdentity PhoneProductId="b9c95f8b-f25b-4875-9862-972d636d517d" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

<Properties>
<DisplayName>BLE</DisplayName>
<PublisherDisplayName>Šárka</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>

<Prerequisites>
<OSMinVersion>6.3.1</OSMinVersion>
<OSMaxVersionTested>6.3.1</OSMaxVersionTested>
</Prerequisites>

<Resources>
<Resource Language="x-generate"/>
</Resources>

<Applications>
<Application Id="App"
Executable="$targetnametoken$.exe"
EntryPoint="BLE.App">
<m3:VisualElements
DisplayName="BLE"
Square150x150Logo="Assets\Logo.png"
Square44x44Logo="Assets\SmallLogo.png"
Description="BLE"
ForegroundText="light"
BackgroundColor="transparent">
<m3:DefaultTile Wide310x150Logo="Assets\WideLogo.png" Square71x71Logo="Assets\Square71x71Logo.png"/>
<m3:SplashScreen Image="Assets\SplashScreen.png"/>
</m3:VisualElements>
</Application>
</Applications>
<Capabilities>
<Capability Name="internetClientServer" />
<m2:DeviceCapability Name="bluetooth.genericAttributeProfile">
<m2:Device Id="any">
<m2:Function Type="serviceId:F000AA80-0451-4000-B000-000000000000"/>
<m2:Function Type="serviceId:F000AA40-0451-4000-B000-000000000000"/>
<m2:Function Type="serviceId:F000AA00-0451-4000-B000-000000000000"/>
<m2:Function Type="serviceId:F000CCC0-0451-4000-B000-000000000000"/>
<m2:Function Type="serviceId:F000AA70-0451-4000-B000-000000000000"/>
<m2:Function Type="serviceId:F000AC00-0451-4000-B000-000000000000"/>
<m2:Function Type="serviceId:F000AA64-0451-4000-B000-000000000000"/>
<m2:Function Type="serviceId:F000AA20-0451-4000-B000-000000000000"/>
<m2:Function Type="serviceId:F000FFC0-0451-4000-B000-000000000000"/>
<m2:Function Type="serviceId:0000ffe0-0000-1000-8000-00805f9b34fb"/>
<m2:Function Type="serviceId:00001800-0000-1000-8000-00805f9b34fb"/>
<m2:Function Type="serviceId:00001801-0000-1000-8000-00805f9b34fb"/>
<m2:Function Type="serviceId:0000180A-0000-1000-8000-00805f9b34fb"/>
</m2:Device>
</m2:DeviceCapability>
</Capabilities>
</Package>

  • Hello Sarka,

    Most of the discussions on this forum are about the device firmware itself. For better exposure to your Win 8 programming questions, I would suggest posting on a website that is dedicated to smartphone programming, such as stackoverflow.net.

    Best wishes