forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
174 lines (146 loc) · 7.3 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using System;
using System.Diagnostics;
using System.Threading;
using nanoFramework.Device.Bluetooth;
using nanoFramework.Device.Bluetooth.GenericAttributeProfile;
using nanoFramework.Device.Bluetooth.Services;
using nanoFramework.Runtime.Native;
/// <summary>
/// Bluetooth Sample 3 is a application which shows the use of:
///
/// Standard Services:
/// - Device Information
/// - Battery Level
/// - Current Time
/// - Environmental Sensor
///
/// Suitable Phone apps for testing: "nRF Connect" or "LightBlue"
/// </summary>
namespace BluetoothLESample3
{
public class Program
{
public static void Main()
{
Debug.WriteLine("Hello from Bluetooth Sample 3");
// Define used custom Uuid
Guid serviceUuid = new("A7EEDF2C-DA87-4CB5-A9C5-5151C78B0057");
Guid readStaticCharUuid = new("A7EEDF2C-DA89-4CB5-A9C5-5151C78B0057");
// BluetoothLEServer is a singleton object so gets its instance. The Object is created when you first access it
// and can be disposed to free up memory.
BluetoothLEServer server = BluetoothLEServer.Instance;
// Give device a name
server.DeviceName = "Sample3";
//The GattServiceProvider is used to create and advertise the primary service definition.
//An extra device information service will be automatically created.
GattServiceProviderResult result = GattServiceProvider.Create(serviceUuid);
if (result.Error != BluetoothError.Success)
{
return;
}
GattServiceProvider serviceProvider = result.ServiceProvider;
// Get created Primary service from provider
GattLocalService service = serviceProvider.Service;
#region Static read characteristic
// Now we add an characteristic to service
// If the read value is not going to change then you can just use a Static value
DataWriter sw = new();
sw.WriteString("This is Bluetooth sample 3");
GattLocalCharacteristicResult characteristicResult = service.CreateCharacteristic(readStaticCharUuid,
new GattLocalCharacteristicParameters()
{
CharacteristicProperties = GattCharacteristicProperties.Read,
UserDescription = "My Static Characteristic",
StaticValue = sw.DetachBuffer()
});
;
if (characteristicResult.Error != BluetoothError.Success)
{
// An error occurred.
return;
}
#endregion
// Add standard Bluetooth Sig services to the provider. These are an example of standard services
// that can be reused/updated for other applications. Based on standards but simplified.
// === Device Information Service ===
// https://www.bluetooth.com/specifications/specs/device-information-service-1-1/
// The Device Information Service is created automatically when you create the initial primary service.
// The default version just has a Manufacturer name of "nanoFramework and model or "Esp32"
// You can add your own service which will replace the default one.
// To make it easy we have included some standard services classes to this sample
DeviceInformationServiceService DifService = new(
"MyGreatCompany",
"Model-1",
null, // no serial number
"v1.0",
SystemInfo.Version.ToString(),
"");
// === Battery Service ===
// https://www.bluetooth.com/specifications/specs/battery-service-1-0/
// Battery service exposes the current battery level percentage
BatteryService BatService = new();
// Update the Battery service the current battery level regularly. In this case 94%
BatService.BatteryLevel = 94;
// === Current Time Service ===
// https://www.bluetooth.com/specifications/specs/current-time-service-1-1/
// The Current Time Service exposes the device current date/time and also
// optionally allows the date time to be updated. You can call the Notify method to inform
// any connected devices of changed in date/time. Any subscribed clients will be automatically
// be notified every 60 seconds.
CurrentTimeService CtService = new(true);
// === Environmental Sensor Service ===
// https://www.bluetooth.com/specifications/specs/environmental-sensing-service-1-0/
// This service exposes measurement data from an environmental sensors.
EnvironmentalSensorService EnvService = new();
// Add sensors to service, return index so sensor can be updated later.
int iTempOut = EnvService.AddSensor(EnvironmentalSensorService.SensorType.Temperature, "Outside Temp");
int iTempOutMax = EnvService.AddSensor(EnvironmentalSensorService.SensorType.Temperature, "Max Outside Temp", EnvironmentalSensorService.Sampling.Maximum);
int iTempOutMin = EnvService.AddSensor(EnvironmentalSensorService.SensorType.Temperature, "Min Outside Temp", EnvironmentalSensorService.Sampling.Minimum);
int iHumidity = EnvService.AddSensor(EnvironmentalSensorService.SensorType.Humidity, "OUtside Humidty");
// Update sensor values, these would need to be updated every time sensors are read.
EnvService.UpdateValue(iTempOut, 23.4F);
EnvService.UpdateValue(iTempOutMax, 28.1F);
EnvService.UpdateValue(iTempOutMin, 7.5F);
EnvService.UpdateValue(iHumidity, 63.3F);
#region Start Advertising
// Once all the Characteristics/Services have been created you need to advertise so
// other devices can see it. Here we also say the device can be connected too and other
// devices can see it with a specific device name.
serviceProvider.StartAdvertising(new GattServiceProviderAdvertisingParameters()
{
IsConnectable = true,
IsDiscoverable = true
});
#endregion
Thread.Sleep(60000);
// Update values after 1 min. to simulate real sensors
while (true)
{
float t1 = 23.4F;
float t3 = 7.5F;
// Move temperatures up
while (t1 < 120)
{
t1 += 1.3F;
t3 += 2.1F;
EnvService.UpdateValue(iTempOut, t1);
EnvService.UpdateValue(iTempOutMin, t3);
Thread.Sleep(5000);
}
// Move temperatures down
while (t1 > -50F)
{
t1 -= 1.3F;
t3 -= 2.1F;
EnvService.UpdateValue(iTempOut, t1);
EnvService.UpdateValue(iTempOutMin, t3);
Thread.Sleep(5000);
}
}
}
}
}