Error loading custom DLL with async for BLE
Hello! This is my first post here and first time developing with Unity so I would appreciate a walk through.
I'm trying to add BLE functionality to an existing Windows 10 Unity app. I'm using a BLE dongle to interface with a hardware device (custom designed and programmed board). I created a Visual Studio C# (.NET framework) project which uses Windows.Devices.Bluetooth (and therefore needs Windows.winmd). I couldn't get Unity to import Windows.winmd so I made a DLL with the functionality.
This DLL loads Windows.Devices.Bluetooth and uses async/await calls. Mainly this: // Function to connect to device with given address: addr private async void ConnectAndStartReading(ulong addr, string s_uuid, string c_uuid) { // Connect to device device = await BluetoothLEDevice.FromBluetoothAddressAsync(addr);
// Get service async
GattDeviceServicesResult s = await device.GetGattServicesForUuidAsync(Guid.Parse(s_uuid));
service = s.Services[0];
// Get characteristic async
GattCharacteristicsResult c = await service.GetCharacteristicsForUuidAsync(Guid.Parse(c_uuid));
characteristic = c.Characteristics[0];
// Set config description value to notify
GattCommunicationStatus status = await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
// Set characteristic value change callback
characteristic.ValueChanged += Handler_CharacteristicValueChanged;
}
In the Handler_CharacteristicValueChanged function, it sets 3 variables (roll, pitch and yaw) from the buffer. I'd like the Unity program to read the 3 public variables.
Again, this works fine on a non-Unity app. (Here are some snippets)
namespace ARC_BLE_Interface_Test
{
class Program
{
public static BLE_Interface ble;
static void Main(string[] args)
{
// Device specifications
ulong dev_addr = 255782611406774;
// Service UUID
string data_stream_service_uuid = "0000fe84-0000-1000-8000-00805f9b34fb";
// Characteristic UUID
string data_stream_char_uuid = "2d30c082-f39f-4ce6-923f-3484ea480596";
ble = new BLE_Interface(dev_addr, data_stream_service_uuid, data_stream_char_uuid);
while (true)
{
Console.WriteLine(ble.roll + "\t" + ble.pitch + "\t" + ble.yaw);
Thread.Sleep(50);
}
}
}
}
but not with Unity. I have an implementation like this:
public BLE_Interface ble;
void Start()
{
ble = new BLE_Interface(device_address, data_stream_service_uuid, data_stream_char_uuid);
}
void Update()
{
ConsoleController.HotMessage(ble.roll.ToString());
}
I get the following errors: TypeLoadException: Could not find method due to a type load error Unloading broken assembly Assets/dll/ARC_BLE_Interface.dll, this assembly can cause crashes in the runtime
Any ideas?
Thank you very much! Let me know if you need any more information. I haven't really programmed in C# or Unity before yesterday.
Cheers!
Your answer
![](https://koobas.hobune.stream/wayback/20220612164113im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity Editor crashes when loading managed Plug-in (external DLL) 1 Answer
x86 build don't work 0 Answers
Windows 10 Unity access violation 1 Answer
How to use DLLs 1 Answer