Skip to content

Commit

Permalink
Renamed IBluetoothDeviceInfo.BluetoothAddress to IBluetoothDeviceInfo…
Browse files Browse the repository at this point in the history
….Address.

Changed references to Wiimote to Device in MsBluetooth and Bluesoleil.
Added better support for Balanceboard in MsBluetooth and Bluesoleil.

git-svn-id: https://wiidevicelibrary.googlecode.com/svn/trunk@8 126d8020-2c24-11de-a0ce-9586e7784243
  • Loading branch information
FrozenCow committed Apr 29, 2009
1 parent 0180545 commit 3a69e7e
Show file tree
Hide file tree
Showing 17 changed files with 185 additions and 141 deletions.
2 changes: 1 addition & 1 deletion Examples/AccelerometerExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static void deviceProvider_DeviceLost(object sender, DeviceInfoEventArgs e)

if (lostDeviceInfo is IBluetoothDeviceInfo)
{
Console.WriteLine("The address of the Bluetooth device is {0}", ((IBluetoothDeviceInfo)lostDeviceInfo).BluetoothAddress);
Console.WriteLine("The address of the Bluetooth device is {0}", ((IBluetoothDeviceInfo)lostDeviceInfo).Address);
}
}
#endregion
Expand Down
2 changes: 1 addition & 1 deletion Examples/ButtonExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static void deviceProvider_DeviceLost(object sender, DeviceInfoEventArgs e)

if (lostDeviceInfo is IBluetoothDeviceInfo)
{
Console.WriteLine("The address of the Bluetooth device is {0}", ((IBluetoothDeviceInfo)lostDeviceInfo).BluetoothAddress);
Console.WriteLine("The address of the Bluetooth device is {0}", ((IBluetoothDeviceInfo)lostDeviceInfo).Address);
}
}
#endregion
Expand Down
2 changes: 1 addition & 1 deletion Examples/ConnectExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static void deviceProvider_DeviceLost(object sender, DeviceInfoEventArgs e)

if (lostDeviceInfo is IBluetoothDeviceInfo)
{
Console.WriteLine("The address of the Bluetooth device is {0}", ((IBluetoothDeviceInfo)lostDeviceInfo).BluetoothAddress);
Console.WriteLine("The address of the Bluetooth device is {0}", ((IBluetoothDeviceInfo)lostDeviceInfo).Address);
}
}
#endregion
Expand Down
4 changes: 2 additions & 2 deletions Examples/DiscoverExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static void deviceProvider_DeviceFound(object sender, DeviceInfoEventArgs e)
// The Bluetooth-address can be used to only use Wii devices with a specific Bluetooth-address.
if (foundDeviceInfo is IBluetoothDeviceInfo)
{
Console.WriteLine("The address of the Bluetooth device is {0}", ((IBluetoothDeviceInfo)foundDeviceInfo).BluetoothAddress);
Console.WriteLine("The address of the Bluetooth device is {0}", ((IBluetoothDeviceInfo)foundDeviceInfo).Address);
// That's it for discovering Wii devices, the next step can be found in 'ConnectExample'.
}
}
Expand All @@ -68,7 +68,7 @@ static void deviceProvider_DeviceLost(object sender, DeviceInfoEventArgs e)

if (lostDeviceInfo is IBluetoothDeviceInfo)
{
Console.WriteLine("The address of the Bluetooth device is {0}", ((IBluetoothDeviceInfo)lostDeviceInfo).BluetoothAddress);
Console.WriteLine("The address of the Bluetooth device is {0}", ((IBluetoothDeviceInfo)lostDeviceInfo).Address);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Examples/IrExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static void deviceProvider_DeviceLost(object sender, DeviceInfoEventArgs e)

if (lostDeviceInfo is IBluetoothDeviceInfo)
{
Console.WriteLine("The address of the Bluetooth device is {0}", ((IBluetoothDeviceInfo)lostDeviceInfo).BluetoothAddress);
Console.WriteLine("The address of the Bluetooth device is {0}", ((IBluetoothDeviceInfo)lostDeviceInfo).Address);
}
}
#endregion
Expand Down
2 changes: 1 addition & 1 deletion Examples/LedsExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static void deviceProvider_DeviceLost(object sender, DeviceInfoEventArgs e)

if (lostDeviceInfo is IBluetoothDeviceInfo)
{
Console.WriteLine("The address of the Bluetooth device is {0}", ((IBluetoothDeviceInfo)lostDeviceInfo).BluetoothAddress);
Console.WriteLine("The address of the Bluetooth device is {0}", ((IBluetoothDeviceInfo)lostDeviceInfo).Address);
}
}
#endregion
Expand Down
2 changes: 1 addition & 1 deletion Examples/NunchukExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static void deviceProvider_DeviceLost(object sender, DeviceInfoEventArgs e)

if (lostDeviceInfo is IBluetoothDeviceInfo)
{
Console.WriteLine("The address of the Bluetooth device is {0}", ((IBluetoothDeviceInfo)lostDeviceInfo).BluetoothAddress);
Console.WriteLine("The address of the Bluetooth device is {0}", ((IBluetoothDeviceInfo)lostDeviceInfo).Address);
}
}
#endregion
Expand Down
2 changes: 1 addition & 1 deletion Examples/ReportingModeExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static void deviceProvider_DeviceLost(object sender, DeviceInfoEventArgs e)

if (lostDeviceInfo is IBluetoothDeviceInfo)
{
Console.WriteLine("The address of the Bluetooth device is {0}", ((IBluetoothDeviceInfo)lostDeviceInfo).BluetoothAddress);
Console.WriteLine("The address of the Bluetooth device is {0}", ((IBluetoothDeviceInfo)lostDeviceInfo).Address);
}
}
#endregion
Expand Down
2 changes: 1 addition & 1 deletion LinuxGUITest/DiscoverWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private void DeviceFound(object sender, DeviceInfoEventArgs args)
LogLine("Found a device, trying to connect...");
string details = "unavailable";
if(args.DeviceInfo is IBluetoothDeviceInfo)
details = ((IBluetoothDeviceInfo)(args.DeviceInfo)).BluetoothAddress.ToString();
details = ((IBluetoothDeviceInfo)(args.DeviceInfo)).Address.ToString();
iter = _ListStore.AppendValues("Device", details);
} );
try
Expand Down
17 changes: 11 additions & 6 deletions WiiDeviceLibrary/Bluetooth/Bluesoleil/BluesoleilDeviceInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,23 @@ public BluetoothService Service
get { return _Service; }
}

private BluetoothAddress _BluetoothAddress;
public BluetoothAddress BluetoothAddress
private BluetoothAddress _Address;
public BluetoothAddress Address
{
get { return _BluetoothAddress; }
get { return _Address; }
}

public string Name
{
get { return this.Device.Name; }
}
#endregion
#region Constructors
public BluesoleilDeviceInfo(BluetoothDevice device, BluetoothService service)
{
_Device = device;
_Service = service;
_BluetoothAddress = new BluetoothAddress(device.Address);
_Address = new BluetoothAddress(device.Address);
}
#endregion
#region Methods
Expand All @@ -62,12 +67,12 @@ public override bool Equals(MsHidDeviceInfo other)

public bool Equals(BluesoleilDeviceInfo other)
{
return this.BluetoothAddress == other.BluetoothAddress;
return this.Address == other.Address;
}

public override int GetHashCode()
{
return BluetoothAddress.GetHashCode();
return Address.GetHashCode();
}
#endregion
}
Expand Down
117 changes: 68 additions & 49 deletions WiiDeviceLibrary/Bluetooth/Bluesoleil/BluesoleilDeviceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class BluesoleilDeviceProvider : IDeviceProvider
private Thread discoveringThread;

private IDictionary<BluetoothAddress, BluesoleilDeviceInfo> lookupDeviceInfo = new Dictionary<BluetoothAddress, BluesoleilDeviceInfo>();
private IDictionary<BluetoothAddress, ReportWiimote> lookupDevice = new Dictionary<BluetoothAddress, ReportWiimote>();
private IDictionary<BluetoothAddress, ReportDevice> lookupDevice = new Dictionary<BluetoothAddress, ReportDevice>();
private IDictionary<BluetoothAddress, BluetoothConnection> lookupConnection = new Dictionary<BluetoothAddress, BluetoothConnection>();

private ICollection<IDeviceInfo> foundDevices = new List<IDeviceInfo>();
Expand Down Expand Up @@ -106,7 +106,7 @@ protected void Discovering()
BluetoothDevice[] devices;
try
{
// Scan for bluetooth-devices (like wiimotes).
// Scan for bluetooth-devices (like devices).
devices = bluesoleil.InquireDevices(pollingTime);
}
catch (BluesoleilFailException)
Expand All @@ -124,6 +124,8 @@ protected void Discovering()
List<BluetoothAddress> notFoundAddresses = new List<BluetoothAddress>(lookupDeviceInfo.Keys);
foreach (BluetoothDevice device in devices)
{
if (!IsWiiDevice(device))
continue;
BluetoothAddress address = new BluetoothAddress(device.Address);
if (lookupDeviceInfo.ContainsKey(address))
{
Expand All @@ -134,7 +136,7 @@ protected void Discovering()
BluetoothService[] services = null;
try
{
// Scan for bluetooth-devices (like wiimotes).
// Scan for bluetooth-devices (like devices).
services = bluesoleil.BrowseServices(device);
Thread.Sleep(1000);
}
Expand All @@ -154,16 +156,16 @@ protected void Discovering()

if (!lookupDeviceInfo.ContainsKey(address))
{
BluesoleilDeviceInfo foundWiimote = new BluesoleilDeviceInfo(device, services[1]);
OnWiimoteFound(foundWiimote);
BluesoleilDeviceInfo foundDevice = new BluesoleilDeviceInfo(device, services[1]);
OnDeviceFound(foundDevice);
}
}

// Remove the lost wiimotes from the list and notify WiimoteLost event.
// Remove the lost devices from the list and notify DeviceLost event.
foreach (BluetoothAddress notFoundAddress in notFoundAddresses)
{
BluesoleilDeviceInfo notFoundWiimoteInfo = lookupDeviceInfo[notFoundAddress];
OnWiimoteLost(notFoundWiimoteInfo);
BluesoleilDeviceInfo notFoundDeviceInfo = lookupDeviceInfo[notFoundAddress];
OnDeviceLost(notFoundDeviceInfo);
}

Thread.Sleep(1000);
Expand All @@ -173,43 +175,60 @@ protected void Discovering()
}
}

private bool IsWiiDevice(BluetoothDevice device)
{
if (device.Name == "Nintendo RVL-CNT-01")
return true;
else if (device.Name == "Nintendo RVL-WBC-01")
return true;
return false;
}

public IDevice Connect(IDeviceInfo deviceInfo)
{
BluesoleilDeviceInfo bsDeviceInfo = (BluesoleilDeviceInfo)deviceInfo;
BluetoothConnection connection = BluesoleilService.Instance.ConnectService(bsDeviceInfo.Service);
BluesoleilDeviceInfo bluetoothDeviceInfo = (BluesoleilDeviceInfo)deviceInfo;
BluetoothConnection connection = BluesoleilService.Instance.ConnectService(bluetoothDeviceInfo.Service);

ReportWiimote wiimote = null;
ReportDevice device = null;
foreach (KeyValuePair<string, SafeFileHandle> pair in MsHidDeviceProviderHelper.GetWiiDeviceHandles())
{
string devicePath = pair.Key;
SafeFileHandle fileHandle = pair.Value;
Stream communicationStream = new MsHidStream(fileHandle);
wiimote = new ReportWiimote(deviceInfo, communicationStream);
if (MsHidDeviceProviderHelper.TryConnect(wiimote, communicationStream, devicePath, fileHandle))

// determine the device type
if (bluetoothDeviceInfo.Name == "Nintendo RVL-WBC-01")
device = new ReportBalanceBoard(deviceInfo, communicationStream);
else if (bluetoothDeviceInfo.Name == "Nintendo RVL-CNT-01")
device = new ReportWiimote(deviceInfo, communicationStream);
else
throw new ArgumentException("The specified deviceInfo with name '" + bluetoothDeviceInfo.Name + "' is not supported.", "deviceInfo");

if (MsHidDeviceProviderHelper.TryConnect(device, communicationStream, devicePath, fileHandle))
break;
wiimote = null;
device = null;
}
if (wiimote == null)
if (device == null)
{
bluesoleil.DisconnectService(connection);
throw new DeviceConnectException("The connected bluetooth device was not found in the HID-list.");
}

wiimote.Disconnected += new EventHandler(wiimote_Disconnected);
lookupConnection.Add(bsDeviceInfo.BluetoothAddress, connection);
OnWiimoteConnected(wiimote);
return wiimote;
device.Disconnected += new EventHandler(device_Disconnected);
lookupConnection.Add(bluetoothDeviceInfo.Address, connection);
OnDeviceConnected(device);
return device;
}

void wiimote_Disconnected(object sender, EventArgs e)
void device_Disconnected(object sender, EventArgs e)
{
ReportWiimote wiimote = (ReportWiimote)sender;
BluesoleilDeviceInfo deviceInfo = (BluesoleilDeviceInfo)wiimote.DeviceInfo;
ReportDevice device = (ReportDevice)sender;
BluesoleilDeviceInfo deviceInfo = (BluesoleilDeviceInfo)device.DeviceInfo;
BluetoothConnection connection;
if (lookupConnection.TryGetValue(deviceInfo.BluetoothAddress, out connection))
if (lookupConnection.TryGetValue(deviceInfo.Address, out connection))
{
lookupConnection.Remove(deviceInfo.BluetoothAddress);
OnWiimoteDisconnected(wiimote);
lookupConnection.Remove(deviceInfo.Address);
OnDeviceDisconnected(device);
try
{
bluesoleil.DisconnectService(connection);
Expand All @@ -225,78 +244,78 @@ void wiimote_Disconnected(object sender, EventArgs e)
private void OnConnectionClosed(object sender, BluetoothConnectionEventArgs e)
{
BluetoothAddress address = new BluetoothAddress(e.BluetoothConnection.Service.Device.Address);
ReportWiimote wiimote;
if (lookupDevice.TryGetValue(address, out wiimote))
ReportDevice device;
if (lookupDevice.TryGetValue(address, out device))
{
wiimote.Disconnect();
device.Disconnect();
}
}

private void OnWiimoteConnected(ReportWiimote device)
private void OnDeviceConnected(ReportDevice device)
{
BluesoleilDeviceInfo deviceInfo = (BluesoleilDeviceInfo)device.DeviceInfo;

OnWiimoteLost(deviceInfo);
lookupDevice.Add(deviceInfo.BluetoothAddress, device);
OnDeviceLost(deviceInfo);
lookupDevice.Add(deviceInfo.Address, device);
connectedDevices.Add(device);
OnWiimoteConnected(new DeviceEventArgs(device));
OnDeviceConnected(new DeviceEventArgs(device));
}

private void OnWiimoteDisconnected(ReportWiimote device)
private void OnDeviceDisconnected(ReportDevice device)
{
BluesoleilDeviceInfo deviceInfo = (BluesoleilDeviceInfo)device.DeviceInfo;

lookupDevice.Remove(deviceInfo.BluetoothAddress);
lookupDevice.Remove(deviceInfo.Address);
connectedDevices.Remove(device);
OnWiimoteDisconnected(new DeviceEventArgs(device));
OnDeviceDisconnected(new DeviceEventArgs(device));
}

private void OnWiimoteFound(BluesoleilDeviceInfo deviceInfo)
private void OnDeviceFound(BluesoleilDeviceInfo deviceInfo)
{
lookupDeviceInfo.Add(deviceInfo.BluetoothAddress, deviceInfo);
lookupDeviceInfo.Add(deviceInfo.Address, deviceInfo);
foundDevices.Add(deviceInfo);

OnWiimoteFound(new DeviceInfoEventArgs(deviceInfo));
OnDeviceFound(new DeviceInfoEventArgs(deviceInfo));
}

private void OnWiimoteLost(BluesoleilDeviceInfo deviceInfo)
private void OnDeviceLost(BluesoleilDeviceInfo deviceInfo)
{
lookupDeviceInfo.Remove(deviceInfo.BluetoothAddress);
lookupDeviceInfo.Remove(deviceInfo.Address);
foundDevices.Remove(deviceInfo);

OnWiimoteLost(new DeviceInfoEventArgs(deviceInfo));
OnDeviceLost(new DeviceInfoEventArgs(deviceInfo));
}

#region Events
#region WiimoteConnected Event
protected virtual void OnWiimoteConnected(DeviceEventArgs e)
#region DeviceConnected Event
protected virtual void OnDeviceConnected(DeviceEventArgs e)
{
if (DeviceConnected == null)
return;
DeviceConnected(this, e);
}
public event EventHandler<DeviceEventArgs> DeviceConnected;
#endregion
#region WiimoteDisconnected Event
protected virtual void OnWiimoteDisconnected(DeviceEventArgs e)
#region DeviceDisconnected Event
protected virtual void OnDeviceDisconnected(DeviceEventArgs e)
{
if (DeviceDisconnected == null)
return;
DeviceDisconnected(this, e);
}
public event EventHandler<DeviceEventArgs> DeviceDisconnected;
#endregion
#region WiimoteFound Event
protected virtual void OnWiimoteFound(DeviceInfoEventArgs e)
#region DeviceFound Event
protected virtual void OnDeviceFound(DeviceInfoEventArgs e)
{
if (DeviceFound == null)
return;
DeviceFound(this, e);
}
public event EventHandler<DeviceInfoEventArgs> DeviceFound;
#endregion
#region WiimoteLost Event
protected virtual void OnWiimoteLost(DeviceInfoEventArgs e)
#region DeviceLost Event
protected virtual void OnDeviceLost(DeviceInfoEventArgs e)
{
if (DeviceLost == null)
return;
Expand Down
8 changes: 4 additions & 4 deletions WiiDeviceLibrary/Bluetooth/Bluez/BluezDeviceInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ namespace WiiDeviceLibrary.Bluetooth.Bluez
public class BluezDeviceInfo : IBluetoothDeviceInfo
{
#region Fields
private BluetoothAddress _BluetoothAddress;
private BluetoothAddress _Address;
private DateTime _LastSeen;
private string _Name = String.Empty;
#endregion

#region Properties
public BluetoothAddress BluetoothAddress
public BluetoothAddress Address
{
get { return _BluetoothAddress; }
get { return _Address; }
}

public string Name
Expand All @@ -47,7 +47,7 @@ public DateTime LastSeen

internal BluezDeviceInfo(BluetoothAddress bluetoothAddress, string name)
{
_BluetoothAddress = bluetoothAddress;
_Address = bluetoothAddress;
_Name = name;
_LastSeen = DateTime.Now;
}
Expand Down
Loading

0 comments on commit 3a69e7e

Please sign in to comment.