Reputation: 395
I'am trying to send simple string message via bluetooth to another device. My code looks like this:
private ICollection<BluetoothDevice> devicesArray;
private void SendBtn_Click(object sender, EventArgs e)
{
BluetoothDevice bd = null;
foreach(var o in PairedListView.pairedDevices)
{
if(o.Selected == true)
{
bd = getPairedDevice(o.DeviceAddress);
break;
}
}
if(bd != null)
{
UUID uuid = UUID.FromString("00001101-0000-1000-8000-00805f9b34fb");
BluetoothSocket socket = bd.CreateRfcommSocketToServiceRecord(uuid);
socket.Connect();
Stream stream = socket.OutputStream;
byte[] array = Encoding.ASCII.GetBytes("Testing message!");
stream.Write(array, 0, array.Length);
socket.Close();
}
else
{
Alerts.showAlertMessage("Choose device", this);
}
}
private BluetoothDevice getPairedDevice(string address)
{
BluetoothDevice bd = null;
foreach(var o in devicesArray)
{
if(o.Address == address)
{
bd = o;
break;
}
}
return bd;
}
I'am expecting receive string message on choosen device. The problem is I'am getting error
Java.IO.IOException: Service discovery failed
When trying to connect. Could You tell me what I'am doing wrong?
Upvotes: 0
Views: 1907
Reputation: 11
I know this is an old example but to anyone looking for a solution to this exact problem , the problem is u call the socket.close() before starting the connection so all u need to do is to remove socket.close() any everything should work just fine
Upvotes: 1
Reputation: 123
You might try socket.getOutputStream() to receive the associated OutputStream. Check with socket.isConnected() the current state.
But look at the official Google example (bluetooth chat): http://developer.android.com/samples/BluetoothChat/index.html
Upvotes: 1