LEDnetWF
[LEDnetWF0200A32197AC]
.Jiangmen Zhengjiguangzhao Technology Co., Ltd zengge_lednetwf- - Zengge LEDnetWF
- YBCRG-RGBWW
- Magic Hue
- code Py from github <----
- Bluetooth full colors selfie ring light
Zengge LEDnet WF Bluetooth LE WS2812b 8x32 16x16 8x8 WS2812 Led Pixel Module
project to add support for these lights
Zengge LEDnet WF Bluetooth LE reverse engineering
Might also be known as:
Zengge LEDnetWF
YBCRG-RGBWW
Magic Hue
Bluetooth full colors selfie ring light
For this code you will need window 11 and visual studio 2022
#include "pch.h"
#pragma comment(lib, "oleaut32")
#pragma comment(lib, "runtimeobject")
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <winrt/Windows.Devices.Bluetooth.h>
#include <winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h>
#include <winrt/Windows.Devices.Enumeration.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Storage.Streams.h>
using namespace winrt;
using namespace Windows::Foundation;
using namespace winrt::Windows::Foundation;
using namespace Windows::Storage::Streams;
using namespace Windows::Devices::Bluetooth;
using namespace Windows::Foundation::Collections;
using namespace Windows::Devices::Bluetooth::Advertisement;
using namespace Windows::Devices::Bluetooth::GenericAttributeProfile;
using namespace Windows::Devices::Enumeration;
using namespace Windows::Foundation;
using namespace Windows::Storage::Streams;
#include <winrt/Windows.Devices.Bluetooth.Rfcomm.h>
#include <winrt/Windows.Networking.Sockets.h>
using namespace winrt;
using namespace Windows::Devices::Bluetooth::Rfcomm;
using namespace Windows::Networking::Sockets;
std::wstring guidToString(GUID uuid)
{
std::wstring guid;
WCHAR* wszUuid = NULL;
if (::UuidToString(&uuid, (RPC_WSTR*) &wszUuid) == RPC_S_OK)
{
guid = wszUuid;
::RpcStringFree((RPC_WSTR*) &wszUuid);
}
return guid;
}
std::wstring advertisementTypeToString(BluetoothLEAdvertisementType advertisementType)
{
std::wstring ret;
switch (advertisementType)
{
case BluetoothLEAdvertisementType::ConnectableUndirected:
ret = L"ConnectableUndirected";
break;
case BluetoothLEAdvertisementType::ConnectableDirected:
ret = L"ConnectableDirected";
break;
case BluetoothLEAdvertisementType::ScannableUndirected:
ret = L"ScannableUndirected";
break;
case BluetoothLEAdvertisementType::NonConnectableUndirected:
ret = L"NonConnectableUndirected";
break;
case BluetoothLEAdvertisementType::ScanResponse:
ret = L"ScanResponse";
break;
default:
break;
}
return ret;
}
std::wstring bluetoothAddressTypeToString(BluetoothAddressType bluetoothAddressType)
{
std::wstring ret;
switch (bluetoothAddressType)
{
case BluetoothAddressType::Public:
ret = L"Public";
break;
case BluetoothAddressType::Random:
ret = L"Random";
break;
case BluetoothAddressType::Unspecified:
ret = L"Unspecified";
break;
default:
break;
}
return ret;
}
void string2hexString(char* input, char* output)
{
int loop;
int i;
i=0;
loop=0;
while(input[loop] != '\0')
{
sprintf((char*)(output+i),"%02X", input[loop]);
loop+=1;
i+=2;
}
//insert NULL at the end of the output string
output[i++] = '\0';
}
//IAsyncAction
//winrt::Windows::Foundation::IAsyncAction
winrt::Windows::Foundation::IAsyncAction OpenDevice(unsigned long long deviceAddress)
{
auto device = co_await BluetoothLEDevice::FromBluetoothAddressAsync(deviceAddress);
std::wcout << std::hex <<
"\tDevice Information: " << std::endl <<
"\tBluetoothAddress: [" << device.BluetoothAddress() << "]" << std::endl <<
"\tBluetoothAddressType: [" << bluetoothAddressTypeToString(device.BluetoothAddressType()) << "]" << std::endl <<
"\tConnectionStatus: [" << (device.ConnectionStatus() == BluetoothConnectionStatus::Connected ? "Connected" : "Disconnected") << "]" << std::endl <<
"\tDeviceId: [" << device.DeviceId().c_str() << "]" << std::endl <<
std::endl;
//device.DeviceId().c_str();
auto services = co_await device.GetGattServicesAsync();
for (GenericAttributeProfile::GattDeviceService const & s : services.Services())
{
std::wcout << std::hex <<
"\t\tService - Guid: [" << guidToString(s.Uuid()) << "]" << std::endl;
auto characteristics = co_await s.GetCharacteristicsAsync();
for (GenericAttributeProfile::GattCharacteristic const & c : characteristics.Characteristics())
{
std::wcout << std::hex <<
"\t\t\tCharacteristic - Guid: [" << guidToString(c.Uuid()) << "]" << std::endl;
if (c.CharacteristicProperties() == GattCharacteristicProperties::Read)
{
auto readResult = co_await c.ReadValueAsync();
if (readResult.Status() == GattCommunicationStatus::Success)
{
std::wcout << "\t\t\tCharacteristic Data - Size: [" << readResult.Value().Length() << "]" << std::endl;
DataReader reader = DataReader::FromBuffer(readResult.Value());
std::wcout << "\t\t\tCharacteristic Data - [" << reader.ReadString(readResult.Value().Length()).c_str() << "]" << std::endl;
}
}
if (c.CharacteristicProperties() == GattCharacteristicProperties::Notify)
{
c.ValueChanged([](GattCharacteristic const& charateristic, GattValueChangedEventArgs const& args)
{
std::wcout << std::hex <<
"\t\tNotified GattCharacteristic - Guid: [" << guidToString(charateristic.Uuid()) << "]" << std::endl;
DataReader reader = DataReader::FromBuffer(args.CharacteristicValue());
// Note this assumes value is string the characteristic type must be determined before reading
// This can display junk or maybe blowup
std::wcout << "\t\t\tCharacteristic Data - [" << reader.ReadString(args.CharacteristicValue().Length()).c_str() << "]" << std::endl;
});
}
}
}
auto uartService = services.Services().GetAt(3);
auto txService = (co_await uartService.GetCharacteristicsAsync()).Characteristics().GetAt(0);
auto rxService = (co_await uartService.GetCharacteristicsAsync()).Characteristics().GetAt(1);
auto writer = Windows::Storage::Streams::DataWriter();
//const char* data = "\x3B\x23\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x5E";
//writer.WriteString(data);
//{ 0x3B,0x24,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x5f };
writer.WriteByte(0x3B);
writer.WriteByte(0x24);
writer.WriteByte(0);
writer.WriteByte(0);
writer.WriteByte(0);
writer.WriteByte(0);
writer.WriteByte(0);
writer.WriteByte(0);
writer.WriteByte(0);
writer.WriteByte(0);
writer.WriteByte(0);
writer.WriteByte(0);
writer.WriteByte(0x5f);
auto status = co_await txService.WriteValueWithResultAsync(writer.DetachBuffer(), winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::GattWriteOption::WriteWithoutResponse);
if (status.Status() == GattCommunicationStatus::Success)
{
std::wcout << "Write OK";
}
else
{
std::wcout << "Write Failed";
}
device.Close();
}
void scanble()
{
init_apartment();
std::atomic<unsigned long long> deviceAddress = 0;
try
{
BluetoothLEAdvertisementWatcher watcher;
//watcher.ScanningMode(BluetoothLEScanningMode::Active);
watcher.ScanningMode(BluetoothLEScanningMode::Passive);
watcher.Received([&](BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
watcher.Stop();
std::string localName;
std::wcout <<
"AdvertisementReceived:" << std::endl <<
"\tLocalName: [" << eventArgs.Advertisement().LocalName().c_str() << "]" <<
"\tAdvertisementType: [" << advertisementTypeToString(eventArgs.AdvertisementType()) << "]" <<
"\tBluetoothAddress: [0x" << std::hex << eventArgs.BluetoothAddress() << "]" <<
"\tRawSignalStrengthInDBm: [" << std::dec << eventArgs.RawSignalStrengthInDBm() << "]" <<
std::endl;
if (eventArgs.AdvertisementType() == BluetoothLEAdvertisementType::ConnectableUndirected ||
eventArgs.AdvertisementType() == BluetoothLEAdvertisementType::ConnectableDirected) {
for (GUID const& g : eventArgs.Advertisement().ServiceUuids())
std::wcout << "ServiceUUID: [" << guidToString(g) << "]" << std::endl;
deviceAddress = eventArgs.BluetoothAddress();
}
});
std::cout << "Waiting for device: ";
watcher.Start();
int count = 0;
while ((count++ < 10) && deviceAddress == 0)
{
std::this_thread::sleep_for(std::chrono::seconds(3));
std::cout << '.';
}
std::cout << std::endl << "Finished waiting for device." << std::endl;
if (deviceAddress != 0)
{
std::cout << "Device found." << std::endl;
OpenDevice(deviceAddress).get();
}
else {
//watcher.Stop();
std::cout << "Device not found." << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
return ;
}
}
void senddata()
{
}
int main()
{
scanble();
}
/*
*
*
*
//
//0x3B,0x24,0,0,0,0,0,0,0,0,0,0,0x5f
//writer.WriteUInt16(val);
//00 5b 80 00 00 0d 0e 0b 3b 24 00 00 00 00 00 00 00 32 00 00 91
///## Power control
//Example bytes `ON`: `00 04 80 00 00 0d 0e 0b 3b 23 00 00 00 00 00 00 00 32 00 00 90`
//Example bytes `OFF`: `00 5b 80 00 00 0d 0e 0b 3b 24 00 00 00 00 00 00 00 32 00 00 91`
//looks like this:```€��34 {"code":0,"payload":"811D24610F313232FF640200305C"}
//const int test3[21] = { 0x00,0x5b,0x80,0x00,0x00,0x0d,0x0e,0x0b,0x3b,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x32,0x00,0x00,0x91 };
//for (int k = 0; k < 20; k++) writer.WriteByte(test3[k]);
//const int test2[13] = { 0x3B,0x24,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x5f };
//for (int k = 0; k < 13; k++) writer.WriteByte(test2[k]);
//const uint16_t test[13] = { 0x00,0x06,0x80,0x00,0x00,0x04,0x05,0x0b,0x38,0x01,0x01,0x64 };
//const uint16_t test[13] = { 0x00,0x06,0x80,0x00,0x00,0x04,0x05,0x0b,0x38,0x03,0x63,0x64 };
// for (int k = 0; k < 12; k++) writer.WriteByte(test[k]);
//char ascii_str[] = "{\"code\":0,\"payload\":\"811D24610F313232FF640200305C\"}";
// Output result
//0004 8000000d 0e0b3b 23 000000 0000 0000320000 90
//0010 8000000d 0e0b3b b1 000000 6464 0000000000 b4
// srcData = new byte[]{0,0,0x24,0,0,0,0,0,0,0,0,0,0,0}
// Static(1), // 静态
//Running(2), // 流水
//// Strobe(3), // 频闪
// Jump(4), // 跳变
// Breathe(5), // 呼吸
// Twinkly(6); // 闪烁
// //byte[] commandData= {0x3B,0x25,0,0,0,0,0,0,0,0,0,0,0x60};
//
/*
* @OnClick(R.id.btn_open)
public void onBtnOpenClick(View view){
// byte[] sendData={0x3B,0x23,0,0,0,0,0,0,0,0,0,0,0x5e};
byte[] sendData={0x71,0x23,0x0F, (byte) 0xa3};
et_input.setText(ByteUtil.bytesToHexString(sendData));
}
@OnClick(R.id.btn_close)
public void onBtnCloseClick(View view){
// byte[] sendData={0x3B,0x24,0,0,0,0,0,0,0,0,0,0,0x5f};
byte[] sendData={0x71,0x24,0x0F, (byte) 0xa4};
et_input.setText(ByteUtil.bytesToHexString(sendData));
} @OnClick(R.id.btn_buildInMode)
public void onBtnBuiltInModeClick(View view){
byte[] sendData=CMDMgr.getCommandDataForNewSymphonyBuiltInMode(100,100,50);
et_input.setText(ByteUtil.bytesToHexString(sendData));
}
@OnClick(R.id.btn_colorful1)
public void onBtnColorful1Click(View view){
int[] colors = new int[]{Color.parseColor("#00FF6A"), Color.parseColor("#00FFFF"), Color.parseColor("#A200FF"), Color.parseColor("#E5FF00"), Color.parseColor("#FF0015"), Color.parseColor("#004EFF"), Color.parseColor("#FF9D00")};
float[] positions = new float[]{0, 0.06f, 0.31f, 0.37f, 0.63f, 0.69f, 0.94f};
List<StaticColorfulColor> colorfulColors = new ArrayList<>();
for (int i = 0; i < colors.length; i++) {
StaticColorfulColor colorfulColor = new StaticColorfulColor(i, colors[i], positions[i]);
colorfulColors.add(colorfulColor);
}
StaticColorful colorful = new StaticColorful(UUID.randomUUID().toString(), StaticColorfulMode.valueFromCommandType(1), colorfulColors, false, 50, 1);
int ledBeadCount=((deviceInfo.getState()[9]&0xff)<<8) |(deviceInfo.getState()[10]&0xff);
List<Integer> ledBeadColors = colorful.toPointColor(ledBeadCount);
byte[] sendData=CMDMgr.getCommandForNewSymphonyColorful(ledBeadCount,ledBeadColors,colorful.getMode().commandType,colorful.getSpeed(),colorful.getDirection());
et_input.setText(ByteUtil.bytesToHexString(sendData));
}
@OnClick(R.id.btn_colorful2)
public void onBtnColorful2Click(View view){
int[] colors = new int[]{Color.parseColor("#FF002E"), Color.parseColor("#CE00E8"), Color.parseColor("#00FFFF"), Color.parseColor("#F39700"), Color.parseColor("#00FF6E"), Color.parseColor("#004EFF")};
float[] positions = new float[]{0, 0.3f, 0.5f, 0.68f, 0.83f, 0.94f};;
List<StaticColorfulColor> colorfulColors = new ArrayList<>();
for (int i = 0; i < colors.length; i++) {
StaticColorfulColor colorfulColor = new StaticColorfulColor(i, colors[i], positions[i]);
colorfulColors.add(colorfulColor);
}
//unsigned int sendData = { 0x3B,0x24,0,0,0,0,0,0,0,0,0,0,0x5f };
writer.WriteUInt16(0x3B);
writer.WriteUInt16(0x24);
writer.WriteUInt16(0);
writer.WriteUInt16(0);
writer.WriteUInt16(0);
writer.WriteUInt16(0);
writer.WriteUInt16(0);
writer.WriteUInt16(0);
writer.WriteUInt16(0);
writer.WriteUInt16(0);
writer.WriteUInt16(0);
writer.WriteUInt16(0);
writer.WriteUInt16(0x5f);
*/
int mainq()
{
init_apartment();
std::atomic<unsigned long long> deviceAddress = 0;
try
{
BluetoothLEAdvertisementWatcher watcher;
std::wcout << std::hex <<
"BluetoothLEAdvertisementWatcher:" << std::endl <<
"\tMaxOutOfRangeTimeout: [0x" << watcher.MaxOutOfRangeTimeout().count() << "]" << std::endl <<
"\tMaxSamplingInterval: [0x" << watcher.MaxSamplingInterval().count() << "]" << std::endl <<
"\tMinOutOfRangeTimeout: [0x" << watcher.MinOutOfRangeTimeout().count() << "]" << std::endl <<
"\tMinSamplingInterval: [0x" << watcher.MinSamplingInterval().count() << "]" << std::endl <<
std::endl;
watcher.ScanningMode(BluetoothLEScanningMode::Passive);
watcher.Received([&](BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
watcher.Stop();
std::wcout <<
"AdvertisementReceived:" << std::endl <<
"\tLocalName: [" << eventArgs.Advertisement().LocalName().c_str() << "]" <<
"\tAdvertisementType: [" << advertisementTypeToString(eventArgs.AdvertisementType()) << "]" <<
"\tBluetoothAddress: [0x" << std::hex << eventArgs.BluetoothAddress() << "]" <<
"\tRawSignalStrengthInDBm: [" << std::dec << eventArgs.RawSignalStrengthInDBm() << "]" <<
std::endl;
for (GUID const & g : eventArgs.Advertisement().ServiceUuids())
std::wcout << "ServiceUUID: [" << guidToString(g) << "]" << std::endl;
deviceAddress = eventArgs.BluetoothAddress();
});
std::cout << "Waiting for device: ";
watcher.Start();
int count = 0;
while ((count++ < 1) && deviceAddress == 0)
//while ((count++ < 10) && deviceAddress == 0)
{
std::this_thread::sleep_for(std::chrono::seconds(3));
std::cout << '.';
}
std::cout << std::endl << "Finished waiting for device." << std::endl;
if (deviceAddress != 0)
{
std::cout << "Device found." << std::endl;
OpenDevice(deviceAddress).get();
}
else
std::cout << "Device not found." << std::endl;
return 0;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
/*
LocalName: [LEDnetWF0200A32197AC] AdvertisementType: [ConnectableUndirected] BluetoothAddress: [0x865f02197ac] RawSignalStrengthInDBm: [-22]
.
Finished waiting for device.
Device found.
Device Information:
BluetoothAddress: [865f02197ac]
BluetoothAddressType: [Public]
ConnectionStatus: [Disconnected]
DeviceId: [BluetoothLE#BluetoothLE64:6e:69:8b:25:88-08:65:f0:21:97:ac]
Service - Guid: [00001800-0000-1000-8000-00805f9b34fb]
Characteristic - Guid: [00002a00-0000-1000-8000-00805f9b34fb]
Characteristic Data - Size: [14]
Characteristic Data - [LEDnetWF0200A32197AC]
Characteristic - Guid: [00002a01-0000-1000-8000-00805f9b34fb]
Characteristic Data - Size: [2]
Characteristic Data - []
Characteristic - Guid: [00002a04-0000-1000-8000-00805f9b34fb]
Characteristic Data - Size: [8]
Characteristic Data - []
Service - Guid: [00001801-0000-1000-8000-00805f9b34fb]
Characteristic - Guid: [00002a05-0000-1000-8000-00805f9b34fb]
Service - Guid: [0000fe00-0000-1000-8000-00805f9b34fb]
Characteristic - Guid: [0000ff22-0000-1000-8000-00805f9b34fb]
Characteristic - Guid: [0000ff11-0000-1000-8000-00805f9b34fb]
Service - Guid: [0000ffff-0000-1000-8000-00805f9b34fb]
Characteristic - Guid: [0000ff02-0000-1000-8000-00805f9b34fb]
Characteristic - Guid: [0000ff01-0000-1000-8000-00805f9b34fb]*/
/*
#include <winrt/Windows.Devices.Bluetooth.Rfcomm.h>
#include <winrt/Windows.Networking.Sockets.h>
using namespace winrt;
using namespace Windows::Devices::Bluetooth::Rfcomm;
using namespace Windows::Networking::Sockets;
int main()
{
init_apartment();
// Create an RfcommDeviceService object from a BluetoothDevice object
auto service = RfcommDeviceService::FromIdAsync(deviceId).get();
// Create a StreamSocket object and connect to the remote device
auto socket = StreamSocket();
socket.ConnectAsync(service.ConnectionHostName(), service.ConnectionServiceName()).get();
// Send data over the socket
const char* data = "\x3B\x24\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x5F";
auto writer = DataWriter(socket.OutputStream());
writer.WriteBytes(array_view<const uint8_t>(reinterpret_cast<const uint8_t*>(data), strlen(data)));
writer.StoreAsync().get();
return 0;
}
#include <winrt/Windows.Devices.Enumeration.h>
#include <winrt/Windows.Devices.Bluetooth.h>
using namespace winrt;
using namespace Windows::Devices::Enumeration;
using namespace Windows::Devices::Bluetooth;
int main()
{
init_apartment();
// Find all Bluetooth devices
auto devices = DeviceInformation::FindAllAsync(BluetoothDevice::GetDeviceSelector()).get();
// Select the device with the specified name
auto device = std::find_if(devices.begin(), devices.end(), [](const auto& d) { return d.Name() == L"LEDnetWF"; });
// Get the device ID
auto deviceId = device->Id();
return 0;
}
#include <winrt/Windows.Devices.Bluetooth.h>
#include <winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h>
#include <winrt/Windows.Devices.Enumeration.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Storage.Streams.h>
using namespace winrt;
using namespace Windows::Devices::Bluetooth;
using namespace Windows::Devices::Bluetooth::GenericAttributeProfile;
using namespace Windows::Devices::Enumeration;
using namespace Windows::Foundation;
using namespace Windows::Storage::Streams;
int main()
{
init_apartment();
// Find the device with the specified name
auto devices = DeviceInformation::FindAllAsync(BluetoothLEDevice::GetDeviceSelector()).get();
auto device = std::find_if(devices.begin(), devices.end(), [](const auto& d) { return d.Name() == L"LEDnetWF"; });
// Connect to the device
auto bleDevice = BluetoothLEDevice::FromIdAsync(device->Id()).get();
// Get the service and characteristic UUIDs
auto serviceUuid = GattDeviceService::GetDeviceSelectorFromUuid(GattServiceUuids::GenericAccess);
auto characteristicUuid = GattCharacteristicUuids::DeviceName;
// Get the service and characteristic
auto services = bleDevice.GetGattServicesForUuidAsync(serviceUuid).get();
auto characteristics = services.GetAt(0).GetCharacteristicsForUuidAsync(characteristicUuid).get();
auto characteristic = characteristics.GetAt(0);
// Subscribe to notifications
characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue::Notify).get();
// Listen for notifications
characteristic.ValueChanged([&](auto&& sender, auto&& args)
{
// Send data over the characteristic
const char* data = "\x3B\x23\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x5E";
auto writer = DataWriter();
writer.WriteString(data);
characteristic.WriteValueWithResultAsync(writer.DetachBuffer()).get();
});
return 0;
}
#include <winrt/Windows.Devices.Bluetooth.h>
#include <winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h>
#include <winrt/Windows.Devices.Enumeration.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Storage.Streams.h>
using namespace winrt;
using namespace Windows::Devices::Bluetooth;
using namespace Windows::Devices::Bluetooth::GenericAttributeProfile;
using namespace Windows::Devices::Enumeration;
using namespace Windows::Foundation;
using namespace Windows::Storage::Streams;
int main()
{
init_apartment();
// Find the device with the specified name
auto devices = DeviceInformation::FindAllAsync(BluetoothLEDevice::GetDeviceSelector()).get();
auto device = std::find_if(devices.begin(), devices.end(), [](const auto& d) { return d.Name() == L"LEDnetWF"; });
// Connect to the device
auto bleDevice = BluetoothLEDevice::FromIdAsync(device->Id()).get();
// Get the service and characteristic UUIDs
auto serviceUuid = GattDeviceService::GetDeviceSelectorFromUuid(GattServiceUuids::GenericAccess);
auto characteristicUuid = GattCharacteristicUuids::DeviceName;
// Get the service and characteristic
auto services = bleDevice.GetGattServicesForUuidAsync(serviceUuid).get();
auto characteristics = services.GetAt(0).GetCharacteristicsForUuidAsync(characteristicUuid).get();
auto characteristic = characteristics.GetAt(0);
// Subscribe to notifications
characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue::Notify).get();
// Listen for notifications
characteristic.ValueChanged([&](auto&& sender, auto&& args)
{
// Send data over the characteristic
const char* data = "\x3B\x23\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x5E";
auto writer = DataWriter();
writer.WriteString(data);
characteristic.WriteValueWithResultAsync(writer.DetachBuffer()).get();
// Output result
auto reader = DataReader(args.CharacteristicValue());
printf("Received data: %s\n", reader.ReadString(args.CharacteristicValue().Length()).c_str());
});
return 0;
}
#include <winrt/Windows.Devices.Bluetooth.h>
#include <winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h>
#include <winrt/Windows.Devices.Enumeration.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Storage.Streams.h>
using namespace winrt;
using namespace Windows::Devices::Bluetooth;
using namespace Windows::Devices::Bluetooth::GenericAttributeProfile;
using namespace Windows::Devices::Enumeration;
using namespace Windows::Foundation;
using namespace Windows::Storage::Streams;
struct BLEDevice
{
DeviceInformation deviceInfo;
BluetoothLEDevice bleDevice;
GattCharacteristic characteristic;
BLEDevice(DeviceInformation deviceInfo) : deviceInfo(deviceInfo)
{
bleDevice = BluetoothLEDevice::FromIdAsync(deviceInfo.Id()).get();
// Get the service and characteristic UUIDs
auto serviceUuid = GattDeviceService::GetDeviceSelectorFromUuid(GattServiceUuids::GenericAccess);
auto characteristicUuid = GattCharacteristicUuids::DeviceName;
// Get the service and characteristic
auto services = bleDevice.GetGattServicesForUuidAsync(serviceUuid).get();
auto characteristics = services.GetAt(0).GetCharacteristicsForUuidAsync(characteristicUuid).get();
characteristic = characteristics.GetAt(0);
// Subscribe to notifications
characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue::Notify).get();
}
void SendPayload()
{
// Send data over the characteristic
const char* data = "\x3B\x23\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x5E";
auto writer = DataWriter();
writer.WriteString(data);
characteristic.WriteValueWithResultAsync(writer.DetachBuffer()).get();
}
std::string ReceiveData()
{
// Listen for notifications
auto result = std::string();
characteristic.ValueChanged([&](auto&& sender, auto&& args)
{
auto reader = DataReader(args.CharacteristicValue());
result = reader.ReadString(args.CharacteristicValue().Length());
printf("Received data: %s\n", result.c_str());
});
return result;
}
};
int main()
{
init_apartment();
// Find the device with the specified name
auto devices = DeviceInformation::FindAllAsync(BluetoothLEDevice::GetDeviceSelector()).get();
auto device = std::find_if(devices.begin(), devices.end(), [](const auto& d) { return d.Name() == L"LEDnetWF"; });
// Connect to the device
BLEDevice ble(*device);
// Send payload and output result
ble.SendPayload();
auto result = ble.ReceiveData();
return 0;
}
#include <winrt/Windows.Devices.Bluetooth.h>
#include <winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h>
#include <winrt/Windows.Devices.Enumeration.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Storage.Streams.h>
using namespace winrt;
using namespace Windows::Devices::Bluetooth;
using namespace Windows::Devices::Bluetooth::GenericAttributeProfile;
using namespace Windows::Devices::Enumeration;
using namespace Windows::Foundation;
using namespace Windows::Storage::Streams;
struct BLEDevice
{
DeviceInformation deviceInfo;
BluetoothLEDevice bleDevice;
GattCharacteristic characteristic;
BLEDevice(DeviceInformation deviceInfo) : deviceInfo(deviceInfo)
{
bleDevice = BluetoothLEDevice::FromIdAsync(deviceInfo.Id()).get();
// Get the service and characteristic UUIDs
auto serviceUuid = GattDeviceService::GetDeviceSelectorFromUuid(GattServiceUuids::GenericAccess);
auto characteristicUuid = GattCharacteristicUuids::DeviceName;
// Get the service and characteristic
auto services = bleDevice.GetGattServicesForUuidAsync(serviceUuid).get();
auto characteristics = services.GetAt(0).GetCharacteristicsForUuidAsync(characteristicUuid).get();
characteristic = characteristics.GetAt(0);
// Subscribe to notifications
characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue::Notify).get();
}
void SendPayload()
{
// Send data over the characteristic
const char* data = "{\"code\":0,\"payload\":\"81 1D 24 24 02 00 64 32 FF 00 02 00 30 AF\"}";
auto writer = DataWriter();
writer.WriteString(data);
characteristic.WriteValueWithResultAsync(writer.DetachBuffer()).get();
}
std::string ReceiveData()
{
// Listen for notifications
auto result = std::string();
characteristic.ValueChanged([&](auto&& sender, auto&& args)
{
auto reader = DataReader(args.CharacteristicValue());
result = reader.ReadString(args.CharacteristicValue().Length());
printf("Received data: %s\n", result.c_str());
});
return result;
}
};
int main()
{
init_apartment();
// Find the device with the specified name
auto devices = DeviceInformation::FindAllAsync(BluetoothLEDevice::GetDeviceSelector()).get();
auto device = std::find_if(devices.begin(), devices.end(), [](const auto& d) { return d.Name() == L"LEDnetWF"; });
// Connect to the device
BLEDevice ble(*device);
// Send payload and output result
ble.SendPayload();
auto result = ble.ReceiveData();
printf("Result: %s\n", result.substr(0,8).c_str());
return 0;
}
#include <winrt/Windows.Devices.Bluetooth.h>
#include <winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h>
#include <winrt/Windows.Devices.Enumeration.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Storage.Streams.h>
using namespace winrt;
using namespace Windows::Devices::Bluetooth;
using namespace Windows::Devices::Bluetooth::GenericAttributeProfile;
using namespace Windows::Devices::Enumeration;
using namespace Windows::Foundation;
using namespace Windows::Storage::Streams;
struct BLEDevice
{
DeviceInformation deviceInfo;
BluetoothLEDevice bleDevice;
GattCharacteristic characteristic;
BLEDevice(DeviceInformation deviceInfo) : deviceInfo(deviceInfo)
{
bleDevice = BluetoothLEDevice::FromIdAsync(deviceInfo.Id()).get();
// Get the service and characteristic UUIDs
auto serviceUuid = GattDeviceService::GetDeviceSelectorFromUuid(GattServiceUuids::GenericAccess);
auto characteristicUuid = GattCharacteristicUuids::DeviceName;
// Get the service and characteristic
auto services = bleDevice.GetGattServicesForUuidAsync(serviceUuid).get();
auto characteristics = services.GetAt(0).GetCharacteristicsForUuidAsync(characteristicUuid).get();
characteristic = characteristics.GetAt(0);
// Subscribe to notifications
characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue::Notify).get();
}
void SendPayload()
{
// Send data over the characteristic
const char* data = "{\"code\":0,\"payload\":\"81 1D 24 24 02 00 64 32 FF 00 02 00 30 AF\"}";
auto writer = DataWriter();
writer.WriteString(data);
characteristic.WriteValueWithResultAsync(writer.DetachBuffer()).get();
}
std::string ReceiveData()
{
// Listen for notifications
auto result = std::string();
characteristic.ValueChanged([&](auto&& sender, auto&& args)
{
auto reader = DataReader(args.CharacteristicValue());
result = reader.ReadString(args.CharacteristicValue().Length());
printf("Received data: %s\n", result.c_str());
printf("Result: %s\n", result.substr(0,8).c_str());
});
return result;
}
};
int main()
{
init_apartment();
// Find the device with the specified name
auto devices = DeviceInformation::FindAllAsync(BluetoothLEDevice::GetDeviceSelector()).get();
auto device = std::find_if(devices.begin(), devices.end(), [](const auto& d) { return d.Name().substr(0,8) == L"LEDnetWF"; });
// Connect to the device
BLEDevice ble(*device);
// Send payload and output result
ble.SendPayload();
ble.ReceiveData();
return 0;
}
*/