UWP Bluetooth ConnectAsync error to Raspberry Pi. Element not found

0

I'm making a Windows UWP app that uses RFCOMM to communicate to a Raspberry Pi. Both my Windows laptop and Raspberry Pi are already connected and paired (manually). I know the bluetooth address of the Raspberry Pi and the channel it's using and these values are hardcoded into the parameters for ConnectAsync(...).

When ConnectAsync(...) is called, the Raspberry Pi does receive a connection and it prints the line "accepted connection from ('Windows bluetooth address', 1). However, the exception "An exception of type 'System.Exception' occurred in mscorlib.ni.dll but was not handled in user code. Additional information: Element not found. (Exception from HRESULT: 0x80070490)" happens at where ConnectAsync(...) is called, and the message is never delivered to the Raspberry Pi.

Windows Code (as the client, trying to send message to the 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 (as Python Server):

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)

When I replace the "1" in ConnectAsync(...) with bluetoothAddress, a 'Value does not fall within the expected range' exception appears.

python
bluetooth
uwp
raspberry-pi
windows-applications
asked on Stack Overflow Jul 3, 2017 by Lenny

1 Answer

0

The second parameter "remoteServiceName" of the method socket.ConnectAsync() should be RfcommDeviceService.ConnectionServiceName.

For more information you can reference Bluetooth RFCOMM chat sample.

answered on Stack Overflow Jul 4, 2017 by Rita Han

User contributions licensed under CC BY-SA 3.0