示例#1
0
        public async Task <bool> BleClientWriteChar(BleGattCharacteristic character, byte[] data)
        {
            return(await Task.Run(() =>
            {
                if (!character.Service.GattDevice.Connected)
                {
                    Error("[CSR]:BleClientWriteChar when device is not connect!");
                    return false;
                }

                var value = Marshal.AllocHGlobal(data.Length);
                Marshal.Copy(data, 0, value, data.Length);

                if (CsrBleDll.CsrBleClientWriteCharByHandle(
                        character.Service.GattDevice.Handle, false, character.Handle, (ushort)data.Length, value))
                {
                    if (_writeEvent.WaitOne(5000))
                    {
                        Marshal.FreeHGlobal(value);
                        Debug("[CSR]:BleClientWriteChar Success!");
                        return true;
                    }
                }

                Marshal.FreeHGlobal(value);
                Debug("[CSR]:BleClientWriteChar Fail!");
                return false;
            }));
        }
示例#2
0
        public bool BleWriteChar(byte[] data)
        {
            if (!IsConnected)
            {
                Error("BleWriteChar when no device is connect!");
            }

            IntPtr value = Marshal.AllocHGlobal(data.Length);

            Marshal.Copy(data, 0, value, data.Length);

            if (CsrBleDll.CsrBleClientWriteCharByHandle(CurDevInfo.ConnHandle, false, (ushort)CurDevInfo.WriteHandle,
                                                        (ushort)data.Length, value))
            {
                Marshal.FreeHGlobal(value);

                if (_writeEvent.WaitOne(5000))
                {
                    Debug("BleWriteChar Success!");
                    return(true);
                }
            }
            Marshal.FreeHGlobal(value);
            return(false);
        }