Exemplo 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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        internal void RegisterGattService(BluetoothGattServer server, BluetoothGattService service)
        {
            int err = Interop.Bluetooth.BtGattServerRegisterService(_handle, service.GetHandle());

            GattUtil.ThrowForError(err, "Failed to Register service");

            service.SetParent(server);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        internal void UnregisterGattService(BluetoothGattService service)
        {
            int err = Interop.Bluetooth.BtGattServerUnregisterService(_handle, service.GetHandle());

            GattUtil.ThrowForError(err, "Failed to Unregister service");

            service.UnregisterService();
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
0
        internal BluetoothGattServerImpl()
        {
            int err = Interop.Bluetooth.BtGattServerInitialize();

            GattUtil.ThrowForError(err, "Failed to initialize server");

            err = Interop.Bluetooth.BtGattServerCreate(out _handle);
            GattUtil.ThrowForError(err, "Failed to create server");
        }
Exemplo n.º 8
0
        internal string GetUuid()
        {
            string uuid;
            int    err = Interop.Bluetooth.BtGattGetUuid(_handle, out uuid);

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

            return(uuid);
        }
Exemplo n.º 9
0
        internal string GetRemoteAddress()
        {
            string remoteAddress;
            int    err = Interop.Bluetooth.BtGattClientGetRemoteAddress(_handle, out remoteAddress);

            GattUtil.ThrowForError(err, "Failed to get remote address for this client");

            return(remoteAddress);
        }
Exemplo n.º 10
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);
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Sets the string value as a specified offset.
        /// </summary>
        /// <param name="value">value to set</param>
        /// <exception cref="InvalidOperationException">Throws exception if the value is null.</exception>
        /// <since_tizen> 3 </since_tizen>
        public void SetValue(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                GattUtil.ThrowForError((int)BluetoothError.InvalidParameter, "value should not be null");
            }

            byte[] val = Encoding.UTF8.GetBytes(value);
            Impl.SetValue(val);
        }
Exemplo n.º 12
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));
        }
Exemplo n.º 13
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);
        }
Exemplo n.º 14
0
        internal BluetoothGattServerImpl()
        {
            _sendIndicationCallback = SendIndicationCallback;

            int err = Interop.Bluetooth.BtGattServerInitialize();

            GattUtil.ThrowForError(err, "Failed to initialize server");

            err = Interop.Bluetooth.BtGattServerCreate(out _handle);
            GattUtil.ThrowForError(err, "Failed to create server");
        }
Exemplo n.º 15
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);
        }
Exemplo n.º 16
0
        internal static BluetoothGattService CreateBluetoothGattService(BluetoothGattAttributeHandle handle, string uuid)
        {
            if (uuid == "")
            {
                int err = Interop.Bluetooth.BtGattGetUuid(handle, out uuid);
                GattUtil.ThrowForError(err, "Failed to get UUID");
            }

            BluetoothGattServiceImpl impl = new BluetoothGattServiceImpl(handle);

            return(new BluetoothGattService(impl, uuid));
        }
Exemplo n.º 17
0
 internal BluetoothGattClientImpl(string remoteAddress)
 {
     if (BluetoothAdapter.IsBluetoothEnabled)
     {
         int err = Interop.Bluetooth.BtGattClientCreate(remoteAddress, out _handle);
         GattUtil.ThrowForError(err, "Failed to get native client handle");
     }
     else
     {
         BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
     }
 }
Exemplo n.º 18
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);
        }
Exemplo n.º 19
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);
        }
Exemplo n.º 20
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);
        }
Exemplo n.º 21
0
        internal static BluetoothGattDescriptor CreateBluetoothGattDescriptor(BluetoothGattAttributeHandle handle, string uuid)
        {
            int permission;
            int err = Interop.Bluetooth.BtGattDescriptorGetPermissions(handle, out permission);

            GattUtil.ThrowForError(err, string.Format("Failed to get permissions with UUID ({0})", uuid));

            if (uuid == "")
            {
                int ret = Interop.Bluetooth.BtGattGetUuid(handle, out uuid);
                GattUtil.ThrowForError(ret, "Failed to get UUID");
            }

            BluetoothGattDescriptorImpl impl = new BluetoothGattDescriptorImpl(handle);

            return(new BluetoothGattDescriptor(impl, uuid, (BluetoothGattPermission)permission));
        }
Exemplo n.º 22
0
        internal static BluetoothGattCharacteristic CreateBluetoothGattGattCharacteristic(BluetoothGattAttributeHandle handle, string uuid)
        {
            int permission;
            int err = Interop.Bluetooth.BtGattCharacteristicGetPermissions(handle, out permission);

            GattUtil.ThrowForError(err, "Failed to get permissions");

            if (uuid == "")
            {
                err = Interop.Bluetooth.BtGattGetUuid(handle, out uuid);
                GattUtil.ThrowForError(err, "Failed to get UUID");
            }

            BluetoothGattCharacteristicImpl impl = new BluetoothGattCharacteristicImpl(handle);

            return(new BluetoothGattCharacteristic(impl, uuid, (BluetoothGattPermission)permission));
        }
Exemplo n.º 23
0
        internal Task <bool> SendIndicationAsync(BluetoothGattServer server, BluetoothGattCharacteristic characteristic, string clientAddress)
        {
            TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();

            Interop.Bluetooth.BtGattServerNotificationSentCallback cb = (result, address, serverHandle, characteristicHandle, completed, userData) =>
            {
                _notificationSent?.Invoke(characteristic, new NotificationSentEventArg(server, address, result, completed));
                if (completed)
                {
                    tcs.SetResult(true);
                }
            };

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

            GattUtil.ThrowForError(err, string.Format("Failed to send value changed indication for characteristic uuid {0}", characteristic.Uuid));

            return(tcs.Task);
        }
Exemplo n.º 24
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);
        }
Exemplo n.º 25
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);
        }
Exemplo n.º 26
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);
        }
Exemplo n.º 27
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);
        }
Exemplo n.º 28
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);
        }
Exemplo n.º 29
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);
        }
Exemplo n.º 30
0
        internal void SetWriteValueRequestedEventCallback(Interop.Bluetooth.BtGattServerWriteValueRequestedCallback callback)
        {
            int err = Interop.Bluetooth.BtGattServerSetWriteValueRequestedCallback(_handle, callback, IntPtr.Zero);

            GattUtil.ThrowForError(err, "Failed to set attribute write value requested callback");
        }