Exemplo n.º 1
0
        internal BluetopiaError QueryName(BluetopiaDeviceInfo device,
                                          bool mayUseCached, bool mayQueryName)
        {
            const int CancelBlockingTimeSeconds = 2;

            lock (_lockDevices) {
                if (mayUseCached)
                {
                    BluetopiaDeviceInfo prev;
                    var got = _knownDevices.TryGetValue(device.DeviceAddress, out prev);
                    if (prev != null && prev.HasDeviceName)
                    {
                        device.SetName(prev.DeviceName);
                        return(BluetopiaError.OK);
                    }
                }
                //
                if (!mayQueryName)
                {
                    return(BluetopiaError.OK);
                }
                var since = DateTime.UtcNow - _lastCancelAllQueryNames;
                if (since.TotalSeconds < CancelBlockingTimeSeconds)
                {
                    Debug.WriteLine("QueryName blocked");
                    return(BluetopiaError.OK);
                }
                else     //DEBUG
                {
                }
                List <BluetopiaDeviceInfo> instList;
                var exists = _nameQueryList.TryGetValue(device.DeviceAddress, out instList);
                if (instList == null)
                {
                    Debug.Assert(!exists, "Null Value!?!");
                    instList = new List <BluetopiaDeviceInfo>();
                    _nameQueryList.Add(device.DeviceAddress, instList);
                }
                if (instList.Contains(device))
                {
                    // Already querying...
                    return(BluetopiaError.OK);
                }
                instList.Add(device);
            }//lock
            var ret = _api.GAP_Query_Remote_Device_Name(this.StackId,
                                                        BluetopiaUtils.BluetoothAddressAsInteger(device.DeviceAddress),
                                                        _HandleNameLookup, 0);

            BluetopiaUtils.WriteLineIfError(ret, "GAP_Query_Remote_Device_Name");
            return(ret);
        }