Skip to content

Commit 0078eb5

Browse files
committed
rdy 2 use
1 parent 8947f70 commit 0078eb5

1 file changed

Lines changed: 64 additions & 42 deletions

File tree

LighthouseV2PowerControl/Form1.cs

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.ComponentModel;
4-
using System.Data;
5-
using System.Drawing;
6-
using System.Globalization;
7-
using System.Linq;
8-
using System.Text;
9-
using System.Threading.Tasks;
3+
using System.Text.RegularExpressions;
104
using System.Windows.Forms;
11-
using System.Xml;
125
using Windows.Devices.Bluetooth;
13-
using Windows.Devices.Bluetooth.Advertisement;
146
using Windows.Devices.Bluetooth.GenericAttributeProfile;
157
using Windows.Devices.Enumeration;
168
using Windows.Storage.Streams;
@@ -23,59 +15,89 @@ public partial class Form1 : Form
2315
private Guid characteristic = Guid.Parse("00001525-1212-efde-1523-785feabcd124");
2416
private byte activateByte = 0x01;
2517
private byte deactivateByte = 0x00;
26-
private DeviceInformationCollection GatDevices;
18+
19+
private Regex regex = new Regex("^LHB-.{8}");
20+
21+
private static List<GattCharacteristic> listGattCharacteristics = new List<GattCharacteristic>();
2722

2823
public Form1()
2924
{
3025
InitializeComponent();
3126
lbStatus.Text = "";
27+
btnStart.Click += (obj,e) => SendOnLighthouse(activateByte);
28+
btnStop.Click += (obj,e) => SendOnLighthouse(deactivateByte);
29+
btnStop.Enabled = btnStart.Enabled = false;
3230
}
3331

3432
private void Form1_Load(object sender, EventArgs e)
3533
{
36-
SendByte(activateByte);
34+
GetGattCharacteristics();
3735
}
3836

39-
private async void SendByte(byte byte4send)
37+
private async void GetGattCharacteristics()
4038
{
41-
GatDevices = await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(service));
42-
BluetoothLEDevice bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(GatDevices[1].Id);
43-
GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync();
44-
45-
if (result.Status == GattCommunicationStatus.Success)
39+
DeviceInformationCollection GatDevices = await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(service));
40+
for (int id = 0; id < GatDevices.Count; id++)
4641
{
47-
IReadOnlyList<GattDeviceService> serviceList = result.Services;
48-
for (int i = 0; i < serviceList.Count; ++i)
49-
{
50-
if (serviceList[i].Uuid != service) continue;
42+
if(!regex.IsMatch(GatDevices[id].Name)) continue;
5143

52-
GattCharacteristicsResult gattRes = await serviceList[i].GetCharacteristicsAsync();
53-
if (gattRes.Status == GattCommunicationStatus.Success)
44+
BluetoothLEDevice bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(GatDevices[id].Id);
45+
GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync();
46+
47+
if (result.Status == GattCommunicationStatus.Success)
48+
{
49+
IReadOnlyList<GattDeviceService> serviceList = result.Services;
50+
for (int i = 0; i < serviceList.Count; ++i)
5451
{
55-
IReadOnlyList<GattCharacteristic> characteristics = gattRes.Characteristics;
56-
DataWriter writer = new DataWriter();
57-
writer.WriteByte(deactivateByte);
58-
var openStatus = await serviceList[i].OpenAsync(GattSharingMode.SharedReadAndWrite);
59-
lbStatus.Items.Add(openStatus);
60-
for (int j = 0; j < characteristics.Count; ++j)
52+
if (serviceList[i].Uuid != service) continue;
53+
GattCharacteristicsResult gattRes = await serviceList[i].GetCharacteristicsForUuidAsync(characteristic);
54+
if (gattRes.Status == GattCommunicationStatus.Success)
6155
{
62-
if(characteristics[j].Uuid != characteristic) continue;
63-
lbStatus.Items.Add(characteristics[j].CharacteristicProperties);
64-
var resWrite = await characteristics[j].WriteValueWithResultAsync(writer.DetachBuffer());
65-
lbStatus.Items.Add(resWrite.Status);
66-
//GattCommunicationStatus resWrite = await characteristics[j].WriteValueAsync(writer.DetachBuffer());
67-
//if (resWrite == GattCommunicationStatus.Success)
68-
//{
69-
// lbStatus.Items.Add(resWrite);
70-
//}
71-
//else
72-
//{
73-
// lbStatus.Items.Add(resWrite);
74-
//}
56+
var openStatus = await serviceList[i].OpenAsync(GattSharingMode.SharedReadAndWrite);
57+
IReadOnlyList<GattCharacteristic> characteristics = gattRes.Characteristics;
58+
for (int j = 0; j < characteristics.Count; ++j)
59+
{
60+
if (characteristics[j].Uuid != characteristic) continue;
61+
listGattCharacteristics.Add(characteristics[j]);
62+
}
7563
}
7664
}
7765
}
7866
}
67+
68+
Log($"lighthouses found: {listGattCharacteristics.Count}");
69+
if (listGattCharacteristics.Count > 0)
70+
{
71+
btnStop.Enabled = btnStart.Enabled = true;
72+
}
73+
}
74+
75+
private async void SendOnLighthouse(byte byte4send)
76+
{
77+
for (int i = 0; i < listGattCharacteristics.Count; ++i)
78+
{
79+
DataWriter writer = new DataWriter();
80+
writer.WriteByte(byte4send);
81+
GattCommunicationStatus resWrite = await listGattCharacteristics[i].WriteValueAsync(writer.DetachBuffer());
82+
if (resWrite == GattCommunicationStatus.Success)
83+
{
84+
Log($"Success");
85+
}
86+
else
87+
{
88+
LogError($"lighthouse: {resWrite}");
89+
}
90+
}
91+
}
92+
93+
private void Log(object msg)
94+
{
95+
lbStatus.Items.Add(msg);
96+
}
97+
98+
private void LogError(object msg)
99+
{
100+
lbStatus.Items.Add(msg);
79101
}
80102
}
81103
}

0 commit comments

Comments
 (0)