i'm able to pairing bluetooth devices but i can't connect, send and receive data, any help please? i'm using xamarin
What I have tried:
BluetoothDevice bdDevice = mBluetoothAdapter.GetRemoteDevice(devAdd);
Toast.MakeText(BaseContext, "Connect to " + devName, ToastLength.Short).Show();
try
{
btSocket = bdDevice.CreateRfcommSocketToServiceRecord(MY_UUID);
}
catch (Java.IO.IOException e1)
{
Toast.MakeText(this, "Socket create failed: " + e1.Message + ".", ToastLength.Short);
}
// Discovery is resource intensive. Make sure it isn't going on
// when you attempt to connect and pass your message.
mBluetoothAdapter.CancelDiscovery();
// Establish the connection. This will block until it connects.
try
{
btSocket.ConnectAsync();
Toast.MakeText(BaseContext, "Connected ", ToastLength.Short).Show();
}
catch
{
try
{
btSocket.Close();
}
catch (Java.IO.IOException e2)
{
Toast.MakeText(BaseContext, "Unable to close socket during connection failure" + e2.Message + ".", ToastLength.Short).Show();
}
}
// Create a data stream so we can talk to server.
try
{
outStream = btSocket.OutputStream;
}
catch (Java.IO.IOException er)
{
Toast.MakeText(BaseContext, "OutputStream creator failed" + er.Message + ".", ToastLength.Short).Show();
}
}
private BluetoothSocket createBluetoothSocket(BluetoothDevice device)
{
try
{
Method m = device.Class.GetMethod("createInsecureRfcommSocketToServiceRecord", new Java.Lang.Class[] { Class });
return (BluetoothSocket)m.Invoke(device, MY_UUID);
}
catch (System.Exception e)
{
Toast.MakeText(BaseContext, "Error " + e.Message, ToastLength.Short).Show();
return device.CreateRfcommSocketToServiceRecord(MY_UUID);
}
}