📄 bluetoothradio.cs
字号:
using System;
using System.Collections;
using Microsoft.Win32;
namespace bluetoothX
{
public class BluetoothRadio
{
#region IsSupported
public static bool IsSupported
{
get
{
return (AllRadios.Length > 0);
}
}
#endregion
private BLUETOOTH_RADIO_INFO radio;
private IntPtr handle;
#region Constructor
internal BluetoothRadio(IntPtr handle)
{
radio = new BLUETOOTH_RADIO_INFO();
radio.dwSize = 520;
int hresult = NativeMethods.BluetoothGetRadioInfo(handle, ref radio);
if(hresult!=0)
{
throw new System.ComponentModel.Win32Exception(hresult, "Error retrieving Radio information.");
}
this.handle = handle;
}
#endregion
#region Primary Radio
public static BluetoothRadio PrimaryRadio
{
get
{
//get a single radio
IntPtr handle = IntPtr.Zero;
IntPtr findhandle = IntPtr.Zero;
BLUETOOTH_FIND_RADIO_PARAMS bfrp;
bfrp.dwSize = 4;
findhandle = NativeMethods.BluetoothFindFirstRadio(ref bfrp, ref handle);
if(findhandle!=IntPtr.Zero)
{
NativeMethods.BluetoothFindRadioClose(findhandle);
}
if(handle!=IntPtr.Zero)
{
return new BluetoothRadio(handle);
}
return null;
}
}
#endregion
#region All Radios
public static BluetoothRadio[] AllRadios
{
get
{
IntPtr handle = IntPtr.Zero;
IntPtr findhandle = IntPtr.Zero;
BLUETOOTH_FIND_RADIO_PARAMS bfrp;
bfrp.dwSize = 4;
ArrayList radiocollection = new ArrayList();
findhandle = NativeMethods.BluetoothFindFirstRadio(ref bfrp, ref handle);
if(findhandle!=IntPtr.Zero)
{
radiocollection.Add(handle);
while(NativeMethods.BluetoothFindNextRadio(findhandle, ref handle))
{
radiocollection.Add(handle);
}
NativeMethods.BluetoothFindRadioClose(findhandle);
}
BluetoothRadio[] results = new BluetoothRadio[radiocollection.Count];
for(int radioindex = 0; radioindex < results.Length; radioindex++)
{
results[radioindex] = new BluetoothRadio((IntPtr)radiocollection[radioindex]);
}
return results;
}
}
#endregion
#region Handle
public IntPtr Handle
{
get
{
return this.handle;
}
}
#endregion
#region Hardware Status
public HardwareStatus HardwareStatus
{
get
{
return HardwareStatus.Unknown;
}
}
#endregion
#region Mode
public RadioMode Mode
{
get
{
if(NativeMethods.BluetoothIsDiscoverable(handle))
{
return RadioMode.Discoverable;
}
if(NativeMethods.BluetoothIsConnectable(handle))
{
return RadioMode.Connectable;
}
return RadioMode.PowerOff;
}
set
{
switch(value)
{
case RadioMode.Discoverable:
if(Mode==RadioMode.PowerOff)
{
NativeMethods.BluetoothEnableIncomingConnections(handle, true);
}
NativeMethods.BluetoothEnableDiscovery(handle, true);
break;
case RadioMode.Connectable:
if(Mode==RadioMode.Discoverable)
{
NativeMethods.BluetoothEnableDiscovery(handle, false);
}
else
{
NativeMethods.BluetoothEnableIncomingConnections(handle, true);
}
break;
case RadioMode.PowerOff:
if(Mode==RadioMode.Discoverable)
{
NativeMethods.BluetoothEnableDiscovery(handle, false);
}
NativeMethods.BluetoothEnableIncomingConnections(handle, false);
break;
}
}
}
#endregion
#region Local Address
public BluetoothAddress LocalAddress
{
get
{
return new BluetoothAddress(radio.address);
}
}
#endregion
#region Name
public string Name
{
get
{
return radio.szName;
}
set
{
}
}
#endregion
#region Class Of Device
public ClassOfDevice ClassOfDevice
{
get
{
return new ClassOfDevice(radio.ulClassofDevice);
}
}
#endregion
#region Manufacturer
public Manufacturer Manufacturer
{
get
{
return radio.manufacturer;
}
}
#endregion
#region LmpSubVersion
public int LmpSubversion
{
get
{
return radio.lmpSubversion;
}
}
#endregion
#region Stack
public Manufacturer SoftwareManufacturer
{
get
{
return Manufacturer.Microsoft;
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -