通过蓝牙代码示例C ++将文件从PC传输到其他设备 [英] Transfer file from PC to other device via Bluetooth code example C++

查看:1630
本文介绍了通过蓝牙代码示例C ++将文件从PC传输到其他设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Stackoverflow用户!!!



$ b
  • 我在寻找一些代码示例,显示如何找到连接到我的计算机的蓝牙设备。

  • 寻找一些代码示例,显示如何将信息从我的计算机传输到移动设备。

  • 试图在C ++上开发应用程序,必须使用 蓝牙 设备。当我通过互联网研究时,我发现一些关于蓝牙的信息,我发现一些代码示例巫婆目前如何使用蓝牙设备。例如 这里 我发现代码巫婆介绍如何使用find所有蓝牙设备范围内的所有蓝牙设备:

      #define WIN32_LEAN_AND_MEAN 
    #include< windows.h>
    #include< stdio.h>
    #include< stdlib.h>
    #include< winsock2.h>
    #include< bthdef.h>
    #include< BluetoothAPIs.h>

    #pragma comment(lib,Irprops.lib)

    // BLUETOOTH_FIND_RADIO_PARAMS结构便于
    //枚举已安装的蓝牙无线电。
    BLUETOOTH_FIND_RADIO_PARAMS m_bt_find_radio = {
    sizeof(BLUETOOTH_FIND_RADIO_PARAMS)
    }

    // BLUETOOTH_RADIO_INFO结构包含关于蓝牙无线电的信息
    //。
    BLUETOOTH_RADIO_INFO m_bt_info = {
    sizeof(BLUETOOTH_RADIO_INFO),
    0,
    };

    // BLUETOOTH_DEVICE_SEARCH_PARAMS结构指定
    //搜索蓝牙设备搜索条件。
    BLUETOOTH_DEVICE_SEARCH_PARAMS m_search_params = {
    sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS),
    1,
    0,
    1,
    1,
    1,$ b b 15,
    NULL
    };

    // BLUETOOTH_DEVICE_INFO结构提供关于蓝牙设备的信息
    //。
    BLUETOOTH_DEVICE_INFO m_device_info = {
    sizeof(BLUETOOTH_DEVICE_INFO),
    0,
    };

    HANDLE m_radio = NULL;
    HBLUETOOTH_RADIO_FIND m_bt = NULL;
    HBLUETOOTH_DEVICE_FIND m_bt_dev = NULL;

    int wmain(int argc,wchar_t ** args){
    while(true){
    // BluetoothFindFirstRadio函数开始
    //本地蓝牙枚举收音机。
    m_bt = BluetoothFindFirstRadio(& m_bt_find_radio,& m_radio);

    int m_radio_id = 0;
    do {
    m_radio_id ++;

    // BluetoothGetRadioInfo函数获取关于蓝牙无线电的信息
    //。
    BluetoothGetRadioInfo(m_radio,& m_bt_info);

    wprintf(LRadio%d:\r\\\
    ,m_radio_id);
    wprintf(L\tName:%s\r\\\
    ,m_bt_info.szName);
    wprintf(L\tAddress:%02x:%02x:%02x:%02x:%02x:%02x\r\\\
    ,m_bt_info.address.rgBytes [1],m_bt_info.address。 rgBytes [0],m_bt_info.address.rgBytes [2],m_bt_info.address.rgBytes [3],m_bt_info.address.rgBytes [4],m_bt_info.address.rgBytes [5]);
    wprintf(L\tClass:0x%08x\r\\\
    ,m_bt_info.ulClassofDevice);
    wprintf(L\tManufacturer:0x%04x\r\\\
    ,m_bt_info.manufacturer);

    m_search_params.hRadio = m_radio;

    :: ZeroMemory(& m_device_info,sizeof(BLUETOOTH_DEVICE_INFO));
    m_device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);

    m_bt_dev = BluetoothFindFirstDevice(& m_search_params,& m_device_info);

    int m_device_id = 0;
    do {
    m_device_id ++;

    wprintf(L\tDevice%d:\r\\\
    ,m_device_id);
    wprintf(L\t\tName:%s\r\\\
    ,m_device_info.szName);
    wprintf(L\t\tAddress:%02x:%02x:%02x:%02x:%02x:%02x\r\\\
    ,m_device_info.Address.rgBytes [1],m_device_info .Address.rgBytes [0],m_device_info.Address.rgBytes [2],m_device_info.Address.rgBytes [3],m_device_info.Address.rgBytes [4],m_device_info.Address.rgBytes [5]
    wprintf(L\t\tClass:0x%08x\r\\\
    ,m_device_info.ulClassofDevice);
    wprintf(L\t\tConnected:%s\r\\\
    ,m_device_info.fConnected?Ltrue:Lfalse);
    wprintf(L\t\tAuthenticated:%s\r\\\
    ,m_device_info.fAuthenticated?Ltrue:Lfalse);
    wprintf(L\t\tRemembered:%s\r\\\
    ,m_device_info.fRemembered?Ltrue:Lfalse);

    } while(BluetoothFindNextDevice(m_bt_dev,& m_device_info));

    BluetoothFindDeviceClose(m_bt_dev);

    } while(BluetoothFindNextRadio(& m_bt_find_radio,& m_radio));

    BluetoothFindRadioClose(m_bt);

    Sleep(10000);
    }

    return 0;
    }

    然后我创建项目并在我的笔记本上运行,打开蓝牙没有结果出现。如果应用程序找不到任何蓝牙设备。在我继续研究之后,我发现了其他文章,名为 查找蓝牙设备和服务Windows XP中的C ++

      #include< winsock2.h> 
    #include< ws2bth.h>
    #include< BluetoothAPIs.h>
    #include< stdlib.h>
    #include< stdio.h>
    #include< iostream>

    #pragma comment(lib,ws2_32.lib)
    #pragma comment(lib,irprops.lib)

    using namespace std;

    int main(){

    //初始化winsock

    WSADATA数据;
    int result;

    result = WSAStartup(MAKEWORD(2,2),& data); //初始化winsock

    if(result!= 0){
    cout ;& 初始化winsock时出错,关闭....;
    exit(result);
    }

    //初始化查询设备
    WSAQUERYSET查询集;

    memset(& queryset,0,sizeof(WSAQUERYSET));
    queryset.dwSize = sizeof(WSAQUERYSET);
    queryset.dwNameSpace = NS_BTH;

    HANDLE hLookup;

    result = WSALookupServiceBegin(& queryset,LUP_CONTAINERS,& hLookup);

    if(result!= 0){
    cout< 初始化查找设备时出错,关闭....;
    Sleep(100000);
    exit(result);
    }

    //初始化成功,开始查找设备

    BYTE buffer [4096];
    memset(buffer,0,sizeof(buffer));
    DWORD bufferLength = sizeof(buffer);
    WSAQUERYSET * pResults =(WSAQUERYSET *)& buffer;

    while(result == 0){
    result = WSALookupServiceNext(hLookup,LUP_RETURN_NAME | LUP_CONTAINERS | LUP_RETURN_ADDR | LUP_FLUSHCACHE | LUP_RETURN_TYPE | LUP_RETURN_BLOB | LUP_RES_SERVICE,& bufferLength,pResults);

    if(result == 0){//找到一个设备
    //打印设备名称
    LPTSTR s = pResults-> lpszServiceInstanceName;
    wcout<< s < found。quering for services \\\
    ;
    //初始化设备服务
    WSAQUERYSET queryset2;
    memset(& queryset2,0,sizeof(queryset2));
    queryset2.dwSize = sizeof(queryset2);
    queryset2.dwNameSpace = NS_BTH;
    queryset2.dwNumberOfCsAddrs = 0;
    CSADDR_INFO * addr =(CSADDR_INFO *)pResults-> lpcsaBuffer;
    WCHAR addressAsString [1000];
    DWORD addressSize = sizeof(addressAsString);
    WSAAddressToString(addr-> RemoteAddr.lpSockaddr,addr-> RemoteAddr.iSockaddrLength,NULL,addressAsString,& addressSize);
    queryset2.lpszContext = addressAsString;
    GUID protocol = L2CAP_PROTOCOL_UUID;
    queryset2.lpServiceClassId =& protocol;
    HANDLE hLookup2;
    int result2 = WSALookupServiceBegin(& queryset2,LUP_FLUSHCACHE | LUP_RETURN_NAME | LUP_RETURN_TYPE | LUP_RETURN_ADDR | LUP_RETURN_BLOB | LUP_RETURN_COMMENT,& hLookup2);

    if(result2!= 0){
    cout< 初始化服务查询时出错;
    Sleep(100000);
    exit(result);
    }

    //开始查询设备服务
    while(result2 == 0){
    BYTE buffer2 [4096];
    memset(buffer2,0,sizeof(buffer2));
    DWORD bufferLength2 = sizeof(buffer2);
    WSAQUERYSET * pResults2 =(WSAQUERYSET *)& buffer2;
    result2 = WSALookupServiceNext(hLookup2,LUP_FLUSHCACHE | LUP_RETURN_NAME | LUP_RETURN_TYPE | LUP_RETURN_ADDR | LUP_RETURN_BLOB | LUP_RETURN_COMMENT,& bufferLength2,pResults2);
    if(result2 == 0)
    wcout<< Service found:< pResults2-> lpszServiceInstanceName<<\\\
    ;
    Sleep(100000);
    }
    }
    }

    Sleep(100000);

    return 0;

    }



    当我编译这一个并启动应用程序时出现错误信息 初始化查找设备时出现错误,正在关闭...



    如果你可以给我任何文章,代码示例或其他东西,帮助我在我的研究我会很高兴,感谢你的信息!



    最好的问候!

    解决方案

    要检查此链接



    它可能对你有用...:)


    Hi Stackoverflow Users !!!

    Questions:

    1. I am looking for some code examples which show how can I find Bluetooth devices that are connected to my computer.
    2. I am looking for some code examples which show how can I transfer information from my computer to a mobile device.

    Now I am trying to develop application on C++ which must work with Bluetooth devices. While I was researching through internet I found some information about Bluetooth and I found some code examples witch present how can work with Bluetooth devices. For example here I found code witch presents how can use find all bluetooth devices within range of all bluetooth radios:

    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <winsock2.h>
    #include <bthdef.h>
    #include <BluetoothAPIs.h>
    
    #pragma comment(lib, "Irprops.lib")
    
    // The BLUETOOTH_FIND_RADIO_PARAMS structure facilitates 
    // enumerating installed Bluetooth radios.
    BLUETOOTH_FIND_RADIO_PARAMS m_bt_find_radio = {
      sizeof(BLUETOOTH_FIND_RADIO_PARAMS)
    };
    
    // The BLUETOOTH_RADIO_INFO structure contains information
    // about a Bluetooth radio.
    BLUETOOTH_RADIO_INFO m_bt_info = {
      sizeof(BLUETOOTH_RADIO_INFO),
      0,
    };
    
    // The BLUETOOTH_DEVICE_SEARCH_PARAMS structure specifies
    // search criteria for Bluetooth device searches.
    BLUETOOTH_DEVICE_SEARCH_PARAMS m_search_params = {
      sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS),
      1,
      0,
      1,
      1,
      1,
      15,
      NULL
    };
    
    // The BLUETOOTH_DEVICE_INFO structure provides information
    // about a Bluetooth device.
    BLUETOOTH_DEVICE_INFO m_device_info = {
      sizeof(BLUETOOTH_DEVICE_INFO),
      0,
    };
    
    HANDLE m_radio = NULL;
    HBLUETOOTH_RADIO_FIND m_bt = NULL;
    HBLUETOOTH_DEVICE_FIND m_bt_dev = NULL;
    
    int wmain(int argc, wchar_t **args) {
      while(true) {
        // The BluetoothFindFirstRadio function begins the 
        // enumeration of local Bluetooth radios.
        m_bt = BluetoothFindFirstRadio(&m_bt_find_radio, &m_radio);
    
        int m_radio_id = 0;
        do {
          m_radio_id++;
    
          // The BluetoothGetRadioInfo function obtains information
          // about a Bluetooth radio.
          BluetoothGetRadioInfo(m_radio, &m_bt_info);
    
          wprintf(L"Radio %d:\r\n", m_radio_id);
          wprintf(L"\tName: %s\r\n", m_bt_info.szName);
          wprintf(L"\tAddress: %02x:%02x:%02x:%02x:%02x:%02x\r\n", m_bt_info.address.rgBytes[1], m_bt_info.address.rgBytes[0], m_bt_info.address.rgBytes[2], m_bt_info.address.rgBytes[3], m_bt_info.address.rgBytes[4], m_bt_info.address.rgBytes[5]);
          wprintf(L"\tClass: 0x%08x\r\n", m_bt_info.ulClassofDevice);
          wprintf(L"\tManufacturer: 0x%04x\r\n", m_bt_info.manufacturer);
    
          m_search_params.hRadio = m_radio;
    
          ::ZeroMemory(&m_device_info, sizeof(BLUETOOTH_DEVICE_INFO));
          m_device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
    
          m_bt_dev = BluetoothFindFirstDevice(&m_search_params, &m_device_info);
    
          int m_device_id = 0;
          do {
            m_device_id++;
    
            wprintf(L"\tDevice %d:\r\n", m_device_id);
            wprintf(L"\t\tName: %s\r\n", m_device_info.szName);
            wprintf(L"\t\tAddress: %02x:%02x:%02x:%02x:%02x:%02x\r\n", m_device_info.Address.rgBytes[1], m_device_info.Address.rgBytes[0], m_device_info.Address.rgBytes[2], m_device_info.Address.rgBytes[3], m_device_info.Address.rgBytes[4], m_device_info.Address.rgBytes[5]);
            wprintf(L"\t\tClass: 0x%08x\r\n", m_device_info.ulClassofDevice);
            wprintf(L"\t\tConnected: %s\r\n", m_device_info.fConnected ? L"true" : L"false");
            wprintf(L"\t\tAuthenticated: %s\r\n", m_device_info.fAuthenticated ? L"true" : L"false");
            wprintf(L"\t\tRemembered: %s\r\n", m_device_info.fRemembered ? L"true" : L"false");
    
          } while(BluetoothFindNextDevice(m_bt_dev, &m_device_info));
    
          BluetoothFindDeviceClose(m_bt_dev);
    
        } while(BluetoothFindNextRadio(&m_bt_find_radio, &m_radio));
    
        BluetoothFindRadioClose(m_bt);
    
        Sleep(10000);
      }
    
      return 0;
    }
    

    Then I create project and run it on my notebook with turned on Bluetooth no result appear. As if the application can't find any Bluetooth device. After I continued my research and found other article called Finding Bluetooth devices and services using C++ in Windows XP

    #include <winsock2.h>
    #include <ws2bth.h>
    #include <BluetoothAPIs.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <iostream>
    
    #pragma comment(lib, "ws2_32.lib")
    #pragma comment(lib, "irprops.lib")
    
    using namespace std;
    
    int main(){
    
        //Initialising winsock
    
        WSADATA data;
        int result;
    
        result = WSAStartup(MAKEWORD(2, 2), &data);//initializing winsock
    
        if (result!=0) {
            cout << "An error occured while initialising winsock, closing....";
            exit(result);
        }
    
        //Initialising query for device
        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!=0){
            cout << "An error occured while initialising look for devices, closing....";
            Sleep(100000);
            exit(result);
        }
    
        //Initialisation succedd, start looking for devices
    
        BYTE buffer[4096];
        memset(buffer, 0, sizeof(buffer));
        DWORD bufferLength = sizeof(buffer);
        WSAQUERYSET *pResults = (WSAQUERYSET*)&buffer;
    
            while (result==0){
                result = WSALookupServiceNext(hLookup, LUP_RETURN_NAME | LUP_CONTAINERS | LUP_RETURN_ADDR | LUP_FLUSHCACHE | LUP_RETURN_TYPE | LUP_RETURN_BLOB | LUP_RES_SERVICE,&bufferLength, pResults);
    
                if(result==0){// A device found
                    //print the name of the device
                    LPTSTR s = pResults->lpszServiceInstanceName;
                    wcout << s << "found. quering for services\n";
                    //Initialise quering the device services
                    WSAQUERYSET queryset2;
                    memset(&queryset2, 0, sizeof(queryset2));
                    queryset2.dwSize = sizeof(queryset2);
                    queryset2.dwNameSpace = NS_BTH;
                    queryset2.dwNumberOfCsAddrs = 0;
                    CSADDR_INFO * addr = (CSADDR_INFO *)pResults->lpcsaBuffer;
                    WCHAR addressAsString[1000];
                    DWORD addressSize = sizeof(addressAsString);
                    WSAAddressToString(addr->RemoteAddr.lpSockaddr,addr->RemoteAddr.iSockaddrLength,NULL,addressAsString, &addressSize);
                    queryset2.lpszContext = addressAsString;
                    GUID protocol = L2CAP_PROTOCOL_UUID;
                    queryset2.lpServiceClassId = &protocol;
                    HANDLE hLookup2;
                    int result2 = WSALookupServiceBegin(&queryset2, LUP_FLUSHCACHE |LUP_RETURN_NAME | LUP_RETURN_TYPE | LUP_RETURN_ADDR | LUP_RETURN_BLOB | LUP_RETURN_COMMENT, &hLookup2);
    
                    if (result2 !=0){
                        cout << "An error occured while initializing query for services";
                        Sleep(100000);
                        exit(result);
                    }
    
                    //Start quering for device services
                    while(result2 ==0){
                        BYTE buffer2[4096];
                        memset(buffer2, 0, sizeof(buffer2));
                        DWORD bufferLength2 = sizeof(buffer2);
                        WSAQUERYSET *pResults2 = (WSAQUERYSET*)&buffer2;
                        result2 = WSALookupServiceNext(hLookup2,LUP_FLUSHCACHE |LUP_RETURN_NAME | LUP_RETURN_TYPE | LUP_RETURN_ADDR | LUP_RETURN_BLOB | LUP_RETURN_COMMENT,&bufferLength2,pResults2);
                        if(result2 == 0)
                            wcout << "Service found: " << pResults2->lpszServiceInstanceName <<"\n";
                        Sleep(100000);
                    }
                }
            }
    
            Sleep(100000);
    
        return 0;     
    
    }
    

    When I compile this one and launch application error message appears"An error occurred while initializing look for devices, closing ......" !!!

    If you can give me information any article, code example or something else with helps me in my research I will be very pleased and thanks to you !!!

    Best Regards !!!

    解决方案

    Try to check this link,

    It may be useful for you ... :)

    这篇关于通过蓝牙代码示例C ++将文件从PC传输到其他设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆