2017-07-03 61 views
0

我正在製作一個使用RFCOMM與Raspberry Pi進行通信的Windows UWP應用程序。我的Windows筆記本電腦和Raspberry Pi已經連接並配對(手動)。我知道Raspberry Pi的藍牙地址和它使用的通道,並且這些值被硬編碼爲ConnectAsync(...)的參數。UWP藍牙ConnectAsync錯誤到Raspberry Pi。元素未找到

當ConnectAsync(...)被調用時,Raspberry Pi確實收到一個連接,並打印出「來自('Windows bluetooth address',1)的接受的連接,但是, System.Exception'發生在mscorlib.ni.dll中,但未在用戶代碼中處理。附加信息:找不到元素。 (來自HRESULT的異常:0x80070490)「發生在ConnectAsync(...)被調用的地方,並且該消息從未被傳送到樹莓派

Windows代碼(作爲客戶端,試圖發送消息到樹莓派):

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(); 

樹莓派(像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,α「值不落在預期範圍「異常出現

回答

0

方法socket.ConnectAsync()的第二個參數「remoteServiceName」應爲RfcommDeviceService.ConnectionServiceName

欲瞭解更多信息,你可以參考Bluetooth RFCOMM chat sample