Hi,
The last four days I am trying to connect my Bluetooth controller "MOCUTE" (see attachment) with my smartphone, but it's still not working. I have seen a lot of posts about fixes, but none of them are working. Because it really depends on what kind of Bluetooth "MOCUTE" is using. There is actually a manual of the controller, but I can't find something like "Classic" or "BLE".
I also tried it with another Bluetooth device, namely "Mr. Handsfree Blue Easy". I've got the same problem there, the error sounds like: Java.IO.IOException: "read failed, socket might closed or timeout, read ret: -1
which is really annoying.
Now I tried so many things that I am confused about the whole Bluetooth protocol. There should be something going on with my code, but I have really no idea. Some people said that I have to create a Bluetooth server, but that didn't work as well.
Here are the UUID's of the Bluetooth devices:
MOCUTE-controller: 00001124-0000-1000-8000-00805f9b34fb
Mr. Handsfree Blue Easy: 00001108-0000-1000-8000-00805f9b34fb
So I've three classes, namely a "BluetoothHandler", "BluetoothServer" and "BluetoothListener".
Below the code of those three classes.
BluetoothHandler (that handles the Bluetooth device and "client" socket)
protected BluetoothAdapter bluetoothAdapter;
protected BluetoothServer btServer;
protected BluetoothSocket btSocket;
protected BluetoothDevice pairedBTDevice;
protected BluetoothListener btListener;
protected ParcelUuid uuid;
public BluetoothHandler()
{
BluetoothAdapter = null;
}
// This is the first method that will be called
public void Initialize()
{
BluetoothAdapter = BluetoothAdapter.DefaultAdapter;
if (BluetoothAdapter == null)
{
Console.WriteLine("Bluetooth is not available!");
return;
}
if (!BluetoothAdapter.IsEnabled)
{
BluetoothAdapter.Enable();
}
int count = 0;
var listDevices = BluetoothAdapter.BondedDevices;
if (listDevices.Count > 0)
{
foreach (var btDevice in listDevices)
{
if (btDevice.Name == "MOCUTE-032_B52-CA7E")
{
// Is this the right UUID?
UUID = btDevice.GetUuids().ElementAt(count);
pairedBTDevice = btDevice;
}
count++;
}
}
if (BluetoothAdapter.IsEnabled && pairedBTDevice != null)
{
if (pairedBTDevice.BondState == Bond.Bonded)
{
btServer = new BluetoothServer(this);
// Just trying
Thread.Sleep(3000);
Thread thread = new Thread(new ThreadStart(Connect));
thread.Start();
}
}
}
protected void Connect()
{
if (btSocket == null)
{
try
{
btSocket = pairedBTDevice.CreateRfcommSocketToServiceRecord(UUID.Uuid);
}
catch (IOException ex)
{
throw ex;
}
}
try
{
Console.WriteLine("Attempting to connect...");
btSocket.Connect();
}
catch
{
Console.WriteLine("The bluetooth connection failed...");
Console.WriteLine("Attempting to connect second time...");
try
{
// Do I need to do this?
btSocket = pairedBTDevice.CreateInsecureRfcommSocketToServiceRecord(UUID.Uuid);
btSocket.Connect();
}
catch
{
Console.WriteLine("Sorry, it failed again...");
return;
}
}
Console.WriteLine("Client socket is connected!");
Read();
}
protected void Read()
{
btListener = new BluetoothListener();
btListener.Read(btSocket);
}
BluetoothServer (Creates a "BluetoothServerSocket which is maybe not neccesary)
private BluetoothHandler bluetoothHandler;
private BluetoothServerSocket serverSocket;
private Thread thread;
public BluetoothServer(BluetoothHandler bluetoothHandler)
{
this.bluetoothHandler = bluetoothHandler;
BluetoothServerSocket tmp = null;
try
{
// Listen to the client device with the same UUID of that Bluetooth device?
tmp = bluetoothHandler.BluetoothAdapter.ListenUsingRfcommWithServiceRecord("MOCUTE-032_B52-CA7E", bluetoothHandler.UUID.Uuid);
}
catch (IOException ex)
{
throw ex;
}
serverSocket = tmp;
thread = new Thread(new ThreadStart(Run));
thread.Start();
}
protected void Run()
{
System.Console.WriteLine("Server is running...");
// Do I need to do this in a while?
while (true)
{
try
{
serverSocket.Accept();
}
catch (IOException ex)
{
System.Console.WriteLine("FAILED! === > " + ex);
}
}
}
BluetoothListener which should read the data from the "MOCUTE" controller
protected Stream stream;
public void Read(BluetoothSocket socket)
{
Stream tmpIn = null;
try
{
tmpIn = socket.InputStream;
}
catch (IOException ex)
{
throw ex;
}
stream = tmpIn;
Thread thread = new Thread(new ThreadStart(Run));
thread.Start();
}
protected void Run()
{
byte[] buffer = new byte[1024];
int bytes;
Console.WriteLine("I am waiting for the controller input...");
while (true)
{
try
{
if (stream.IsDataAvailable())
{
bytes = stream.Read(buffer, 0, buffer.Length);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
I'd hope someone could help me out!
Thank you in advance.
Jamie