Пример #1
0
        private void OnBluetoothDeviceFound(BluetoothDevice device, byte[] advertisementPacket)
        {
            lock (addressConnectionLookup) {
                if (device.Name.IsValidSerialNumber())
                {
                    IConnection connection = null;
                    if (!addressConnectionLookup.ContainsKey(device.Address))
                    {
                        var sn = device.Name.ParseSerialNumber();

                        if (sn.rawSerial.StartsWith("S", StringComparison.Ordinal))
                        {
                            connection = new RigadoConnection(this, device, sn);
                            addressConnectionLookup[device.Address] = connection;
                            NotifyNewConnection(connection);
                        }
                        else
                        {
                            connection = new LeConnection(this, device, sn);
                            addressConnectionLookup[device.Address] = connection;
                            NotifyNewConnection(connection);
                        }
                    }
                    else
                    {
                        connection = addressConnectionLookup[device.Address];
                    }

//					connection.ReceivePacket(advertisementPacket);
                }
                else if (device.Name.IsValidRigIdentifier())
                {
                    ERigType rigType;
                    if (device.Name.TryGetRigType(out rigType))
                    {
                        IRig rig;
                        if (!addressRigLookup.TryGetValue(device.Address, out rig))
                        {
                            switch (rigType)
                            {
                            case ERigType.Vacuum:
                                rig = new VacuumRig(this, device);
                                addressRigLookup[device.Address] = rig;
                                break;

                            case ERigType.Pressure:
//									addressRigLookup[device.Address] = rig = new PressureRig(this, device);
                                break;
                            }

                            NotifyNewRig(rig);
                        }
                    }
                    else
                    {
                        Log.E(this, "Failed to find rig type for: " + device.Name);
                    }
                }
                else
                {
                    // The check if the advertisement packet has a serial number.
                    ISerialNumber sn;
                    byte[]        packet;
                    if (RigadoBroadcastParser.ParseBroadcastPacket(advertisementPacket, out sn, out packet))
                    {
                        foreach (var c in addressConnectionLookup.Values)
                        {
                            if (c.serialNumber.Equals(sn))
                            {
                                c.ReceivePacket(packet);
                            }
                        }
                    }
                    else
                    {
                        Log.D(this, "Invalid ION device");
                    }
                }
            }
        }
        // Overridden form CBCentralManagerDelegate
        public override void DiscoveredPeripheral(CBCentralManager central, CBPeripheral peripheral, NSDictionary advertisementData, NSNumber RSSI)
        {
            string name = null;

            var values = advertisementData.Values;

            var data = advertisementData[CBAdvertisement.DataManufacturerDataKey];

            if (!AttemptNameFetch(peripheral, advertisementData, out name))
            {
//        Log.E(this, "Failed to resolve peripheral name '" + name + "'. The peripheral will not be presented to the application.");
                return;
            }

            if (!name.IsValidSerialNumber())
            {
                return;
            }

            IConnection connection = null;
            var         uuid       = CBUUID.FromNSUuid(peripheral.Identifier);

            if (connectionLookup.ContainsKey(uuid))
            {
                connection = connectionLookup[uuid];
            }

            ISerialNumber sn = null;

            byte[] packet = null;

            if (RigadoBroadcastParser.ParseBroadcastPacket(ExtractBroadcastData(advertisementData), out sn, out packet))
            {
                // We received a fully valid broadcast packet. We know that the gauge is an Appion gauge and should resolve it.
                if (connection == null)
                {
                    // We have not discovered this device before. Notify the world
                    NotifyDeviceFound(sn, uuid.ToString(), packet, EProtocolVersion.V4);
                }
                else
                {
                    // The connection already exists. Give it a new packet.
                    NotifyDeviceFound(sn, uuid.ToString(), null, EProtocolVersion.V4);
                    connection.lastPacket = packet;
                    connection.lastSeen   = DateTime.Now;
                }
            }
            else
            {
                // We didn't receive a valid broadcasting packet, but the device may still be ours.
                if (name.IsValidSerialNumber())
                {
                    // See, the device has a valid serial number.
                    sn = name.ParseSerialNumber();
                    // Check that an iserialnumber was returned
                    if (sn != null)
                    {
                        if (connection == null)
                        {
                            var p = FindProtocolFromDeviceModel(sn.deviceModel);
                            // We have not discovered this device before. Notify the world
                            NotifyDeviceFound(sn, uuid.ToString(), null, p);
                        }
                        else
                        {
                            // The connection already exists. Update the last time that it was seen.
                            var d = ion.deviceManager[sn];
                            if (d == null)
                            {
                                //Log.E(this, "Failed to get device from device manager");
                                return;
                            }
                            NotifyDeviceFound(sn, uuid.ToString(), null, d.protocol.version);
                            connection.lastSeen = DateTime.Now;
                        }
                    }
                }
                else
                {
                    Log.D(this, "Ignoring non-appion device: " + name);
                }
            }
        }