Esempio n. 1
0
        internal Task <bool> WriteValueAsyncTask(BluetoothGattAttributeHandle handle)
        {
            TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();

            Interop.Bluetooth.BtGattClientRequestCompletedCallback cb = (result, requestHandle, userData) =>
            {
                if (result == (int)BluetoothError.None)
                {
                    tcs.SetResult(true);
                }
                else
                {
                    tcs.SetResult(false);
                }
            };

            int err = Interop.Bluetooth.BtGattClientWriteValue(handle, cb, IntPtr.Zero);

            if (err.IsFailed())
            {
                GattUtil.Error(err, "Failed to write value to remote device");
                tcs.SetResult(false);
                BluetoothErrorFactory.ThrowBluetoothException(err);
            }
            return(tcs.Task);
        }
Esempio n. 2
0
        internal BluetoothGattWriteType GetWriteType()
        {
            int writeType;
            int err = Interop.Bluetooth.BtGattCharacteristicGetWriteType(_handle, out writeType);

            GattUtil.Error(err, "Failed to get characteristic writetype");
            return((BluetoothGattWriteType)writeType);
        }
Esempio n. 3
0
        internal BluetoothGattProperty GetProperties()
        {
            int properties = 0;
            int err        = Interop.Bluetooth.BtGattCharacteristicGetProperties(_handle, out properties);

            GattUtil.Error(err, "Failed to get characteristic properties");
            return((BluetoothGattProperty)properties);
        }
Esempio n. 4
0
        internal float GetValue(FloatDataType type, int offset)
        {
            float value;
            int   err = Interop.Bluetooth.BtGattGetFloatValue(_handle, (int)type, offset, out value);

            GattUtil.Error(err, "Failed to get attribute float value at offset");
            return(value);
        }
Esempio n. 5
0
        internal string GetUuid()
        {
            string uuid;
            int    err = Interop.Bluetooth.BtGattGetUuid(_handle, out uuid);

            GattUtil.Error(err, "Failed to get attribute uuid");

            return(uuid);
        }
Esempio n. 6
0
        internal byte[] GetValue()
        {
            IntPtr nativeValue;
            int    nativeValueLength;
            int    err = Interop.Bluetooth.BtGattGetValue(_handle, out nativeValue, out nativeValueLength);

            GattUtil.Error(err, "Failed to get attribute value");

            return(GattUtil.IntPtrToByteArray(nativeValue, nativeValueLength));
        }
Esempio n. 7
0
        internal void SetAttMtu(int mtu)
        {
            int err = Interop.Bluetooth.BtGattClientSetAttMtu(_handle, mtu);

            if (err.IsFailed())
            {
                GattUtil.Error(err, "Failed to set MTU value");
                BluetoothErrorFactory.ThrowBluetoothException(err);
            }
        }
Esempio n. 8
0
        internal int GetAttMtu(string clientAddress)
        {
            int err = Interop.Bluetooth.BtGattServerGetDeviceMtu(clientAddress, out int mtu);

            if (err.IsFailed())
            {
                GattUtil.Error(err, "Failed to get MTU value");
                BluetoothErrorFactory.ThrowBluetoothException(err);
            }
            return(mtu);
        }
Esempio n. 9
0
        internal int GetAttMtu()
        {
            int err = Interop.Bluetooth.BtGattClientGetAttMtu(_handle, out int mtu);

            if (err.IsFailed())
            {
                GattUtil.Error(err, "Failed to get MTU value");
                BluetoothErrorFactory.ThrowBluetoothException(err);
            }
            return(mtu);
        }
Esempio n. 10
0
        internal BluetoothGattDescriptor GetDescriptor(BluetoothGattCharacteristic characteristic, string uuid)
        {
            BluetoothGattAttributeHandle handle;
            int err = Interop.Bluetooth.BtGattCharacteristicGetDescriptor(_handle, uuid, out handle);

            if (err.IsFailed())
            {
                GattUtil.Error(err, string.Format("Failed to get descriptor with UUID ({0})", uuid));
                return(null);
            }
            BluetoothGattDescriptor descriptor = BluetoothGattDescriptorImpl.CreateBluetoothGattDescriptor(handle, uuid);

            descriptor.SetParent(characteristic);
            return(descriptor);
        }
Esempio n. 11
0
        internal BluetoothGattService GetService(BluetoothGattServer server, string uuid)
        {
            BluetoothGattAttributeHandle serviceHandle;
            int err = Interop.Bluetooth.BtGattServerGetService(_handle, uuid, out serviceHandle);

            if (err.IsFailed())
            {
                GattUtil.Error(err, string.Format("Failed to get service with UUID ({0})", uuid));
                return(null);
            }

            BluetoothGattService service = new BluetoothGattService(new BluetoothGattServiceImpl(serviceHandle), uuid);;

            service.SetParent(server);
            return(service);
        }
Esempio n. 12
0
        internal BluetoothGattCharacteristic GetCharacteristic(BluetoothGattService service, string uuid)
        {
            BluetoothGattAttributeHandle attributeHandle;
            int err = Interop.Bluetooth.BtGattServiceGetCharacteristic(_handle, uuid, out attributeHandle);

            if (err.IsFailed())
            {
                GattUtil.Error(err, string.Format("Failed to get Characteristic with UUID ({0})", uuid));
                return(null);
            }

            BluetoothGattCharacteristic Characteristic = BluetoothGattCharacteristicImpl.CreateBluetoothGattGattCharacteristic(attributeHandle, uuid);

            Characteristic.SetParent(service);
            return(Characteristic);
        }
Esempio n. 13
0
        internal IEnumerable <BluetoothGattDescriptor> GetDescriptors(BluetoothGattCharacteristic characteristic)
        {
            List <BluetoothGattDescriptor> attribututeList = new List <BluetoothGattDescriptor>();

            Interop.Bluetooth.BtGattForeachCallback cb = (total, index, attributeHandle, userData) =>
            {
                BluetoothGattAttributeHandle handle     = new BluetoothGattAttributeHandle(attributeHandle, false);
                BluetoothGattDescriptor      descriptor = BluetoothGattDescriptorImpl.CreateBluetoothGattDescriptor(handle, "");
                if (descriptor != null)
                {
                    descriptor.SetParent(characteristic);
                    attribututeList.Add(descriptor);
                }
                return(true);
            };

            int err = Interop.Bluetooth.BtGattCharacteristicForeachDescriptors(characteristic.GetHandle(), cb, IntPtr.Zero);

            GattUtil.Error(err, "Failed to get all descriptor");

            return(attribututeList);
        }
Esempio n. 14
0
        internal IEnumerable <BluetoothGattService> GetIncludeServices(BluetoothGattService parentService)
        {
            List <BluetoothGattService> attribututeList = new List <BluetoothGattService>();

            _includedServiceForeachCallback = (total, index, attributeHandle, userData) =>
            {
                BluetoothGattAttributeHandle handle  = new BluetoothGattAttributeHandle(attributeHandle, false);
                BluetoothGattService         service = BluetoothGattServiceImpl.CreateBluetoothGattService(handle, "");
                if (service != null)
                {
                    service.SetParent(parentService);
                    attribututeList.Add(service);
                }
                return(true);
            };

            int err = Interop.Bluetooth.BtGattServiceForeachIncludedServices(parentService.GetHandle(), _includedServiceForeachCallback, IntPtr.Zero);

            GattUtil.Error(err, "Failed to get all services");

            return(attribututeList);
        }
Esempio n. 15
0
        internal Task <bool> WriteValueAsyncTask(BluetoothGattAttributeHandle handle)
        {
            TaskCompletionSource <bool> task = new TaskCompletionSource <bool>();
            int requestId = 0;

            lock (this)
            {
                requestId = _requestId++;
                _writeValueTaskSource[requestId] = task;
            }

            int err = Interop.Bluetooth.BtGattClientWriteValue(handle, _writeValueCallback, (IntPtr)requestId);

            if (err.IsFailed())
            {
                GattUtil.Error(err, "Failed to write value to remote device");
                task.SetResult(false);
                _writeValueTaskSource.Remove(requestId);
                BluetoothErrorFactory.ThrowBluetoothException(err);
            }
            return(task.Task);
        }
Esempio n. 16
0
        internal IEnumerable <BluetoothGattCharacteristic> GetCharacteristics(BluetoothGattService service)
        {
            List <BluetoothGattCharacteristic> attribututeList = new List <BluetoothGattCharacteristic>();

            Interop.Bluetooth.BtGattForeachCallback cb = (total, index, attributeHandle, userData) =>
            {
                BluetoothGattAttributeHandle handle         = new BluetoothGattAttributeHandle(attributeHandle, false);
                BluetoothGattCharacteristic  Characteristic = BluetoothGattCharacteristicImpl.CreateBluetoothGattGattCharacteristic(handle, "");
                if (Characteristic != null)
                {
                    Characteristic.SetParent(service);
                    attribututeList.Add(Characteristic);
                }
                return(true);
            };

            int err = Interop.Bluetooth.BtGattServiceForeachCharacteristics(service.GetHandle(), cb, IntPtr.Zero);

            GattUtil.Error(err, "Failed to get all Characteristic");

            return(attribututeList);
        }
Esempio n. 17
0
        internal IEnumerable <BluetoothGattService> GetServices(BluetoothGattServer server)
        {
            List <BluetoothGattService> attribututeList = new List <BluetoothGattService>();

            Interop.Bluetooth.BtGattForeachCallback cb = (total, index, attributeHandle, userData) =>
            {
                BluetoothGattAttributeHandle handle  = new BluetoothGattAttributeHandle(attributeHandle, false);
                BluetoothGattService         service = BluetoothGattServiceImpl.CreateBluetoothGattService(handle, "");;
                if (service != null)
                {
                    service.SetParent(server);
                    attribututeList.Add(service);
                }
                return(true);
            };

            int err = Interop.Bluetooth.BtGattServerForeachServices(_handle, cb, IntPtr.Zero);

            GattUtil.Error(err, "Failed to get all services");

            return(attribututeList);
        }
Esempio n. 18
0
        internal Task <bool> SendIndicationAsync(BluetoothGattServer server, BluetoothGattCharacteristic characteristic, string clientAddress)
        {
            TaskCompletionSource <bool> task = new TaskCompletionSource <bool>();
            int requestId = 0;

            lock (this)
            {
                requestId = _requestId++;
                _sendIndicationTaskSource[requestId] = task;
            }

            int err = Interop.Bluetooth.BtGattServerNotify(characteristic.GetHandle(), _sendIndicationCallback, clientAddress, (IntPtr)requestId);

            if (err.IsFailed())
            {
                GattUtil.Error(err, string.Format("Failed to send value changed indication for characteristic uuid {0}", characteristic.Uuid));
                task.SetResult(false);
                _sendIndicationTaskSource.Remove(requestId);
                BluetoothErrorFactory.ThrowBluetoothException(err);
            }
            return(task.Task);
        }
Esempio n. 19
0
        internal void SetNotificationStateChangedEvent(Interop.Bluetooth.BtGattServerNotificationStateChangeCallback callback)
        {
            int err = Interop.Bluetooth.BtGattServeSetNotificationStateChangeCallback(_handle, callback, IntPtr.Zero);

            GattUtil.Error(err, "Failed to set characteristic notification state changed callback");
        }
Esempio n. 20
0
        internal void SetCharacteristicValueChangedEvent(Interop.Bluetooth.BtClientCharacteristicValueChangedCallback callback)
        {
            int err = Interop.Bluetooth.BtGattClientSetCharacteristicValueChangedCallback(_handle, callback, IntPtr.Zero);

            GattUtil.Error(err, "Failed to set client characteristic value changed callback");
        }
Esempio n. 21
0
        internal void SetProperties(BluetoothGattProperty perperties)
        {
            int err = Interop.Bluetooth.BtGattCharacteristicSetProperties(_handle, (int)perperties);

            GattUtil.Error(err, "Failed to set characteristic properties");
        }
Esempio n. 22
0
        internal void UnsetCharacteristicValueChangedEvent()
        {
            int err = Interop.Bluetooth.BtGattClientUnsetCharacteristicValueChangedCallback(_handle);

            GattUtil.Error(err, "Failed to unset client characteristic value changed callback");
        }
Esempio n. 23
0
        internal void SetWriteType(BluetoothGattWriteType writeType)
        {
            int err = Interop.Bluetooth.BtGattCharacteristicSetWriteType(_handle, (int)writeType);

            GattUtil.Error(err, "Failed to get characteristic writetype");
        }