Hey all i am having problems with getting this HIDLibrary.dll code to pick up my Sony Bluetooth Remote.
This is the code:
Code:
using System;
using System.Collections.Generic;
using System.Timers;
using HIDLibrary;
namespace PS3_BluMote
{
public class PS3RemoteDevice
{
public event EventHandler<ButtonData> ButtonAction;
public event EventHandler<EventArgs> Connected;
public event EventHandler<EventArgs> Disconnected;
private HidDevice HidRemote;
private Timer TimerFindRemote;
private Timer TimerHibernation;
private int _vendorID = 0x054c;
private int _productID = 0x0306;
private int _batteryLife = 100;
private bool _hibernationEnabled = false;
public PS3RemoteDevice(int VendorID, int ProductID, bool HibernationEnabled)
{
if (HibernationEnabled)
{
TimerHibernation = new Timer();
TimerHibernation.Interval = 60000;
TimerHibernation.Elapsed += new ElapsedEventHandler(TimerHibernation_Elapsed);
}
_vendorID = VendorID;
_productID = ProductID;
_hibernationEnabled = HibernationEnabled;
}
public void Connect()
{
if (!FindRemote())
{
StartRemoteFindTimer();
}
}
public int BatteryLife
{
get { return _batteryLife; }
}
private void TimerFindRemote_Elapsed(object sender, ElapsedEventArgs e)
{
if (HidRemote == null)
{
if (FindRemote())
{
TimerFindRemote.Enabled = false;
}
}
}
private void TimerHibernation_Elapsed(object sender, ElapsedEventArgs e)
{
// TODO
}
private void ReadButtonData(HidDeviceData InData)
{
if (_hibernationEnabled)
{
TimerHibernation.Enabled = false;
}
if (InData.Status == HidDeviceData.ReadStatus.Success)
{
//Raise event with byte array.
if (ButtonAction != null)
{
byte[] bCode = new byte[4];
bCode[0] = InData.Data[1];
bCode[1] = InData.Data[2];
bCode[2] = InData.Data[3];
bCode[3] = InData.Data[4];
System.Windows.Forms.MessageBox.Show("Worked", Convert.ToString(bCode), System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
ButtonAction(this, new ButtonData(bCode));
}
_batteryLife = InData.Data[11] * 20; //Set battery life.
if (_hibernationEnabled)
{
TimerHibernation.Enabled = true;
}
HidRemote.Read(ReadButtonData); //Read next button pressed.
}
else
{
if (Disconnected != null)
{
Disconnected(this, new EventArgs());
}
DisposeRemote(); //Dispose of current remote.
Connect(); //Try to reconnect.
}
}
private void StartRemoteFindTimer()
{
if (TimerFindRemote == null)
{
TimerFindRemote = new Timer();
TimerFindRemote.Interval = 1000;
TimerFindRemote.Elapsed += new ElapsedEventHandler(TimerFindRemote_Elapsed);
}
TimerFindRemote.Enabled = true;
}
private bool FindRemote()
{
HidDevice[] HidDeviceList = HidDevices.Enumerate(_vendorID, _productID);
if (HidDeviceList.Length > 0)
{
//Form1 theForm = new Form1();
//theForm.labelShow("HI");
HidRemote = HidDeviceList[0];
HidRemote.Open();
if (Connected != null)
{
System.Windows.Forms.MessageBox.Show("Connected", "yes", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
Connected(this, new EventArgs());
}
HidRemote.Read(ReadButtonData);
return true;
}
return false;
}
private void DisposeRemote()
{
HidRemote.Dispose();
HidRemote = null;
}
}
public class ButtonData : EventArgs
{
private byte[] _data;
public ButtonData()
{
}
public ButtonData(byte[] ByteArray)
{
_data = ByteArray;
}
public byte[] Data
{
get { return _data; }
set { _data = value; }
}
public string DataString
{
get
{
string Result = "";
foreach (byte by in _data)
{
Result += by.ToString("x2");
}
return Result;
}
}
}
}
And i start out the code like so:
Code:
private void button1_Click(object sender, EventArgs e)
{
PS3RemoteDevice PS3R = new PS3RemoteDevice(0x054c,0x0306,false);
PS3R.Connect();
}
This is the flow it does when i step through the code:
Code:
-PS3RemoteDevice()
-VenderID = 1356
-ProductID = 774
-HibernationEnabled = false
-Connect
- FindRemote
-HidDeviceList.Length = 1
-HidRemote = {HID-compliant game controller (0x054C, 0x0306, 0)}
-HidRemote.open
-isOpened = True
-isConnected = false
-Connected = null
-ReadButtonData
-Disconnected = null
-DisposeRemote
-Connect
and it just keeps looping another 4 or 5 times. So what i can not figure out is how it can open it but not connect to it? And the bluetooth remote is on and sending data i know that for sure.
Any help would be great!
David