RFCOMM蓝牙直连:从BlueCove到In The Hand 32Feet
RFCOMM Bluetooth Direct Connection: from BlueCove to In The Hand 32Feet
此代码使用 Java Bluecove Bluetooh Library 工作正常。只是探测RFCCOMM直连的主要方法
我正在尝试基于 "In The Hand 32Feet" 实用程序库在 C# 中做同样的事情我想发送一个文本命令并接收来自设备的响应。
对于 Java 基于 BlueCove(工作正常!)
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
这是我目前尝试将其转换为 c#....但问题是我无法访问 peerStream 已关闭? (始终return CanRead 为 false)。
正如您在 Java 中看到的那样,BlueCove 示例可以打开具有双重用途的连接:OutputStream 用于写入命令,InputStream 用于顺序读取结果
我可以用其他方式做到这一点吗?
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();
我认为对 sw.Close() 的调用也会关闭底层流,因此您无法在关闭 sw 后从 peerStream 读取。
我会尝试这样的事情:
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());
}
}
}
}
此代码使用 Java Bluecove Bluetooh Library 工作正常。只是探测RFCCOMM直连的主要方法
我正在尝试基于 "In The Hand 32Feet" 实用程序库在 C# 中做同样的事情我想发送一个文本命令并接收来自设备的响应。
对于 Java 基于 BlueCove(工作正常!)
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
这是我目前尝试将其转换为 c#....但问题是我无法访问 peerStream 已关闭? (始终return CanRead 为 false)。
正如您在 Java 中看到的那样,BlueCove 示例可以打开具有双重用途的连接:OutputStream 用于写入命令,InputStream 用于顺序读取结果
我可以用其他方式做到这一点吗?
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();
我认为对 sw.Close() 的调用也会关闭底层流,因此您无法在关闭 sw 后从 peerStream 读取。
我会尝试这样的事情:
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());
}
}
}
}