Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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);
}

}
Posted
Comments
David Crow 6-Oct-17 20:38pm    
Have you considered asking this type of question in the actual Android forum? What you have here is not a simple 1-2 line question and answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900