lunch break
This commit is contained in:
shinichi
2020-09-09 12:27:37 +02:00
parent 261048c2df
commit 3e3a0cf4c4
3 changed files with 89 additions and 10 deletions

View File

@@ -1,11 +1,80 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using Avans.TI.BLE;
using System.Threading;
namespace Hardware namespace Hardware
{ {
class BLEReciever class BLEReciever
{ {
IDataConverter dataConverter; IDataConverter dataConverter;
public BLEReciever(IDataConverter dataConverter)
{
this.dataConverter = dataConverter;
}
public async void Connect()
{
int errorCode = 0;
BLE bleBike = new BLE();
BLE bleHeart = new BLE();
Thread.Sleep(1000); // We need some time to list available devices
// List available devices
List<String> bleBikeList = bleBike.ListDevices();
Console.WriteLine("Devices found: ");
foreach (var name in bleBikeList)
{
Console.WriteLine(name);
if (name.Contains("Avans Bike"))
{
Connect(name);
Console.WriteLine("connecting to {0}", name);
break;
}
}
}
public async void Connect(string deviceName)
{
int errorCode = 0;
BLE bleBike = new BLE();
BLE bleHeart = new BLE();
errorCode = errorCode = await bleBike.OpenDevice(deviceName);
// __TODO__ Error check
var services = bleBike.GetServices;
foreach (var service in services)
{
Console.WriteLine($"Service: {service}");
}
// Set service
errorCode = await bleBike.SetService("6e40fec1-b5a3-f393-e0a9-e50e24dcca9e");
// __TODO__ error check
// Subscribe
bleBike.SubscriptionValueChanged += BleBike_SubscriptionValueChanged;
errorCode = await bleBike.SubscribeToCharacteristic("6e40fec2-b5a3-f393-e0a9-e50e24dcca9e");
// Heart rate
errorCode = await bleHeart.OpenDevice(deviceName);
await bleHeart.SetService("HeartRate");
bleHeart.SubscriptionValueChanged += BleBike_SubscriptionValueChanged;
await bleHeart.SubscribeToCharacteristic("HeartRateMeasurement");
}
private void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
{
Console.WriteLine("Received from {0}: {1}, {2}", e.ServiceName,
BitConverter.ToString(e.Data).Replace("-", " "),
Encoding.UTF8.GetString(e.Data));
//send to dataconverter
}
} }
} }

View File

@@ -11,15 +11,14 @@ namespace FietsDemo
{ {
internal class Program internal class Program
{ {
static void Main(string[] args) //static void Main(string[] args)
{ //{
foo foo = new foo(); // foo foo = new foo();
// BLEReceiver bLEReceiver = new BLEReceiver(foo); // BLEReceiver bLEReceiver = new BLEReceiver(foo);
// bLEReceiver.ConnectToBLE(); // bLEReceiver.ConnectToBLE();
Console.Read(); // Console.Read();
} //}
interface IFoo interface IFoo
{ {

View File

@@ -1,10 +1,21 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using Hardware;
namespace ProftaakRH namespace ProftaakRH
{ {
class Main class Program
{ {
static void Main(string[] args)
{
IDataConverter dataConverter = new DataConverter();
BLEReciever bLEReceiver = new BLEReciever(dataConverter);
bLEReceiver.Connect("sdkgjnzsoifgnzaiof");
//Console.ReadLine();
}
} }
} }