Bluetooth UWP蓝牙射频通信同步
我想构建两个简单的应用程序,使用蓝牙RFCOMM相互通信 但是,当我运行客户端应用程序时,它找不到任何具有Bluetooth UWP蓝牙射频通信同步,bluetooth,uwp,rfcomm,windowsiot,Bluetooth,Uwp,Rfcomm,Windowsiot,我想构建两个简单的应用程序,使用蓝牙RFCOMM相互通信 但是,当我运行客户端应用程序时,它找不到任何具有\u devices=wait DeviceInformation.findalsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.ObexObjectPush))的设备; _设备集合为空 根据微软文档中的例子,我成功地写了这样的东西 应用程序接收消息(服务器)-部署在Raspberry PI 3上 namespace Ra
\u devices=wait DeviceInformation.findalsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.ObexObjectPush))的设备;
_设备集合为空
根据微软文档中的例子,我成功地写了这样的东西
应用程序接收消息(服务器)-部署在Raspberry PI 3上
namespace RaspRFCOMM
{
public sealed partial class MainPage : Page
{
private RfcommServiceProvider _provider;
public MainPage()
{
this.InitializeComponent();
this.Initialize();
}
private async void Initialize()
{
msgStatus.Text = "Inicjalizacja...";
// Initialize the provider for the hosted RFCOMM service
_provider = await RfcommServiceProvider.CreateAsync(RfcommServiceId.ObexObjectPush);
// Create a listener for this service and start listening
StreamSocketListener listener = new StreamSocketListener();
listener.ConnectionReceived += OnConnectionReceived;
await listener.BindServiceNameAsync(
_provider.ServiceId.AsString(),
SocketProtectionLevel
.BluetoothEncryptionAllowNullAuthentication);
// Set the SDP attributes and start advertising
InitializeServiceSdpAttributes(_provider);
_provider.StartAdvertising(listener, true);
}
const uint SERVICE_VERSION_ATTRIBUTE_ID = 0x0300;
const byte SERVICE_VERSION_ATTRIBUTE_TYPE = 0x0A; // UINT32
const uint SERVICE_VERSION = 200;
private void InitializeServiceSdpAttributes(RfcommServiceProvider provider)
{
DataWriter writer = new DataWriter();
// First write the attribute type
writer.WriteByte(SERVICE_VERSION_ATTRIBUTE_TYPE);
// Then write the data
writer.WriteUInt32(SERVICE_VERSION);
IBuffer data = writer.DetachBuffer();
provider.SdpRawAttributes.Add(SERVICE_VERSION_ATTRIBUTE_ID, data);
}
private async void OnConnectionReceived(
StreamSocketListener listener,
StreamSocketListenerConnectionReceivedEventArgs args)
{
msgStatus.Text = "Odczytuje...";
_provider.StopAdvertising();
listener.Dispose();
StreamSocket _socket = args.Socket;
StreamReader reader = new StreamReader(_socket.InputStream.AsStreamForRead());
string response = await reader.ReadLineAsync();
msgStatus.Text = "Odczytałem...";
textboxMsg.Text = response + "To odczytalem";
}
}
}
发送消息:
namespace WinRFCOMM
{
public sealed partial class MainPage : Page
{
private RfcommDeviceService _service;
private StreamSocket _socket;
private DeviceInformationCollection _devices;
private StreamWriter _writer;
public MainPage()
{
this.InitializeComponent();
this.Initialize();
}
private async void Initialize()
{
msgStatus.Text = "Inicjalizacja...";
_devices = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.ObexObjectPush));
this.PopulateDevicesListview(_devices);
msgStatus.Text = "Oczekiwanie na wybór...";
}
private void PopulateDevicesListview(DeviceInformationCollection devices)
{
foreach (DeviceInformation di in devices)
{
String deviceInfo = di.Name + " - " + di.Id;
lvDevices.Items.Add(deviceInfo);
}
}
private void btnConnect_Click(object sender, RoutedEventArgs e)
{
var selected = lvDevices.SelectedIndex;
if (selected != -1)
{
ConnectToRFC(_devices[selected]);
}
}
private async void ConnectToRFC(DeviceInformation selectedDevice)
{
_service = await RfcommDeviceService.FromIdAsync(selectedDevice.Id);
_socket = new StreamSocket();
await _socket.ConnectAsync(
_service.ConnectionHostName,
_service.ConnectionServiceName,
SocketProtectionLevel
.BluetoothEncryptionAllowNullAuthentication);
msgStatus.Text = "Połączono...";
_writer = new StreamWriter(_socket.OutputStream.AsStreamForWrite());
_writer.WriteLineAsync("Test");
}
}
}
两者都有如下清单文件设置:
<Capabilities>
<Capability Name="internetClient" />
<Capability Name="internetClientServer" />
<Capability Name="privateNetworkClientServer" />
<DeviceCapability Name="bluetooth" />
</Capabilities>
我非常感谢任何帮助,因为我已经被困了将近2天。问题不在代码中 在运行此RFCOMM示例之前,首先需要对两个设备进行配对。因为 使用RfcommDeviceService.GetDeviceSelect*函数提供帮助 生成可用于枚举配对设备的AQS查询 所需服务的实例
参考:问题不在代码中 在运行此RFCOMM示例之前,首先需要对两个设备进行配对。因为 使用RfcommDeviceService.GetDeviceSelect*函数提供帮助 生成可用于枚举配对设备的AQS查询 所需服务的实例 参考:我找到了一篇论坛帖子,这篇帖子让我度过了难关 我的问题是试图使用我在配对中发现的设备信息作为RFCOMM的连接点 对于rfcomm,您将要求您的AppManifest如下所示:
<DeviceCapability Name="bluetooth.rfcomm">
<Device Id="any">
<Function Type ="name:serialPort"/>
</Device>
</DeviceCapability>
我找到了一篇论坛帖子,这篇帖子让我度过了难关
我的问题是试图使用我在配对中发现的设备信息作为RFCOMM的连接点
对于rfcomm,您将要求您的AppManifest如下所示:
<DeviceCapability Name="bluetooth.rfcomm">
<Device Id="any">
<Function Type ="name:serialPort"/>
</Device>
</DeviceCapability>
不要只使用“蓝牙”这个名称。谢谢您的回答,但是配对设备还不能解决问题。使用RfcommServiceId.ObexObjectPush选择器列出设备仍然不起作用。它显示带有BluetoothDevice.GetDeviceSelector()的raspberry设备,但随后在RfcommDeviceService.FromIdAsync()上引发“找不到元素”异常。感谢您的回答,但是配对设备还不能解决问题。使用RfcommServiceId.ObexObjectPush选择器列出设备仍然不起作用。它显示带有BluetoothDevice.GetDeviceSelector()的raspberry设备,但随后它在RfcommDeviceService.FromIdAsync()上引发了“找不到元素”异常。不知怎的,它开始工作了,但我不确定为什么。我会让你知道我什么时候能解决这个问题。没有配对它能工作吗?不知怎么的,它开始工作了,但我不知道为什么。我会让你知道我什么时候能解决这个问题。没有配对它能工作吗?