public void Start(TracingInformation tracingInformation) { if (tracingInformation == null || _enabled) { return; } _tracingInformation = tracingInformation; _peripheralManager.RemoveAllServices(); CBUUID uuidService = CBUUID.FromString(tracingInformation.ServiceId); CBUUID uuidCharacteristic = CBUUID.FromString(tracingInformation.CharacteristicId); var data = NSData.FromArray(PayloadFormatter.GetBytesToSend(new PackageData(tracingInformation.DeviceId))); var characteristic = new CBMutableCharacteristic(uuidCharacteristic, CBCharacteristicProperties.Read, data, CBAttributePermissions.Readable); var service = new CBMutableService(uuidService, true); service.Characteristics = new CBCharacteristic[] { characteristic }; _peripheralManager.AddService(service); StartAdvertisingOptions advData = new StartAdvertisingOptions { ServicesUUID = new CBUUID[] { uuidService } }; _peripheralManager.StartAdvertising(advData); TracingState.Instance.SetAdvertisingState(true); _enabled = true; _logger.LogDebug("Advertising starting. DeviceId: " + _tracingInformation.DeviceId); }
public void HandleDeviceCommunicationDiscoveredCharacteristicRead(DeviceDescriptor descriptor, byte[] payload, Action <DeviceDescriptor> onNext = null) { try { if (_discoveredDevices.TryGetValue(descriptor.Identifier, out var deviceDescriptor)) { try { if (PayloadFormatter.TryGetValue(payload, out var packageData)) { _logger.LogDebug( $"DeviceManager - Characteristic read - processing. id: {descriptor.Identifier}."); deviceDescriptor.Context = descriptor.Context; var contact = new ContactDescriptor(packageData.DeviceId, DateTime.UtcNow); DeviceProcessorProvider.GetInstance().Process(contact); Monitor.Enter(deviceDescriptor.ProcessingLock); try { deviceDescriptor.Processed = true; } finally { Monitor.Exit(deviceDescriptor.ProcessingLock); } } } catch (Exception ex) { _logger.LogError(ex, $"DeviceManager - Characteristic read - Processing failed. id: {deviceDescriptor.Identifier}."); } // Disconnect peripheral onNext?.Invoke(deviceDescriptor); } } catch (Exception ex) { _logger.LogError(ex, $"DeviceManager - Characteristic read - Processing failed. id: {descriptor.Identifier}. Reason: {ex.Message}."); } }
public override void OnCharacteristicReadRequest( BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) { base.OnCharacteristicReadRequest(device, requestId, offset, characteristic); if (_gattServer == null) { return; } var response = PayloadFormatter .GetBytesToSend(new PackageData(_tracingInformation.DeviceId)) .Skip(offset) .ToArray(); _logger.LogDebug($"Advertiser - Device connected. Sending response to {device.Address}. Offset={offset}, ResponseLength={response.Length}."); _gattServer.SendResponse(device, requestId, GattStatus.Success, offset, response); }
private string GetPayload(Message message, object payload) { return(PayloadFormatter.Serialize(message, payload)); }