Skip to content

Commit 0d6bdc2

Browse files
committed
Add chatbox variable for device name
Closes #1
1 parent ed9512d commit 0d6bdc2

File tree

3 files changed

+43
-17
lines changed

3 files changed

+43
-17
lines changed

BluetoothHeartrateModule/BluetoothHeartrateModule.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ protected override void CreateAttributes()
4040
CreateSetting(BluetoothHeartrateSetting.WebsocketServerEnabled, @"Websocket Server Enabled", @"Broadcast the heartrate data over a local Websocket server", false);
4141
CreateSetting(BluetoothHeartrateSetting.WebsocketServerHost, @"Websocket Server Hostname", @"Hostname (IP address) for the Websocket server", "127.0.0.1", () => GetSetting<bool>(BluetoothHeartrateSetting.WebsocketServerEnabled));
4242
CreateSetting(BluetoothHeartrateSetting.WebsocketServerPort, @"Websocket Server Port", @"Port for the Websocket server", 36210, () => GetSetting<bool>(BluetoothHeartrateSetting.WebsocketServerEnabled));
43+
44+
CreateVariable(BluetoothHeartratevariable.DeviceName, @"Device Name", "device");
4345
}
4446

4547
protected override async void OnModuleStart()
@@ -72,6 +74,17 @@ internal int GetWebocketPortSetting()
7274
{
7375
return GetSetting<int>(BluetoothHeartrateSetting.WebsocketServerPort);
7476
}
77+
78+
internal void StopWatcher()
79+
{
80+
watcher?.Stop();
81+
}
82+
83+
internal void SetDeviceName(string deviceName)
84+
{
85+
SetVariableValue(BluetoothHeartratevariable.DeviceName, deviceName);
86+
}
87+
7588
private async void SendWebcoketHeartrate(int heartrate)
7689
{
7790
await wsServer.SendIntMessage(heartrate);
@@ -86,17 +99,17 @@ private void ResetWatcher()
8699
}
87100
}
88101

89-
internal void StopWatcher()
90-
{
91-
watcher?.Stop();
92-
}
93-
94102
internal enum BluetoothHeartrateSetting
95103
{
96104
DeviceMac,
97105
WebsocketServerEnabled,
98106
WebsocketServerHost,
99107
WebsocketServerPort
100108
}
109+
110+
internal enum BluetoothHeartratevariable
111+
{
112+
DeviceName
113+
}
101114
}
102115
}

BluetoothHeartrateModule/BluetoothHeartrateModule.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<TargetFramework>net6.0-windows10.0.22621.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7-
<AssemblyVersion>1.2.0.0</AssemblyVersion>
8-
<FileVersion>1.2.0.0</FileVersion>
7+
<AssemblyVersion>1.3.0</AssemblyVersion>
8+
<FileVersion>1.3.0</FileVersion>
99
<Authors>DJDavid98</Authors>
1010
<Product>Bluetooth Heartrate</Product>
1111
<ApplicationIcon>logo\logo.ico</ApplicationIcon>
@@ -15,4 +15,8 @@
1515
<PackageReference Include="VolcanicArts.VRCOSC.SDK" Version="2023.601.0" />
1616
</ItemGroup>
1717

18+
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
19+
<Exec Command="copy /Y &quot;$(TargetDir)$(TargetName).dll&quot; &quot;%25appdata%25\VRCOSC\assemblies\$(TargetName).dll&quot;" />
20+
</Target>
21+
1822
</Project>

BluetoothHeartrateModule/BluetoothHeartrateProvider.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace BluetoothHeartrateModule
88
{
99
public class BluetoothHeartrateProvider : HeartrateProvider
1010
{
11-
private Dictionary<string, string?> potentialDevices = new();
11+
private Dictionary<string, string?> deviceNames = new();
1212
private GattCharacteristic? heartRateCharacteristic;
1313
private HashSet<string> missingCharacteristicDevices = new();
1414
private bool processingData = false;
@@ -61,7 +61,7 @@ private void Reset()
6161
module.watcher.Received -= Watcher_Received;
6262
module.watcher.Stopped -= Watcher_Stopped;
6363
}
64-
potentialDevices.Clear();
64+
deviceNames.Clear();
6565
missingCharacteristicDevices.Clear();
6666
ResetDevice();
6767
processingData = false;
@@ -87,19 +87,26 @@ private async void Watcher_Received(BluetoothLEAdvertisementWatcher sender, Blue
8787

8888
var advertisementMac = Converter.FormatAsMac(args.BluetoothAddress);
8989
var deviceMacSetting = module.GetDeviceMacSetting();
90-
if (deviceMacSetting == string.Empty)
90+
var isConfiguredDevice = advertisementMac == deviceMacSetting;
91+
if (deviceMacSetting == string.Empty || isConfiguredDevice)
9192
{
92-
var potentialDevicesValue = potentialDevices.GetValueOrDefault(advertisementMac, null);
93-
if (potentialDevicesValue == null)
93+
var deviceNamesValue = deviceNames.GetValueOrDefault(advertisementMac, null);
94+
if (deviceNamesValue == null)
9495
{
9596
var advertisementDeviceName = await DeviceNameResolver.GetDeviceNameAsync(args.Advertisement, args.BluetoothAddress);
96-
potentialDevices[advertisementMac] = advertisementDeviceName;
97-
Log($"Discovered device: {advertisementDeviceName} (MAC: {advertisementMac})");
97+
deviceNames[advertisementMac] = advertisementDeviceName;
98+
if (!isConfiguredDevice)
99+
{
100+
Log($"Discovered device: {advertisementDeviceName} (MAC: {advertisementMac})");
101+
}
102+
}
103+
if (!isConfiguredDevice)
104+
{
105+
return;
98106
}
99-
return;
100107
}
101108

102-
if (advertisementMac != deviceMacSetting)
109+
if (!isConfiguredDevice)
103110
{
104111
// Not the droid we're looking for
105112
return;
@@ -117,7 +124,9 @@ private async void Watcher_Received(BluetoothLEAdvertisementWatcher sender, Blue
117124
if (currentDevice == null)
118125
{
119126
currentDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
120-
Log($"Found device for MAC {advertisementMac}");
127+
var currentDeviceName = deviceNames[advertisementMac] ?? "Unknown";
128+
Log($"Found device named {currentDeviceName} for MAC {advertisementMac}");
129+
module.SetDeviceName(currentDeviceName);
121130
}
122131

123132
var missungUnknown = !missingCharacteristicDevices.Contains(deviceMacSetting);

0 commit comments

Comments
 (0)