public override void OnCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, GattStatus status)
 {
     try
     {
         byte[] deviceIdBytes       = characteristic.GetValue();
         string deviceIdEncountered = Encoding.UTF8.GetString(deviceIdBytes);
         DeviceIdEncountered?.Invoke(this, new BluetoothDeviceProximityDatum(_encounterTimestamp, deviceIdEncountered));
     }
     catch (Exception ex)
     {
         SensusServiceHelper.Get().Logger.Log("Exception while getting device ID characteristic value after reading it:  " + ex, LoggingLevel.Normal, GetType());
     }
     finally
     {
         DisconnectPeripheral(gatt);
     }
 }
Ejemplo n.º 2
0
        public override void DiscoveredPeripheral(CBCentralManager central, CBPeripheral peripheral, NSDictionary advertisementData, NSNumber RSSI)
        {
            peripheral.DiscoveredService += (sender, e) =>
            {
                try
                {
                    if (e.Error == null)
                    {
                        // discover characteristics for newly discovered services
                        foreach (CBService service in peripheral.Services)
                        {
                            if (service.UUID.Equals(_probe.DeviceIdService.UUID))
                            {
                                peripheral.DiscoverCharacteristics(new CBUUID[] { _probe.DeviceIdCharacteristic.UUID }, service);
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("Error while discovering service:  " + e.Error);
                    }
                }
                catch (Exception ex)
                {
                    SensusServiceHelper.Get().Logger.Log("Exception while discovering characteristics:  " + ex.Message, LoggingLevel.Normal, GetType());
                    DisconnectPeripheral(central, peripheral);
                }
            };

            peripheral.DiscoveredCharacteristic += (sender, e) =>
            {
                try
                {
                    if (e.Error == null)
                    {
                        // read device ID for newly discovered characteristics
                        foreach (CBCharacteristic characteristic in e.Service.Characteristics)
                        {
                            if (characteristic.UUID.Equals(_probe.DeviceIdCharacteristic.UUID))
                            {
                                peripheral.ReadValue(characteristic);
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("Error while discovering characteristic:  " + e.Error);
                    }
                }
                catch (Exception ex)
                {
                    SensusServiceHelper.Get().Logger.Log("Exception while reading device ID value from peripheral:  " + ex.Message, LoggingLevel.Normal, GetType());
                    DisconnectPeripheral(central, peripheral);
                }
            };

            peripheral.UpdatedCharacterteristicValue += (sender, e) =>
            {
                try
                {
                    if (e.Error == null)
                    {
                        // characteristic should have a non-null value
                        if (e.Characteristic.Value == null)
                        {
                            throw new Exception("Null updated value for characteristic.");
                        }
                        else
                        {
                            string encounteredDeviceId = Encoding.UTF8.GetString(e.Characteristic.Value.ToArray());
                            DeviceIdEncountered?.Invoke(this, new BluetoothDeviceProximityDatum(DateTime.UtcNow, encounteredDeviceId));
                        }
                    }
                    else
                    {
                        throw new Exception("Error while updating characteristic value:  " + e.Error);
                    }
                }
                catch (Exception ex)
                {
                    SensusServiceHelper.Get().Logger.Log("Exception while reporting encountered device ID:  " + ex.Message, LoggingLevel.Normal, GetType());
                }
                finally
                {
                    DisconnectPeripheral(central, peripheral);
                }
            };

            try
            {
                central.ConnectPeripheral(peripheral);
            }
            catch (Exception ex)
            {
                SensusServiceHelper.Get().Logger.Log("Exception while connecting to peripheral:  " + ex.Message, LoggingLevel.Normal, GetType());
            }
        }