Reputation: 21
I am developing a React Native Windows application with BLE communication using a custom C++ native module. However, I am encountering build errors and issues with module registration.
Environment Details : React Native Version: 0.75.5 React Native Windows Version: 0.75.13 C++ Compiler: MSVC (VS 2022) OS: Windows 10 Bluetooth Library Used: (Windows.Devices.Bluetooth API)
I Implemented a BleModule in C++ as a native module. Registered the module using REACT_MODULE(BleModule). Attempted to include the module in ReactPackageProvider.cpp. Ensured the necessary dependencies were installed.
- Code which I have tried :
// BleModule.h
#pragma once
#include "NativeModules.h"
#include <winrt/Windows.Devices.Bluetooth.h>
#include <winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h>
#include <winrt/Windows.Devices.Enumeration.h>
namespace winrt::SampleApp::implementation
{
// Declare the module to React Native
REACT_MODULE(BleModule)
struct BleModule
{
// Default constructor (needed for React Native Windows)
BleModule() = default;
// Constructor with React Context
BleModule(winrt::Microsoft::ReactNative::IReactContext const &reactContext);
REACT_METHOD(StartScan);
void StartScan();
REACT_METHOD(ConnectToDevice);
void ConnectToDevice(std::string deviceId);
private:
winrt::Microsoft::ReactNative::IReactContext m_reactContext{nullptr};
};
}
// BleModule.cpp
#include "pch.h"
#include "BleModule.h"
using namespace winrt;
using namespace Windows::Devices::Enumeration;
using namespace Windows::Devices::Bluetooth;
namespace winrt::SampleApp::implementation
{
// Default constructor (needed for React Native Windows)
BleModule::BleModule() {}
// Constructor to store React context
BleModule::BleModule(winrt::Microsoft::ReactNative::IReactContext const &reactContext)
: m_reactContext(reactContext) {}
void BleModule::StartScan()
{
DeviceWatcher watcher = DeviceInformation::CreateWatcher(
BluetoothLEDevice::GetDeviceSelector());
watcher.Added([this](DeviceWatcher sender, DeviceInformation deviceInfo)
{
auto deviceName = deviceInfo.Name().c_str();
if (m_reactContext) {
m_reactContext.CallJSFunction(
L"RCTDeviceEventEmitter", L"emit", L"onDeviceFound", deviceName);
} });
watcher.Start();
}
void BleModule::ConnectToDevice(std::string deviceId)
{
auto asyncOp = BluetoothLEDevice::FromIdAsync(winrt::to_hstring(deviceId));
asyncOp.Completed([this](auto const &asyncInfo, auto const &status)
{
if (status == Windows::Foundation::AsyncStatus::Completed) {
auto device = asyncInfo.GetResults();
if (device && m_reactContext) {
m_reactContext.CallJSFunction(
L"RCTDeviceEventEmitter", L"emit", L"onDeviceConnected", device.Name().c_str());
}
} });
}
}
// ReactPackageProvider.cpp
#include "pch.h"
#include "ReactPackageProvider.h"
#include "NativeModules.h"
#include "BleModule.h" // Ensure this is included
using namespace winrt::Microsoft::ReactNative;
namespace winrt::SampleApp::implementation
{
void ReactPackageProvider::CreatePackage(IReactPackageBuilder const &packageBuilder) noexcept
{
AddAttributedModules(packageBuilder, true);
packageBuilder.AddModule<L"BleModule">();
}
}
Errors :
Build failed with message C:\React-Native Projects\sample_app\node_modules\react-native-windows\Microsoft.ReactNative.Cxx\NativeModules.h(984,54): error C2027: use of undefined type 'winrt::Microsoft::ReactNative::ModuleMethodInfo' [C:\React-Native Projects\sample_app\windows\SampleApp\SampleApp.vcxproj]. Check your build configuration. Command failed. Re-run the command with --logging for more information.
Expected Outcome : I expect my C++ BleModule to be recognized and registered properly in React Native Windows, allowing JavaScript to call its methods.
Can we add classes in C# instead of C++ in windows project and register C# classes ? Do I need additional steps to expose the module correctly? Is there a specific way to debug module loading in React Native Windows? Any references or working examples would be helpful.
Upvotes: 1
Views: 31