我正在开发一个项目,我的PC充当蓝牙服务器,我的Android手机充当蓝牙客户端
我在我的电脑上使用winsock,我使用python编写了android应用程序,并在pyjnius的帮助下使用了android java apis
应用程序在connect()方法中终止
logcat文件说
发生JVM异常:读取失败,套接字可能关闭或超时,读取ret:-1
蓝牙服务器的代码如下....首先我查找远程设备,然后执行套接字操作
#include "stdafx.h"
#include<iostream>
#include<winsock2.h>
#include<ws2bth.h>
#include<bluetoothapis.h>
#include<stdlib.h>
using namespace std;
#define SUCCESS 0
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "irprops.lib")
int main()
{
CHAR buf[5000];
// LPTSTR s;
WCHAR addressAsString[1000];
BTH_ADDR b;
WSADATA data;
int result, i = 0, channel = 30;
result = WSAStartup(MAKEWORD(2, 2), &data);
if (result != SUCCESS)
{
cout << "error occured while initialising winsock...";
Sleep(1000);
exit(result);
}
cout << "winsock initialisation successful\n";
WSAQUERYSET queryset;
memset(&queryset, 0, sizeof(WSAQUERYSET));
queryset.dwSize = sizeof(WSAQUERYSET);
queryset.dwNameSpace = NS_BTH;
HANDLE hLookup;
result = WSALookupServiceBegin(&queryset, LUP_CONTAINERS, &hLookup);
if (result != SUCCESS)
{
cout << "error in initialising look up service\n";
Sleep(1000);
exit(result);
}
cout << "initialising lookup service successful\n";
BYTE buffer[4096];
memset(buffer, 0, sizeof(buffer));
DWORD bufferLength = sizeof(buffer);
LPWSAQUERYSET pResults = (LPWSAQUERYSET)buf;
BTH_ADDR my_add;
while (!WSALookupServiceNext(hLookup, LUP_RETURN_NAME | LUP_CONTAINERS | LUP_RETURN_ADDR | LUP_FLUSHCACHE | LUP_RETURN_TYPE | LUP_RETURN_BLOB | LUP_RES_SERVICE, &bufferLength, pResults))
{
LPTSTR name = pResults->lpszServiceInstanceName;
BTH_ADDR b = ((SOCKADDR_BTH *)pResults->lpcsaBuffer->RemoteAddr.lpSockaddr)->btAddr;
wcout << hex << name << "\t" << b << endl;
Sleep(1000);
if (!lstrcmp(name, L"Anjali Mohanty (SM-G36"))
{
my_add = b;
}
}
WSALookupServiceEnd(hLookup);
SOCKET server_socket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
cout << "server socket created" << endl;
//SOCKET ADDRESS INFORMATION
SOCKADDR_BTH addr;
addr.addressFamily = AF_BTH;
addr.btAddr = 0;
addr.serviceClassId = GUID_NULL;
addr.port = BT_PORT_ANY;
cout << "Socket address information is stored" << endl;
bind(server_socket, (SOCKADDR*)&addr, sizeof(SOCKADDR_BTH));
cout << "server socket binding complete"<<endl;
listen(server_socket, SOMAXCONN);
GUID my_guid = { 0x00001101, 0x0000, 0x1000, 0x8000, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB };
//SET WSA SERVICE
WSAQUERYSET wsaqueryset = { 0 };
wsaqueryset.dwSize = sizeof(WSAQUERYSET);
wsaqueryset.dwNameSpace = NS_BTH;
wsaqueryset.lpszServiceInstanceName = L"BT Service"; // Can be anything
wsaqueryset.lpszComment = L"bt"; //Can be anything
wsaqueryset.lpServiceClassId = &my_guid;
wsaqueryset.dwNumberOfCsAddrs = 1;
cout << "WSA service is set" << endl;
//CONTAINS INFORMATION ABOUT BLUETOOTH BROADCAST SERVICE
CSADDR_INFO addrInfo = { 0 };
addrInfo.LocalAddr.lpSockaddr = (SOCKADDR*)&addr;
addrInfo.LocalAddr.iSockaddrLength = sizeof(SOCKADDR_BTH);
addrInfo.iSocketType = SOCK_STREAM;
addrInfo.iProtocol = BTHPROTO_RFCOMM;
cout << "Information about bluetooth broadcast service is stored" << endl;
//INITIALISE BLUETOOTH SERVICE
wsaqueryset.lpcsaBuffer = &addrInfo;
WSASetService(&wsaqueryset, RNRSERVICE_REGISTER, 0);
cout << "Bluetooth service is initialised" << endl;
cout << "Waiting for client" << endl;
SOCKET accept_socket = accept(server_socket, NULL, 0);
cout << "connection accepted" << endl;
}
python android app中的方法包含
mac_add="C0:CB:38:DD:E1:69"
str_add=String(mac_add)
bd=self.bta.getRemoteDevice(str_add)
self.ids.l2.text="Bluetooth device created"
uuid=UUID.fromString('00001101-0000-1000-8000-00805F9B34FB')
bs=bd.createRfcommSocketToServiceRecord(uuid)
self.ids.l3.text="Bluetooth socket created"
bs.connect()
提前感谢任何帮助