Skip to content

Commit

Permalink
BluetoothClient.InquiryLength (Win32 only). Added some missing docume…
Browse files Browse the repository at this point in the history
…ntation tags. 4.0.16
  • Loading branch information
peterfoot committed Mar 4, 2021
1 parent 5880971 commit b38d841
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 7 deletions.
36 changes: 36 additions & 0 deletions InTheHand.Net.Bluetooth/BluetoothClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using InTheHand.Net.Bluetooth;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Sockets;

namespace InTheHand.Net.Sockets
Expand Down Expand Up @@ -71,28 +72,56 @@ public void Close()
DoClose();
}

/// <summary>
/// Sets whether an authenticated connection is required.
/// </summary>
public bool Authenticate
{
get => GetAuthenticate();
set => SetAuthenticate(value);
}

/// <summary>
/// Gets the underlying Socket.
/// </summary>
public Socket Client
{
get => GetClient();
}

/// <summary>
/// Gets a value indicating whether the underlying Socket for a BluetoothClient is connected to a remote host.
/// </summary>
public bool Connected
{
get => GetConnected();
}

/// <summary>
/// Sets whether an encrypted connection is required.
/// </summary>
public bool Encrypt
{
get => GetEncrypt();
set => SetEncrypt(value);
}

/// <summary>
/// Amount of time allowed to perform the query.
/// </summary>
/// <remarks>On Windows the actual value used is expressed in units of 1.28 seconds, so will be the nearest match for the value supplied.
/// The default value is 10 seconds. The maximum is 61 seconds.</remarks>
public TimeSpan InquiryLength
{
[DebuggerStepThrough]
get { return GetInquiryLength(); }
[DebuggerStepThrough]
set { SetInquiryLength(value); }
}

/// <summary>
/// Gets the name of the remote device.
/// </summary>
public string RemoteMachineName
{
get
Expand All @@ -101,11 +130,18 @@ public string RemoteMachineName
}
}

/// <summary>
/// Gets the underlying stream of data.
/// </summary>
/// <returns></returns>
public NetworkStream GetStream()
{
return DoGetStream();
}

/// <summary>
/// Closes the BluetoothClient and the underlying connection.
/// </summary>
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Expand Down
47 changes: 46 additions & 1 deletion InTheHand.Net.Bluetooth/BluetoothDeviceInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// InTheHand.Net.Sockets.BluetoothDeviceInfo
//
// Copyright (c) 2003-2020 In The Hand Ltd, All rights reserved.
// Copyright (c) 2003-2021 In The Hand Ltd, All rights reserved.
// This source code is licensed under the MIT License

using InTheHand.Net.Bluetooth;
Expand All @@ -11,13 +11,22 @@

namespace InTheHand.Net.Sockets
{
/// <summary>
/// Provides information about an available device obtained by the client during device discovery.
/// </summary>
public sealed partial class BluetoothDeviceInfo : IEquatable<BluetoothDeviceInfo>
{
/// <summary>
/// Forces the system to refresh the device information.
/// </summary>
public void Refresh()
{
DoRefresh();
}

/// <summary>
/// Gets the device identifier.
/// </summary>
public BluetoothAddress DeviceAddress
{
get
Expand All @@ -26,6 +35,9 @@ public BluetoothAddress DeviceAddress
}
}

/// <summary>
/// Gets the name of a device.
/// </summary>
public string DeviceName
{
get
Expand All @@ -34,6 +46,9 @@ public string DeviceName
}
}

/// <summary>
/// Returns the Class of Device of the remote device.
/// </summary>
public ClassOfDevice ClassOfDevice
{
get
Expand All @@ -42,6 +57,9 @@ public ClassOfDevice ClassOfDevice
}
}

/// <summary>
/// Returns a list of services which are already installed for use on the calling machine.
/// </summary>
public IReadOnlyCollection<Guid> InstalledServices
{
get
Expand All @@ -50,6 +68,9 @@ public IReadOnlyCollection<Guid> InstalledServices
}
}

/// <summary>
/// Specifies whether the device is connected.
/// </summary>
public bool Connected
{
get
Expand All @@ -58,6 +79,9 @@ public bool Connected
}
}

/// <summary>
/// Specifies whether the device is authenticated, paired, or bonded. All authenticated devices are remembered.
/// </summary>
public bool Authenticated
{
get
Expand All @@ -66,17 +90,38 @@ public bool Authenticated
}
}

/// <summary>
/// Enables or disables services for a Bluetooth device.
/// </summary>
/// <remarks>Only applies to Windows platform.</remarks>
/// <param name="service"></param>
/// <param name="state"></param>
public void SetServiceState(Guid service, bool state)
{
DoSetServiceState(service, state);
}

/// <summary>
/// Compares two BluetoothDeviceInfo instances for equality.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public bool Equals(BluetoothDeviceInfo other)
{
if (other is null)
return false;

return DeviceAddress == other.DeviceAddress;
}

public override bool Equals(object obj)
{
if(obj is BluetoothDeviceInfo)
{
return Equals((BluetoothDeviceInfo)obj);
}

return false;
}
}
}
4 changes: 2 additions & 2 deletions InTheHand.Net.Bluetooth/InTheHand.Net.Bluetooth.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Company>In The Hand Ltd</Company>
<Authors>Peter Foot</Authors>
<Product>32feet.NET</Product>
<Version>4.0.15</Version>
<Version>4.0.16</Version>
<RepositoryUrl>https://github.com/inthehand/32feet</RepositoryUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
Expand All @@ -17,7 +17,7 @@
<PackageLicenseUrl></PackageLicenseUrl>
<Copyright>Copyright (c) 2004-2021 In The Hand Ltd</Copyright>
<Description>32feet.NET is an open-source project to make personal area networking technologies such as Bluetooth easily accessible from .NET code.</Description>
<FileVersion>4.0.15.0215</FileVersion>
<FileVersion>4.0.16.0304</FileVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyName>InTheHand.Net.Bluetooth</AssemblyName>
<GenerateDocumentationFile Condition=" '$(Configuration)' == 'Release' ">true</GenerateDocumentationFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ void SetEncrypt(bool value)
_encrypt = value;
}

TimeSpan GetInquiryLength()
{
return TimeSpan.Zero;
}

void SetInquiryLength(TimeSpan length)
{

}

public string GetRemoteMachineName()
{
if(_socket is object && _socket.IsConnected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ void SetEncrypt(bool value)
{
}

TimeSpan GetInquiryLength()
{
return TimeSpan.Zero;
}

void SetInquiryLength(TimeSpan length)
{

}

public string GetRemoteMachineName()
{
return string.Empty;
Expand Down
27 changes: 24 additions & 3 deletions InTheHand.Net.Bluetooth/Platforms/Win32/BluetoothClient.win32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ IReadOnlyCollection<BluetoothDeviceInfo> DoDiscoverDevices(int maxDevices)
List<BluetoothDeviceInfo> devices = new List<BluetoothDeviceInfo>();

BLUETOOTH_DEVICE_SEARCH_PARAMS search = BLUETOOTH_DEVICE_SEARCH_PARAMS.Create();
search.cTimeoutMultiplier = 8;
search.fReturnAuthenticated = false;
search.fReturnRemembered = true;
search.fReturnUnknown = true;
search.fReturnConnected = true;
search.fIssueInquiry = true;

search.cTimeoutMultiplier = Convert.ToByte(inquiryLength.TotalSeconds / 1.28);

BLUETOOTH_DEVICE_INFO device = BLUETOOTH_DEVICE_INFO.Create();
IntPtr searchHandle = NativeMethods.BluetoothFindFirstDevice(ref search, ref device);
if(searchHandle != IntPtr.Zero)
Expand Down Expand Up @@ -197,7 +197,28 @@ void SetEncrypt(bool value)
_encrypt = value;
}
}


//length of time for query
private TimeSpan inquiryLength = new TimeSpan(0, 0, 10);

TimeSpan GetInquiryLength()
{
return inquiryLength;
}

void SetInquiryLength(TimeSpan length)
{
if ((length.TotalSeconds > 0) && (length.TotalSeconds <= 61))
{
inquiryLength = length;
}
else
{
throw new ArgumentOutOfRangeException("length",
"InquiryLength must be a positive TimeSpan between 0 and 61 seconds.");
}
}

internal const AddressFamily AddressFamilyBluetooth = (AddressFamily)32;
private const SocketOptionLevel SocketOptionLevelRFComm = (SocketOptionLevel)0x03;
private const SocketOptionName SocketOptionNameAuthenticate = unchecked((SocketOptionName)0x80000001);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// InTheHand.Net.Sockets.BluetoothDeviceInfo (Win32)
//
// Copyright (c) 2003-2020 In The Hand Ltd, All rights reserved.
// Copyright (c) 2003-2021 In The Hand Ltd, All rights reserved.
// This source code is licensed under the MIT License

using InTheHand.Net.Bluetooth;
Expand All @@ -21,6 +21,10 @@ internal BluetoothDeviceInfo(BLUETOOTH_DEVICE_INFO info)
_info = info;
}

/// <summary>
/// Initializes an instance of the BluetoothDeviceInfo class for the device with the given address.
/// </summary>
/// <param name="address">The BluetoothAddress.</param>
public BluetoothDeviceInfo(BluetoothAddress address)
{
_info = BLUETOOTH_DEVICE_INFO.Create();
Expand Down Expand Up @@ -83,5 +87,27 @@ void DoRefresh()
{
NativeMethods.BluetoothGetDeviceInfo(IntPtr.Zero, ref _info);
}

/// <summary>
/// Date and Time this device was last seen by the system.
/// </summary>
public DateTime LastSeen
{
get
{
return _info.LastSeen;
}
}

/// <summary>
/// Date and Time this device was last used by the system.
/// </summary>
public DateTime LastUsed
{
get
{
return _info.LastUsed;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ bool GetEncrypt()
return false;
}

TimeSpan GetInquiryLength()
{
return TimeSpan.Zero;
}

void SetInquiryLength(TimeSpan length)
{

}

void SetEncrypt(bool value)
{
}
Expand Down
10 changes: 10 additions & 0 deletions InTheHand.Net.Bluetooth/Platforms/iOS/BluetoothClient.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ void SetEncrypt(bool value)
{
}

TimeSpan GetInquiryLength()
{
return TimeSpan.Zero;
}

void SetInquiryLength(TimeSpan length)
{

}

public string GetRemoteMachineName()
{
return _accessory?.Name;
Expand Down

0 comments on commit b38d841

Please sign in to comment.