很长时间以来,我一直在努力寻找一种可行的解决方案,以通过IP连接或蓝牙连接android设备和UWP应用程序(在PC上)。主要问题是找到一套简单易用但可以保证正常工作的代码或示例(这样我的工作就不会徒劳,现在已经有一个多星期了。)
很明显,“代码对”是不可能的(从客户端到服务器),因为所使用的库和代码结构的构建方式必须有很大的不同。另一个问题是蓝牙似乎不允许环回连接,这会导致更多的测试问题。另一个问题可能是过时的示例项目。另外,很难找到xamarin / c#解决方案,而且我不想进入Android Studio和Java(我的项目是UWP项目,android部分仅用于测试)。对于我来说,这些都是太多困难。
目前的目标(以及我的问题寻求帮助)是一项基本操作:
让我们现在忽略设备搜索(如果可能),让我们直接使用IP / MAC地址。从那里开始,一切都应该放到位。设置所有必要的功能/声明并配对设备。
我将非常感谢您的帮助。
答案 0 :(得分:1)
我自己找到了解决方案,所以它是这样的:
首先,请记住为蓝牙定义所有必要的声明和功能。这将明确地集中在代码部分。
对于Xamarin / Android客户端部分。该网站对is this one确实很有帮助。还可以尝试Xamarin众所周知的chat sample。 CreateMessage
是一种在本地设备上创建可以显示的调试消息的方法。我使它非常简单,因为我的项目主要是关于UWP部分的。所有这些都包含在try { } catch { }
子句中,但是由于有更多的缩进和括号,我现在将其省略。
using Java.Util;
using System.Text;
using System.IO;
using Android.Runtime;
using System.Threading.Tasks;
TestClass
{
// The UUIDs will be displayed down below if not known.
const string TARGET_UUID = "00001105-0000-1000-8000-00805f9b34fb";
BluetoothSocket socket = null;
OutputStreamInvoker outStream = null;
InputStreamInvoker inStream = null;
void Connect ()
{
BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter;
if (adapter == null) CreateMessage ("No Bluetooth adapter found.");
else if (!adapter.IsEnabled) CreateMessage ("Bluetooth adapter is not enabled.");
List<BluetoothDevice> L = new List<BluetoothDevice> ();
foreach (BluetoothDevice d in adapter.BondedDevices)
{
CreateMessage ("D: " + d.Name + " " + d.Address + " " + d.BondState.ToString ());
L.Add (d);
}
BluetoothDevice device = null;
device = L.Find (j => j.Name == "PC-NAME");
if (device == null) CreateMessage ("Named device not found.");
else
{
CreateMessage ("Device has been found: " + device.Name + " " + device.Address + " " + device.BondState.ToString ());
}
socket = device.CreateRfcommSocketToServiceRecord (UUID.FromString (TARGET_UUID));
await socket.ConnectAsync ();
if (socket != null && socket.IsConnected) CreateMessage ("Connection successful!");
else CreateMessage ("Connection failed!");
inStream = (InputStreamInvoker) socket.InputStream;
outStream = (OutputStreamInvoker) socket.OutputStream;
if (socket != null && socket.IsConnected)
{
Task t = new Task (() => Listen (inStream));
t.Start ();
}
else throw new Exception ("Socket not existing or not connected.");
}
}
现在,我们输入带有字节和痛苦的部分。我使用这种格式来传输消息:[4 bytes of uint for message length] [1 byte per character]
。重要的是您使用相同的字节到uint的转换,因为order of bytes或它的运行方式在特定于UWP的方法上有所不同。如果您的单词长度不是预期的长度(而不是大约23个类似3000000+的单词),那就是个问题。即使使用try { } catch { }
子句,读取尚不存在的字节也可能意味着异常,甚至意味着无情的崩溃。
以下方法以上述格式发送消息。如前所述,这是执行此操作的最简单方法之一,因此我不会提及如何更好地完成工作。
private async void SendMessage (string message)
{
uint messageLength = (uint) message.Length;
byte[] countBuffer = BitConverter.GetBytes (messageLength);
byte[] buffer = Encoding.UTF8.GetBytes (message);
await outStream.WriteAsync (countBuffer, 0, countBuffer.Length);
await outStream.WriteAsync (buffer, 0, buffer.Length);
}
用法:先运行方法1,再运行方法2。您也可以在方法1的最后(已连接时)执行SendMessage。
现在是关于侦听消息/响应的部分。在第一种方法中,您将看到此方法是通过任务运行的,因此它不会阻止启动它的方法。也许有Xamarin / Android特定的方法可以解决该问题,但这对我来说并不重要,所以我只是绕开了它。
private async void Listen (Stream inStream)
{
bool Listening = true;
CreateMessage ("Listening has been started.");
byte[] uintBuffer = new byte[sizeof (uint)]; // This reads the first 4 bytes which form an uint that indicates the length of the string message.
byte[] textBuffer; // This will contain the string message.
// Keep listening to the InputStream while connected.
while (Listening)
{
try
{
// This one blocks until it gets 4 bytes.
await inStream.ReadAsync (uintBuffer, 0, uintBuffer.Length);
uint readLength = BitConverter.ToUInt32 (uintBuffer, 0);
textBuffer = new byte[readLength];
// Here we know for how many bytes we are looking for.
await inStream.ReadAsync (textBuffer, 0, (int) readLength);
string s = Encoding.UTF8.GetString (textBuffer);
CreateMessage ("Received: " + s);
}
catch (Java.IO.IOException e)
{
CreateMessage ("Error: " + e.Message);
Listening = false;
break;
}
}
CreateMessage ("Listening has ended.");
}
这只是工作的一半。对于UWP服务器部分,我将只发布我的 current 代码,这更简洁,并且不需要对此进行编辑。
using System;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth.Rfcomm;
using Windows.Networking.Sockets;
using DictaNet;
using Windows.Storage.Streams;
namespace BT
{
public sealed class BluetoothConnectionHandler
{
RfcommServiceProvider provider;
bool isAdvertising = false;
StreamSocket socket;
StreamSocketListener socketListener;
DataWriter writer;
DataReader reader;
Task listeningTask;
public bool Listening { get; private set; }
// I use Actions for transmitting the output and debug output. These are custom classes I created to pack them more conveniently and to be able to just "Trigger" them without checking anything. Replace this with regular Actions and use their invoke methods.
public ActionSingle<string> MessageOutput { get; private set; } = new ActionSingle<string> ();
public ActionSingle<string> LogOutput { get; private set; } = new ActionSingle<string> ();
// These were in the samples.
const uint SERVICE_VERSION_ATTRIBUTE_ID = 0x0300;
const byte SERVICE_VERSION_ATTRIBUTE_TYPE = 0x0a; // UINT32
const uint SERVICE_VERSION = 200;
const bool DO_RESPONSE = true;
public async void StartServer ()
{
// Initialize the provider for the hosted RFCOMM service.
provider = await RfcommServiceProvider.CreateAsync (RfcommServiceId.ObexObjectPush);
// Create a listener for this service and start listening.
socketListener = new StreamSocketListener ();
socketListener.ConnectionReceived += OnConnectionReceived;
await socketListener.BindServiceNameAsync (provider.ServiceId.AsString (), SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
// Set the SDP attributes and start advertising.
InitializeServiceSdpAttributes (provider);
provider.StartAdvertising (socketListener);
isAdvertising = true;
}
public void Disconnect ()
{
Listening = false;
if (provider != null) { if (isAdvertising) provider.StopAdvertising (); provider = null; } // StopAdvertising relentlessly causes a crash if not advertising.
if (socketListener != null) { socketListener.Dispose (); socketListener = null; }
if (writer != null) { writer.DetachStream (); writer.Dispose (); writer = null; }
if (reader != null) { reader.DetachStream (); reader.Dispose (); reader = null; }
if (socket != null) { socket.Dispose (); socket = null; }
if (listeningTask != null) { listeningTask = null; }
}
public async void SendMessage (string message)
{
// There's no need to send a zero length message.
if (string.IsNullOrEmpty (message)) return;
// Make sure that the connection is still up and there is a message to send.
if (socket == null || writer == null) { LogOutput.Trigger ("Cannot send message: No clients connected."); return; } // "No clients connected, please wait for a client to connect before attempting to send a message."
uint messageLength = (uint) message.Length;
byte[] countBuffer = BitConverter.GetBytes (messageLength);
byte[] buffer = Encoding.UTF8.GetBytes (message);
LogOutput.Trigger ("Sending: " + message);
writer.WriteBytes (countBuffer);
writer.WriteBytes (buffer);
await writer.StoreAsync ();
}
private void InitializeServiceSdpAttributes (RfcommServiceProvider provider)
{
DataWriter w = new DataWriter ();
// First write the attribute type.
w.WriteByte (SERVICE_VERSION_ATTRIBUTE_TYPE);
// Then write the data.
w.WriteUInt32 (SERVICE_VERSION);
IBuffer data = w.DetachBuffer ();
provider.SdpRawAttributes.Add (SERVICE_VERSION_ATTRIBUTE_ID, data);
}
private void OnConnectionReceived (StreamSocketListener listener, StreamSocketListenerConnectionReceivedEventArgs args)
{
provider.StopAdvertising ();
isAdvertising = false;
provider = null;
listener.Dispose ();
socket = args.Socket;
writer = new DataWriter (socket.OutputStream);
reader = new DataReader (socket.InputStream);
writer.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
//StartListening ();
LogOutput.Trigger ("Connection established.");
listeningTask = new Task (() => StartListening ());
listeningTask.Start ();
// Notify connection received.
}
private async void StartListening ()
{
LogOutput.Trigger ("Starting to listen for input.");
Listening = true;
while (Listening)
{
try
{
// Based on the protocol we've defined, the first uint is the size of the message. [UInt (4)] + [Message (1*n)] - The UInt describes the length of the message.
uint readLength = await reader.LoadAsync (sizeof (uint));
// Check if the size of the data is expected (otherwise the remote has already terminated the connection).
if (!Listening) break;
if (readLength < sizeof (uint))
{
Listening = false;
Disconnect ();
LogOutput.Trigger ("The connection has been terminated.");
break;
}
uint messageLength = reader.RReadUint ();
LogOutput.Trigger ("messageLength: " + messageLength.ToString ());
// Load the rest of the message since you already know the length of the data expected.
readLength = await reader.LoadAsync (messageLength);
// Check if the size of the data is expected (otherwise the remote has already terminated the connection).
if (!Listening) break;
if (readLength < messageLength)
{
Listening = false;
Disconnect ();
LogOutput.Trigger ("The connection has been terminated.");
break;
}
string message = reader.ReadString (messageLength);
MessageOutput.Trigger ("Received message: " + message);
if (DO_RESPONSE) SendMessage ("abcdefghij");
}
catch (Exception e)
{
// If this is an unknown status it means that the error is fatal and retry will likely fail.
if (SocketError.GetStatus (e.HResult) == SocketErrorStatus.Unknown)
{
Listening = false;
Disconnect ();
LogOutput.Trigger ("Fatal unknown error occurred.");
break;
}
}
}
LogOutput.Trigger ("Stopped to listen for input.");
}
}
}
用法如下:
这应该包含执行我要求的所有操作...在hintsight中,我发现不可能有简单的答案。从此以后,所有内容都会得到改进,因为这是UWP与Xamarin / Android之间进行蓝牙通信的可能最基本的方法。
如果对此有疑问,请随时在评论部分提问。