UWP Bluetooth ConnectAsync错误到Raspberry Pi。找不到元素

时间:2017-07-03 08:05:20

标签: python bluetooth uwp raspberry-pi windows-applications

我正在制作一个使用RFCOMM与Raspberry Pi通信的Windows UWP应用程序。我的Windows笔记本电脑和Raspberry Pi都已连接并配对(手动)。我知道Raspberry Pi的蓝牙地址及其使用的频道,这些值被硬编码到ConnectAsync(...)的参数中。

当调用ConnectAsync(...)时,Raspberry Pi会接收连接,并从(' Windows蓝牙地址',1)打印行"接受的连接。但是,异常"类型&System;异常' System.Exception'发生在mscorlib.ni.dll但未在用户代码中处理。附加信息:找不到元素。 (HRESULT异常:0x80070490)"发生在调用ConnectAsync(...)的地方,并且消息永远不会传递给Raspberry Pi。

Windows代码(作为客户端,尝试向Raspberry Pi发送消息):

StreamSocket socket = new StreamSocket();
await socket.ConnectAsync(new HostName(bluetoothAddress), "1"); // the format of bluetoothAddress is 'AB:CD:AB:CD:AB:CD'

Stream streamOut = socket.OutputStream.AsStreamForWrite();
StreamWriter writer = new StreamWriter(streamOut);
await writer.WriteLineAsync(message);
await writer.FlushAsync();

Raspberry Pi(作为Python服务器):

import bluetooth

server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )

port = 1
server_sock.bind(("",port))
server_sock.listen(1)

client_sock,address = server_sock.accept()
print("Accepted connection from ",address)

data = client_sock.recv(1024)
print("received [%s]" % data)

当我更换" 1"在ConnectAsync(...)与bluetoothAddress,一个'值不在预期的范围内'异常出现。

1 个答案:

答案 0 :(得分:0)

方法socket.ConnectAsync()的第二个参数“remoteServiceName”应为RfcommDeviceService.ConnectionServiceName

有关详细信息,请参阅Bluetooth RFCOMM chat sample