我是 C# 的新手,所以对你们中的许多人来说可能看起来很简单,但我在这里需要帮助。我必须在 c# 中创建一个桌面应用程序,该应用程序连接到蓝牙低功耗设备并使用设置特征值函数向设备发送值。为此,我需要使用获取特性函数来获取特性缓冲区。
我有 C++ 代码来做到这一点:
USHORT charBufferSize = 0;
HRESULT hres = BluetoothGATTGetCharacteristics( BLE_Service,NULL, 0,NULL,
&charBufferSize,BLUETOOTH_GATT_FLAG_NONE);
if(hres==HRESULT_FROM_WIN32(ERROR_MORE_DATA))
{
Characteristic_Buffer = new BTH_LE_GATT_CHARACTERISTIC [charBufferSize ];
}
USHORT val = 0;
// get characteristics after allocating required size
hres = BluetoothGATTGetCharacteristics(BLE_Service,NULL,charBufferSize,
Characteristic_Buffer,&val,BLUETOOTH_GATT_FLAG_NONE);
我必须在 C# 中做同样的事情所以我尝试了这样的事情
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct BTH_LE_GATT_CHARACTERISTIC
{
public UInt16 ServiceHandle;
public BTH_LE_UUID CharacteristicUuid;
public UInt16 AttributeHandle;
public UInt16 CharacteristicValueHandle;
public Boolean IsBroadcastable;
public Boolean IsReadable;
public Boolean IsWritable;
public Boolean IsWritableWithoutResponse;
public Boolean IsSignedWritable;
public Boolean IsNotifiable;
public Boolean IsIndicatable;
public Boolean HasExtendedProperties;
}
[DllImport(@"BluetoothApis.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern Boolean BluetoothGATTGetCharacteristics(
IntPtr Device_Handle,
IntPtr Service,
UInt16 Char_Buffer_Count,
ref BTH_LE_GATT_CHARACTERISTIC CharBuffer,
out UInt16 RequiredSize,
uint Flags
);
UInt16 Size_Required = 0;
UInt16 NBytes = 0;
BTH_LE_GATT_CHARACTERISTIC gattChar = new BTH_LE_GATT_CHARACTERISTIC();
Success = BluetoothGATTGetCharacteristics(Ble_Device, IntPtr.Zero, 0, IntPtr.Zero,
out Size_Required, (uint)BTH_LE_FLAGS.BLUETOOTH_GATT_FLAG_NONE);
我被困在这里。现在,要对函数进行第二次调用,我应该像在 C++ 中那样为特征缓冲区分配内存
关于如何执行此操作的任何建议或是否有任何其他方法可以在 C# 中获取特征缓冲区?
提前致谢