This code works fine using Java Bluecove Bluetooh Library. It's just a main method to probe a RFCCOMM direct connection.
I'm trying to do the same in C# based on "In The Hand 32Feet" utility libray i would like send a text command and receive a response from device.
For Java Based On BlueCove ( Works fine ! )
String serverURL = "btspp://XXXXXXXXXXXX:1;authenticate=false;encrypt=false;master=false";
StreamConnection sc = (StreamConnection) MicroeditionConnector.open(serverURL);
DataOutputStream os = sc.openDataOutputStream();
String text = "Send command";
byte data[] = text.getBytes();
os.write(data);
os.flush();
os.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(sc.openInputStream()));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
out.append(line);
}
System.out.println("Receive data" + out.toString());
reader.close();
For C# Based On In The Hand 32 Feet
This is my current attempt at converting it into to c#.... but the problem is that I haven't access to peerStream is closed ? (always return CanRead to false) .
As you can see in the Java BlueCove Sample is possible to open a connection with a double purpose : OutputStream to write commands and a InputStream to read the result sequentially
Can I do this in another way?
String address = "00:00:00:00:00:00";
Guid mUUID = new Guid("00000000-0000-0000-0000-0000500b34fb");
BluetoothAddress addr = BluetoothAddress.Parse(address);
var btEndpoint = new BluetoothEndPoint(addr, mUUID);
var btClient = new BluetoothClient();
btClient.connect(btEndpoint);
Stream peerStream = btClient.GetStream();
using (StreamWriter sw = new StreamWriter(peerStream))
{
sw.WriteLine("Send command");
sw.Flush();
sw.Close();
}
if (peerStream.CanRead)
{
using (StreamReader sr = new StreamReader(peerStream))
{
while (sr.Peek() >= 0)
{
Debug.WriteLine("Receive data" + sr.ReadLine());
}
sr.Close();
}
}
btClient.Close();
btClient.Dispose();
I think the call to sw.Close() closes the underlying stream as well, an therefore you cant read from peerStream after closing sw.
I would try something like this:
String address = "00:00:00:00:00:00";
Guid mUUID = new Guid("00000000-0000-0000-0000-0000500b34fb");
BluetoothAddress addr = BluetoothAddress.Parse(address);
var btEndpoint = new BluetoothEndPoint(addr, mUUID);
var btClient = new BluetoothClient();
btClient.connect(btEndpoint);
using (Stream peerStream = btClient.GetStream())
using (StreamWriter sw = new StreamWriter(peerStream))
{
sw.WriteLine("Send command");
sw.Flush();
if (peerStream.CanRead)
{
using (StreamReader sr = new StreamReader(peerStream))
{
while (sr.Peek() >= 0)
{
Debug.WriteLine("Receive data" + sr.ReadLine());
}
}
}
}