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); }
internal BluetoothGattWriteType GetWriteType() { int writeType; int err = Interop.Bluetooth.BtGattCharacteristicGetWriteType(_handle, out writeType); GattUtil.Error(err, "Failed to get characteristic writetype"); return((BluetoothGattWriteType)writeType); }
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); }
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); }
internal string GetUuid() { string uuid; int err = Interop.Bluetooth.BtGattGetUuid(_handle, out uuid); GattUtil.Error(err, "Failed to get attribute uuid"); return(uuid); }
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)); }
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); } }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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"); }
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"); }
internal void SetProperties(BluetoothGattProperty perperties) { int err = Interop.Bluetooth.BtGattCharacteristicSetProperties(_handle, (int)perperties); GattUtil.Error(err, "Failed to set characteristic properties"); }
internal void UnsetCharacteristicValueChangedEvent() { int err = Interop.Bluetooth.BtGattClientUnsetCharacteristicValueChangedCallback(_handle); GattUtil.Error(err, "Failed to unset client characteristic value changed callback"); }
internal void SetWriteType(BluetoothGattWriteType writeType) { int err = Interop.Bluetooth.BtGattCharacteristicSetWriteType(_handle, (int)writeType); GattUtil.Error(err, "Failed to get characteristic writetype"); }