static NativeMethods.BLUETOOTH_GATT_FLAGS GetGattFlags(BleRequestFlags requestFlags) { NativeMethods.BLUETOOTH_GATT_FLAGS flags = 0; if (0 != (requestFlags & BleRequestFlags.Authenticated)) { flags |= NativeMethods.BLUETOOTH_GATT_FLAGS.AUTHENTICATED; } if (0 != (requestFlags & BleRequestFlags.Encrypted)) { flags |= NativeMethods.BLUETOOTH_GATT_FLAGS.ENCRYPTED; } return(flags); }
unsafe void WriteCharacteristic(BleCharacteristic characteristic, byte[] value, int offset, int count, NativeMethods.BLUETOOTH_GATT_FLAGS flags) { Throw.If.Null(characteristic, "characteristic").Null(value, "value").OutOfRange(value, offset, count); HandleAcquireIfOpenOrFail(); try { lock (_writeSync) { int error; var wc = (WinBleCharacteristic)characteristic; var cb = stackalloc byte[NativeMethods.BTH_LE_GATT_CHARACTERISTIC_VALUE.Size + count]; var cv = (NativeMethods.BTH_LE_GATT_CHARACTERISTIC_VALUE *)cb; cv->DataSize = (uint)count; Marshal.Copy(value, offset, (IntPtr)(void *)&cv->Data[0], count); error = NativeMethods.BluetoothGATTSetCharacteristicValue(_handle, ref wc.NativeData, cv, 0, flags); if (error != 0) { var message = string.Format("Failed to write {0} bytes to characteristic {1}.", count, characteristic); throw DeviceException.CreateIOException(Device, message, error); } } } finally { HandleRelease(); } }