Example #1
0
        IBluetoothDeviceInfo[] DoDiscoverDevices(
            int maxDevices, bool authenticated, bool remembered, bool unknown, bool discoverableOnly,
            BluetoothClient.LiveDiscoveryCallback liveDiscoHandler, object liveDiscoState)
        {
            Console.WriteLine("DoDiscoverDevices");
            var discoTime = DateTime.UtcNow;
            List <IBluetoothDeviceInfo> known = new List <IBluetoothDeviceInfo>(); // hack often GC fodder
            var bus = _fcty.BluezDbus;

            known = bus.GetDeviceList_OnDefaultAdapter();
            //
            List <IBluetoothDeviceInfo> inquired = null;

            if (discoverableOnly || unknown)
            {
                inquired = new List <IBluetoothDeviceInfo>();
                double       td         = InquiryLength.TotalSeconds;
                const double Multiplier = 1.25d;
                td /= Multiplier;
                int t = (int)td;
                StackConsts.IREQ_int flags = StackConsts.IREQ_int.IREQ_CACHE_FLUSH;
                //
                var    TypeofItem = typeof(Structs.inquiry_info);
                var    SizeofItem = Marshal.SizeOf(TypeofItem);
                IntPtr pii        = BluezUtils.malloc(maxDevices * SizeofItem);
                try {
                    Console.WriteLine("Gonna hci_inquiry num_rsp: {0}, t: {1} ({2} was {3}) ", maxDevices, t, td, InquiryLength);
                    // TO-DO LAP/IAC: var lap = InquiryAccessCode;
                    //var stackTrace = new StackTrace();
                    //var msg = "Gonna hci_inquiry at: " + stackTrace;
                    //Debug.WriteLine(msg);
                    //Console.WriteLine(msg);
                    int num = NativeMethods.hci_inquiry(_fcty.DevId, t, maxDevices, IntPtr.Zero, ref pii, flags);
                    Console.WriteLine("inquiry num=" + num);
                    //BluezUtils.CheckAndThrow((BluezError)num, "hci_inquiry");
                    BluezUtils.Assert((BluezError)num, "hci_inquiry");
                    //
                    IntPtr pCur = pii;
                    for (int i = 0; i < num; i++)
                    {
                        var cur = (Structs.inquiry_info)Marshal.PtrToStructure(pCur, TypeofItem);
                        var bdi = BluezDeviceInfo.CreateFromInquiry(_fcty, cur);
                        inquired.Add(bdi);
                        pCur = PointerAdd(pCur, SizeofItem);
                    }//for
                } finally {
                    BluezUtils.free(pii);
                }
            }
            //
            var merged = BluetoothClient.DiscoverDevicesMerge(
                authenticated, remembered, unknown,
                known, inquired, discoverableOnly, discoTime);

            return(merged.ToArray());
        }
Example #2
0
        internal static BluezDeviceInfo CreateFromInquiry(BluezFactory fcty, Structs.inquiry_info cur)
        {
            var addr = BluezUtils.ToBluetoothAddress(cur.bdaddr);
            var bdi  = new BluezDeviceInfo(fcty, addr);

            bdi._cod = BluezUtils.ToClassOfDevice(cur.dev_class);
            // Note the devices here aren't 'created' by BlueZ so no ObjectPath.
            // We'll lookup/create the device later if required.
            //
            return(bdi);
        }
        void adapter_DeviceFound(string address, IDictionary <string, object> properties)
        {
            Console.WriteLine("xAdapter_DeviceFound addr: {0}, prop.Count: {1}",
                              address, properties.Count);
            if (!_livePropDumped)
            {
                _livePropDumped = true;
                BluezUtils.DumpKeys(properties);
                // "keys: Address, Class, Icon, RSSI, Name, Alias, LegacyPairing, Paired"
            }
            var bdi = BluezDeviceInfo.CreateFromInquiryLive(_fcty, properties);

            LiveDisco(bdi);
        }
Example #4
0
        internal static BluezDeviceInfo CreateFromStored(BluezFactory fcty,
                                                         ObjectPath objectPath,
                                                         BluetoothAddress address,
                                                         IDictionary <string, object> dbusDeviceDict)
        {
            var bdi = new BluezDeviceInfo(fcty, address);

            if (!_propDumped)
            {
                _propDumped = true;
                BluezUtils.DumpKeys(dbusDeviceDict);
                // "keys: Address, Name, Alias, Class, Icon, Paired, Trusted, Connected, UUIDs, Adapter"
            }
            SetProperties(objectPath, bdi, dbusDeviceDict);
            return(bdi);
        }
Example #5
0
        //----
        internal static BluezDeviceInfo CreateFromGivenAddress(BluezFactory fcty, BluetoothAddress address)
        {
            var        bdi = new BluezDeviceInfo(fcty, address);
            ObjectPath objectPath;
            var        dict = fcty.BluezDbus.FindDeviceProperties_OnDefaultAdapter(address, out objectPath);

            if (dict != null)
            {
                SetProperties(objectPath, bdi, dict);
            }
            else
            {
                Console.WriteLine("No dbus device properties.");
            }
            return(bdi);
        }
        //----
        public List <IBluetoothDeviceInfo> GetDeviceList_OnDefaultAdapter()
        {
            var a      = GetDefaultAdapter();
            var inList = GetDevices(a);

            Console.WriteLine("got Devices[]");
            //Console.WriteLine("Devices is type: " + inList.GetType().FullName);
            var outList = new List <IBluetoothDeviceInfo>();

            foreach (var curDevicePath in inList)
            {
                var d          = GetDevice(curDevicePath);
                var deviceDict = d.GetProperties();
                var addrStr    = (string)deviceDict["Address"];
                Console.WriteLine("  GDL got addrStr: " + addrStr);
                var addr = BluetoothAddress.Parse(addrStr);
                var bdi  = BluezDeviceInfo.CreateFromStored(_fcty, curDevicePath, addr, deviceDict);
                outList.Add(bdi);
            }
            return(outList);
        }
Example #7
0
        private static void EnsureHasObjectPath(BluezDeviceInfo bdi)
        {
            // A device from discovery (D-Bus or HCI) isn't 'created' by BlueZ
            // so no ObjectPath. So we have to lookup/create the device now.
            if (bdi._dbusPath != null)
            {
                return;
            }
            var pd = bdi._fcty.BluezDbus.FindDeviceProperties_OnDefaultAdapter(bdi._addr, out bdi._dbusPath);

            Debug.Assert((pd == null) == (bdi._dbusPath == null), "Xor! "
                         + Utils.MiscUtils.ToStringQuotedOrNull(pd) + " vs "
                         + Utils.MiscUtils.ToStringQuotedOrNull(bdi._dbusPath));
            if (pd == null)
            {
                // Doesn't exist, so have to create the device now.
                var a = bdi._fcty.BluezDbus.GetDefaultAdapter();
                bdi._dbusPath = a.CreateDevice(BluezUtils.FromBluetoothAddressToDbus(bdi._addr));
            }
            Debug.Assert(bdi._dbusPath != null, "NOT _dbusPath!=null after EnsureHasObjectPath.");
            Console.WriteLine("EnsureHasObjectPath after: "
                              + Utils.MiscUtils.ToStringQuotedOrNull(bdi._dbusPath));
        }
Example #8
0
        internal static BluezDeviceInfo CreateFromInquiryLive(BluezFactory fcty,
                                                              IDictionary <string, object> dbusDeviceDict)
        {
            var addr = BluetoothAddress.Parse((string)dbusDeviceDict["Address"]);
            var bdi  = new BluezDeviceInfo(fcty, addr);

            // "keys: Address, Class, Icon, RSSI, Name, Alias, LegacyPairing, Paired"
            // Note the devices here aren't 'created' by BlueZ so no ObjectPath.
            // We'll lookup/create the device later if required.
            SetProperties(null, bdi, dbusDeviceDict);
            object value;

            if (dbusDeviceDict.TryGetValue("RSSI", out value))
            {
                // Int16--Console.WriteLine("RSSI type: " + value.GetType().Name);
                bdi._rssiAtInquiry = (int?)Convert.ToInt32(value);
            }
            else
            {
                Console.WriteLine("Where's the 'RSSI' property?!?");
            }
            //
            return(bdi);
        }
Example #9
0
        private static void SetProperties(ObjectPath objectPath,
                                          BluezDeviceInfo bdi, IDictionary <string, object> dbusDeviceDict)
        {
            // Stored:
            //   "keys: Address, Name, Alias, Class, Icon, Paired, Trusted, Connected, UUIDs, Adapter"
            // D-Bus Inquiry event:
            //   "keys: Address, Name, Alias, Class, Icon, Paired; RSSI, LegacyPairing"
            //   ("keys: Address, Class, Icon, RSSI, Name, Alias, LegacyPairing, Paired")
#if DEBUG
            var checkAddr = BluetoothAddress.Parse((string)dbusDeviceDict["Address"]);
            Debug.Assert(bdi._addr == checkAddr, "NOT EQUAL address: " + bdi._addr + " checkAddr: " + checkAddr);
#endif
            bdi._rmbd     = true;
            bdi._dbusPath = objectPath;
            BluezUtils.DebugKeyExists(dbusDeviceDict, "Paired");
            bdi._authd = (bool)dbusDeviceDict["Paired"];
            BluezUtils.DebugKeyExists(dbusDeviceDict, "Connected");
            object value;
            if (dbusDeviceDict.TryGetValue("Connected", out value))
            {
                bdi._connd = (bool)value;
            }
            BluezUtils.DebugKeyExists(dbusDeviceDict, "Alias");
            bdi._name = (string)dbusDeviceDict["Alias"];
            // Seen not present when callled from CreateFromGivenAddress from BtCli.GetRemoteMachineName.
            BluezUtils.DebugKeyExists(dbusDeviceDict, "Class");
            if (dbusDeviceDict.ContainsKey("Class"))
            {
                bdi._cod = new ClassOfDevice((UInt32)dbusDeviceDict["Class"]);
            }
            else
            {
                bdi._cod = new ClassOfDevice(0);
            }
            Console.WriteLine("After SetProperties, _name: " + bdi._name);
        }
Example #10
0
 //----
 protected override IBluetoothDeviceInfo GetBluetoothDeviceInfo(BluetoothAddress address)
 {
     return(BluezDeviceInfo.CreateFromGivenAddress(this, address));
 }