问题
I am trying to make an application that transfers Strings of data from my Android phone to my Laptop running Windows 7 (and Vice-Versa) over Bluetooth.
I figured that I would open up a Server-Socket on the Windows Machine and program the Android app to start a Client-Socket. But being aware that Android SDP(Service Discovery Protocol) and connects By a UUID, created a point where in I am confused.
Confused on how I should start up a server socket on the Server Side Using the Windows API. MY Question is whether android will detect the server socket if I merely start a Bluetooth socket with
SOCKET sock_s, sock_c;
SOCKADDR_BTH add1,add2;
sock_s =socket( AF_BTH,SOCK_STREAM,BTHPROTO_RFCOMM );
if ( sock_s == INVALID_SOCKET )
return -2;
//Clearing the SOCKADDR_BTH variable
memset(&add1,0,sizeof(add1));
add1.addressFamily=AF_BTH;
add1.serviceClassId=my_Guid;
Or is there another way to create a server socket that follows the SDP protocol on the MICROSOFT stack?
回答1:
Do you have to use native code on the Windows box? If not use my library (32feet.NET) its make such things easy e.g. Bluetooth Server-side:
Class MyConsts
Shared ReadOnly MyServiceUuid As Guid _
= New Guid("{00112233-4455-6677-8899-aabbccddeeff}")
End Class
...
Dim lsnr As New BluetoothListener(MyConsts.MyServiceUuid)
lsnr.Start()
' Now accept new connections, perhaps using the thread pool to handle each
Dim conn As New BluetoothClient = lsnr.AcceptBluetoothClient()
Dim peerStream As Stream = conn.GetStream()
...
You could even 32feet.NET use it from C++ with C+++/CLR.
Or IIRC there is a simple way to get Windows WSASetService to add a basic SDP record for your server socket (if you don't have a custom record to add). Or get the RFCOMM port number from the Windows socket (getsockname) and tell the Android app to connect directly to that port number -- assuming it can do that...
来源:https://stackoverflow.com/questions/6353939/bluetooth-android-to-windows-7