// 32feet.NET - Personal Area Networking for .NET
//
// InTheHand.Net.Bluetooth.WinCEBluetoothRadio
//
// Copyright (c) 2003-2010 In The Hand Ltd, All rights reserved.
// This source code is licensed under the In The Hand Community License - see License.txt
using System;
using System.Collections;
using System.ComponentModel;
using Microsoft.Win32;
using InTheHand.Net.Bluetooth.Factory;
#if NETCF
namespace InTheHand.Net.Bluetooth{
sealed class WindowsBluetoothRadio : IBluetoothRadio //: IDisposable
{
public string Remote { get { return null; } }
#region IsSupported
internal static bool IsPlatformSupported
{
get
{
PlatformVerification.ThrowException();
try
{
HardwareStatus status = 0;
int result = NativeMethods.BthGetHardwareStatus(ref status);
if (result == 0)
{
if (status != HardwareStatus.NotPresent)
{
return true;
}
}
}
catch
{
}
return false;
}
}
#endregion
private static bool hasBthUtil = System.IO.File.Exists("\\Windows\\BthUtil.dll");
//private IntPtr msgQueueHandle;
//private IntPtr notificationHandle;
#region Constructor
internal WindowsBluetoothRadio(IntPtr handle)
{
//get version/manufacturer
byte hv;
ushort hr;
byte lv;
ushort ls;
ushort man;
byte fea;
int hresult = NativeMethods.BthReadLocalVersion(out hv, out hr, out lv, out ls, out man, out fea);
if(hresult==0)
{
manufacturer = (Manufacturer)man;
lmpSubversion = ls;
}
else
{
manufacturer = Manufacturer.Unknown;
}
//setup message queue
/*NativeMethods.MSGQUEUEOPTIONS mqo = new NativeMethods.MSGQUEUEOPTIONS();
mqo.dwFlags = 0;
mqo.cbMaxMessage = 72;
mqo.bReadAccess = true;
mqo.dwSize = System.Runtime.InteropServices.Marshal.SizeOf(mqo);
msgQueueHandle = NativeMethods.CreateMsgQueue("InTheHand.Net.Bluetooth.BluetoothRadio", ref mqo);
notificationHandle = NativeMethods.RequestBluetoothNotifications(NativeMethods.BTE_CLASS.CONNECTIONS | NativeMethods.BTE_CLASS.DEVICE | NativeMethods.BTE_CLASS.PAIRING | NativeMethods.BTE_CLASS.STACK, msgQueueHandle);
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(EventThread));
t.IsBackground = true;
t.Start();*/
}
#endregion
#region Primary Radio
private static WindowsBluetoothRadio primaryRadio;
internal static IBluetoothRadio PrimaryRadio
{
get
{
if (primaryRadio == null)
{
if (IsPlatformSupported)
{
primaryRadio = new WindowsBluetoothRadio(IntPtr.Zero);
}
}
return primaryRadio;
}
}
#endregion
#region All Radios
internal static IBluetoothRadio[] AllRadios
{
get
{
if (IsPlatformSupported)
{
return new IBluetoothRadio[] { new WindowsBluetoothRadio(IntPtr.Zero) };
}
return new IBluetoothRadio[0] { };
}
}
#endregion
#region Handle
public IntPtr Handle
{
get
{
return IntPtr.Zero;
}
}
#endregion
#region Hardware Status
public HardwareStatus HardwareStatus
{
get
{
HardwareStatus status = 0;
int result = NativeMethods.BthGetHardwareStatus(ref status);
if (result != 0)
{
throw new System.ComponentModel.Win32Exception(result, "Error retrieving Bluetooth hardware status");
}
return status;
}
}
#endregion
#region Mode
public RadioMode Mode
{
get
{
if (hasBthUtil)
{
RadioMode val;
int result = NativeMethods.BthGetMode(out val);
if(result!=0)
{
throw new System.ComponentModel.Win32Exception(result, "Error getting BluetoothRadio mode");
}
return val;
}
else
{
byte mask;
int result = NativeMethods.BthReadScanEnableMask(out mask);
if (result != 0)
{
throw new System.ComponentModel.Win32Exception(result, "Error getting BluetoothRadio mode");
}
switch (mask)
{
case 2:
return RadioMode.Connectable;
case 3:
return RadioMode.Discoverable;
default:
return RadioMode.PowerOff;
}
}
}
set
{
int result = 0;
if (hasBthUtil)
{
result = NativeMethods.BthSetMode(value);
}
else
{
byte mask = 0;
switch (value)
{
case RadioMode.Connectable:
mask = 2;
break;
case RadioMode.Discoverable:
mask = 3;
break;
}
result = NativeMethods.BthWriteScanEnableMask(mask);
}
if(result!=0)
{
throw new System.ComponentModel.Win32Exception(result, "Error setting BluetoothRadio mode");
}
}
}
#endregion
#region Local Address
private BluetoothAddress localAddress;
public BluetoothAddress LocalAddress
{
get
{
if (localAddress == null)
{
// Get correct size of address array
byte[] bytes = new BluetoothAddress().ToByteArray();
int hresult = NativeMethods.BthReadLocalAddr(bytes);
BluetoothAddress ba = new BluetoothAddress(bytes);
if (hresult == 0)
{
localAddress = ba;
}
}
return localAddress;
}
}
#endregion
#region Name
public string Name
{
get
{
string radioName = string.Empty;
//get name from registry
RegistryKey keyName = Registry.CurrentUser.OpenSubKey("\\Software\\Microsoft\\Bluetooth\\Settings");
if (keyName != null)
{
radioName = (string)keyName.GetValue("LocalName", string.Empty);
keyName.Close();
}
return radioName;
}
set
{
RegistryKey keyName = Registry.CurrentUser.CreateSubKey("\\Software\\Microsoft\\Bluetooth\\Settings");
if (keyName != null)
{
keyName.SetValue("LocalName", value);
keyName.Close();
}
}
}
#endregion
#region Class Of Device
public ClassOfDevice ClassOfDevice
{
get
{
uint cod;
int hresult = NativeMethods.BthReadCOD(out cod);
if (hresult == 0)
{
return new ClassOfDevice(cod);
}
else
{
return new ClassOfDevice((uint)DeviceClass.Uncategorized);
}
}
}
#endregion
#region Manufacturer
private Manufacturer manufacturer;
public Manufacturer Manufacturer
{
get
{
return manufacturer;
}
}
#endregion
#region Lmp Subversion
private int lmpSubversion;
public int LmpSubversion
{
get
{
return lmpSubversion;
}
}
#endregion
#region Stack
public Manufacturer SoftwareManufacturer
{
get
{
return Manufacturer.Microsoft;
}
}
#endregion
//events
/*private void EventThread()
{
int len = 72;
IntPtr buffer = System.Runtime.InteropServices.Marshal.AllocHGlobal(len);
int received = 0;
int flags = 0;
try
{
while (msgQueueHandle != IntPtr.Zero)
{
bool success = NativeMethods.ReadMsgQueue(msgQueueHandle, buffer, len, out received, -1, out flags);
if (success)
{
NativeMethods.BTEVENT bte = (NativeMethods.BTEVENT)System.Runtime.InteropServices.Marshal.PtrToStructure(buffer, typeof(NativeMethods.BTEVENT));
switch (bte.dwEventId)
{
case NativeMethods.BTE.CONNECTION:
if (this.Connection != null)
{
this.Connection(this, EventArgs.Empty);
}
break;
case NativeMethods.BTE.DISCONNECTION:
if (this.Disconnection != null)
{
this.Disconnection(this, EventArgs.Empty);
}
break;
case NativeMethods.BTE.PAGE_TIMEOUT:
if (this.PageTimeout != null)
{
this.PageTimeout(this, EventArgs.Empty);
}
break;
}
}
}
}
finally
{
System.Runtime.InteropServices.Marshal.FreeHGlobal(buffer);
}
}*/
//public event EventHandler Connection;
//public event EventHandler Disconnection;
//public event EventHandler PageTimeout;
#region IDisposable Members
/*
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Dispose(bool disposing)
{
if (notificationHandle != IntPtr.Zero)
{
NativeMethods.StopBluetoothNotifications(notificationHandle);
notificationHandle = IntPtr.Zero;
}
if (msgQueueHandle != IntPtr.Zero)
{
NativeMethods.CloseMsgQueue(msgQueueHandle);
msgQueueHandle = IntPtr.Zero;
}
}
~BluetoothRadio()
{
Dispose(false);
}*/
#endregion
}
}
#endif
|