接收文本数据从远程安卓蓝牙到Windows xp PC
Recieving text data from remote android bluetooth to Windows xp PC
我想从远程安卓蓝牙接收文本数据。我写了简单的winApi服务器,但它不响应远程安卓发送。连接很好,并且使用带有蓝牙的控制面板管理器进行了响应(配对)。当发送和接收控制面板管理器工作正常时。但是这一次,我想使用一些c ++ winapi进行测试。这是我到目前为止的代码。它改编自某个代码项目站点:)
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <string>
#include <tchar.h>
#include <WinSock2.h>
#include <ws2bth.h>
#include <BluetoothAPIs.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
WORD wVersionRequested = 0x202;
WSADATA m_data;
if (0 == WSAStartup(wVersionRequested, &m_data))
{
SOCKET s = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
const DWORD lastError = GetLastError();
if (s == INVALID_SOCKET)
{
ostringstream stream;
stream << lastError;
string data= stream.str();
printf("Failed to get bluetooth socket! %sn", data.c_str());
return 1;
}
WSAPROTOCOL_INFO protocolInfo;
int protocolInfoSize = sizeof(protocolInfo);
if (0 != getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFO,
(char*)&protocolInfo, &protocolInfoSize))
{
return 1;
}
SOCKADDR_BTH address;
address.addressFamily = AF_BTH;
address.btAddr = 0;
address.serviceClassId = GUID_NULL;
address.port = BT_PORT_ANY;
sockaddr *pAddr = (sockaddr*)&address;
if (0 != bind(s, pAddr, sizeof(SOCKADDR_BTH)))
{
ostringstream stream;
stream << GetLastError();
string data= stream.str();
printf("%sn", data.c_str() );
}
else
{
printf("nBinding Successful....n");
int length = sizeof(SOCKADDR_BTH) ;
getsockname(s,(sockaddr*)&address,&length);
wprintf (L"Local Bluetooth device is %04x%08x nServer channel = %dn",
GET_NAP(address.btAddr), GET_SAP(address.btAddr), address.port);
}
int size = sizeof(SOCKADDR_BTH);
if (0 != getsockname(s, pAddr, &size))
{
ostringstream stream;
stream << GetLastError();
string data= stream.str();
printf("%sn", data.c_str());
}
if (0 != listen(s, 10))
{
ostringstream stream;
stream << GetLastError();
string data= stream.str();
printf("%sn", data.c_str());
}
WSAQUERYSET service;
memset(&service, 0, sizeof(service));
service.dwSize = sizeof(service);
// service.lpszServiceInstanceName = reinterpret_cast<LPWSTR>(_T("Accelerometer Data..."));
//service.lpszServiceInstanceName = ;
// service.lpszComment = reinterpret_cast<LPWSTR>(_T("Pushing data to PC"));
GUID serviceID = OBEXFileTransferServiceClass_UUID;
service.lpServiceClassId = &serviceID;
service.dwNumberOfCsAddrs = 1;
service.dwNameSpace = NS_BTH;
CSADDR_INFO csAddr;
memset(&csAddr, 0, sizeof(csAddr));
csAddr.LocalAddr.iSockaddrLength = sizeof(SOCKADDR_BTH);
csAddr.LocalAddr.lpSockaddr = pAddr;
csAddr.iSocketType = SOCK_STREAM;
csAddr.iProtocol = BTHPROTO_RFCOMM;
service.lpcsaBuffer = &csAddr;
if (0 != WSASetService(&service, RNRSERVICE_REGISTER, 0))
{
printf("Service registration failed....");
ostringstream stream;
stream << GetLastError();
string data= stream.str();
printf("%dn", data.c_str());
}
else
{
printf("nService registration Successful....n");
}
HANDLE hRadio ;
BLUETOOTH_FIND_RADIO_PARAMS btfrp = { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) };
HBLUETOOTH_RADIO_FIND hFind = BluetoothFindFirstRadio(&btfrp, &hRadio);
if ((hFind != NULL)&& (hRadio !=NULL))
{
if (BluetoothEnableDiscovery(hRadio,TRUE)){
printf("BluetoothEnableDiscovery() is working!n");
}
if (BluetoothEnableIncomingConnections(hRadio,TRUE)){
printf("BluetoothEnableIncomingConnections() is working!n");
}
if (BluetoothIsConnectable(hRadio)){
printf("BluetoothIsConnectable() is working!n");
}
}
printf("nBefore accept.........");
SOCKADDR_BTH sab2;
int ilen = sizeof(sab2);
SOCKET s2 = accept (s,(sockaddr*)&sab2, &ilen);
if (s2 == INVALID_SOCKET)
{
wprintf (L"Socket bind, error %dn", WSAGetLastError ());
}
wprintf (L"nConnection came from %04x%08x to channel %dn",
GET_NAP(sab2.btAddr), GET_SAP(sab2.btAddr), sab2.port);
wprintf (L"nAfter Acceptn");
char buffer[1024] = {0};
memset(buffer, 0, sizeof(buffer));
int r = recv(s2,(char*)buffer, sizeof(buffer), 0);
printf("%sn",buffer);
closesocket(s2);
if (0 != WSASetService(&service, RNRSERVICE_DELETE, 0))
{
ostringstream stream;
stream << GetLastError();
string data= stream.str();
printf("%sn", data.c_str());
}
closesocket(s);
WSACleanup();
return 0;
}
}
顺便说一句,我使用微软BT设备。
只是为了确定:你说你在WinXP上...好的,但是带有Microsoft BT堆栈和适当的加密狗的WinXP SP3?还是另一个XP版本和另一个BT堆栈(如Widcomm或BlueSoleil...)?在后一种情况下,BT 套接字 API 将不起作用...
否则,关于代码没什么好说的,除了缺少蓝牙查找无线电关闭...
相关文章:
- std::async from std::async in windows xp
- 如何在Visual Studio 2017中编译Windows XP的代码
- 如何构建使用Visual Studio 2017的Windows XP的Winsock2(WS2_32.LIB)应用程序
- WinHttpSendRequest and HTTPS on Windows XP
- 功能本地静态为Windows XP生成故障代码
- SHOpenWithDialog analog for windows XP?
- 如何检查操作系统是否至少是Windows XP SP3
- 使用Visual Studio 2012针对Windows XP构建Boost 1.52库
- 使用 Build Tools 2015 (不带 Visual Studio) 面向 Windows XP
- 有任何替代SHGetKnownFolderPath的Windows XP用户令牌的方案吗
- 当Windows XP(32位,SP3)播放声音时,随机应用程序暂停半秒钟
- 函数从windows XP中的数据包中检索标头目标地址
- GetConsoleScreenBufferInfoEx equivalent for windows XP?
- 如何跟踪Windows 7是否在C ++中以编程方式进入睡眠或休眠状态,就像在Windows XP中所做的那样
- 如何从Windows XP下的程序中知道或更改Windows事件日志的大小
- 带Realtek Alc888的活动耳机插孔(Windows XP)
- 如何使用Visual Studio 2012编译Windows XP
- Visual C 2012 Express Compied Application在Windows XP上不起作用
- 为 Windows XP 编译的应用程序无法在 Windows 7 上运行
- 检测本地化的 Windows XP 安装的系统语言