代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
C语言蓝牙模块
作者:
/ 发布于
2011/1/5
/
1471
#include "stdafx.h" #include <winsock2.h> #include <Ws2bth.h> #include <BluetoothAPIs.h> #include <vector> #pragma comment(lib, "Irprops.lib") #pragma comment(lib, "Ws2_32.lib") #define WIN32_LEAN_AND_MEAN using namespace std; #include <stdio.h> #include <stdlib.h> BLUETOOTH_FIND_RADIO_PARAMS m_bt_find_radio = { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) }; BLUETOOTH_RADIO_INFO m_bt_info = { sizeof(BLUETOOTH_RADIO_INFO), 0, }; BLUETOOTH_DEVICE_SEARCH_PARAMS m_search_params = { sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS), 1, 0, 1, 1, 1, 15, NULL }; BLUETOOTH_DEVICE_INFO m_device_info = { sizeof(BLUETOOTH_DEVICE_INFO), 0, }; int wczytaj(char *(&bufor),char* nazwa) { FILE * pFile; long rozmiar; size_t result; pFile = fopen ( nazwa , "rb" ); if (pFile==NULL) {fputs ("File error",stderr); exit (1);} // wielkosc pliku: fseek (pFile , 0 , SEEK_END); rozmiar = ftell (pFile); rewind (pFile); // alokacja pamieci dla bufora: bufor = new char[rozmiar]; // kopiowanie do bufora result = fread (bufor,1,rozmiar,pFile); if (result != rozmiar) {fputs ("Blad czytania",stderr); exit (3);} fclose (pFile); return rozmiar; } int _tmain(int argc, wchar_t **argv) { unsigned int j=MAXINT; HANDLE m_radio=NULL; HBLUETOOTH_RADIO_FIND m_bt=NULL; HBLUETOOTH_DEVICE_FIND m_bt_dev=NULL; vector<HANDLE> radios; vector<BLUETOOTH_DEVICE_INFO> devices; char* inc=new char[7]; printf("Szukanie adapterow Bluetooth...\n"); m_bt=BluetoothFindFirstRadio(&m_bt_find_radio, &m_radio); if (m_bt==NULL) { printf("Nie znaleziono adapterow Bluetooth"); return 1; } do { radios.push_back(m_radio); }while(BluetoothFindNextRadio(&m_bt_find_radio, &m_radio)); for(unsigned int i=0;i<radios.size();i++) { BluetoothGetRadioInfo(m_radio,&m_bt_info); wprintf(L"Adapter nr. %d:\r\n", i); wprintf(L"\tNazwa:\t\t%s\r\n", m_bt_info.szName); wprintf(L"\tMAC\t\t%02x:%02x:%02x:%02x:%02x:%02x\r\n", m_bt_info.address.rgBytes[0], m_bt_info.address.rgBytes[1], 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"\tKlasa:\t\t0x%08x\r\n", m_bt_info.ulClassofDevice); wprintf(L"\tProducent:\t0x%04x\r\n", m_bt_info.manufacturer); } do { printf("Ktorego adaptera uzywac? (nr) "); scanf_s("%u",&j); }while(j>devices.size()); m_search_params.hRadio=radios[j]; printf("\nSzukam urzadzen widocznych poprzez Bluetooth...\n\n"); m_bt_dev=BluetoothFindFirstDevice(&m_search_params, &m_device_info); do { devices.push_back(m_device_info); }while(BluetoothFindNextDevice(m_bt_dev,&m_device_info)); for(unsigned int i=0;i<devices.size();i++) { m_device_info = devices[i]; printf("Urzadzenie %d:\r\n", i); wprintf(L"\tNazwa:\t\t%s\r\n", m_device_info.szName); wprintf(L"\tMAC:\t\t%02x:%02x:%02x:%02x:%02x:%02x\r\n", m_device_info.Address.rgBytes[0], m_device_info.Address.rgBytes[1], 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"\tKlasa:\t\t0x%08x\r\n", m_device_info.ulClassofDevice); wprintf(L"\tPolaczony:\t%s\r\n", m_device_info.fConnected ? L"tak" : L"nie"); wprintf(L"\tParowany:\t%s\r\n", m_device_info.fAuthenticated ? L"tak" : L"nie"); wprintf(L"\tPamietany:\t%s\r\n", m_device_info.fRemembered ? L"tak" : L"nie"); } do { printf("Z ktorym urzadzeniem parowac? (nr) "); scanf_s("%u",&j); }while(j>devices.size()); BluetoothAuthenticateDevice(NULL,radios[0],&devices[j],NULL,0); //WSA I WINSOCK2 SOCKET sck; SOCKADDR_BTH sadr; WSADATA wsaData={0}; int iResult=0; iResult=WSAStartup(MAKEWORD(2,2),&wsaData); if(iResult!=NO_ERROR) { printf("Blad inicjalizacji WSA\n"); return 0; } sck=socket(AF_BTH,SOCK_STREAM,BTHPROTO_RFCOMM); if(sck==INVALID_SOCKET) { printf("Blad przy tworzeniu gniazda\n"); CloseHandle(m_radio); BluetoothFindDeviceClose(m_bt_dev); BluetoothFindRadioClose(m_bt); WSACleanup(); system("pause"); return 0; } sadr.addressFamily=AF_BTH; sadr.btAddr=devices[j].Address.ullLong; sadr.serviceClassId=OBEXObjectPushServiceClass_UUID; sadr.port=BT_PORT_ANY; if(connect(sck,(SOCKADDR *) &sadr,sizeof(sadr))==SOCKET_ERROR) { printf("Blad polaczenia!\n"); CloseHandle(m_radio); BluetoothFindDeviceClose(m_bt_dev); BluetoothFindRadioClose(m_bt); closesocket(sck); WSACleanup(); system("PAUSE"); return 0; } char a=0xA0; char* inform = new char[7]; inform[0] = 0x80; inform[1] = 0x00; inform[2] = 0x07; inform[3] = 0x10; inform[4] = 0x00; inform[5] = 0x20; inform[6] = 0x00; send(sck,inform,7,0); recv(sck,inc,7,0); if(inc[0]!=a) { printf("Blad polaczenia OBEX\n"); closesocket(sck); WSACleanup(); BluetoothFindDeviceClose(m_bt_dev); BluetoothFindRadioClose(m_bt); CloseHandle(m_radio); system("pause"); } delete []inform; int rozmiar=0; char* bufor=0; rozmiar=wczytaj(bufor,"a.txt"); printf("rozmiar pliku to:%d\n",rozmiar); inform = new char[20+rozmiar]; unsigned int* pRozmiar_32=(unsigned int*)(inform+13);//jak widac na 13 pozycji powinien znajdowac sie rozmiar obiektu unsigned __int16* pRozmiar_16 = (unsigned __int16*)(inform+18);//jak widac na 18 pozycji znajduje sie rozmiar kawalka Body pRozmiar_32[0]=rozmiar;//dzieki temu zawsze automatycznie bedziemy mieli rozmiar podany pRozmiar_16[0]=rozmiar+3;//+3 bo rozmiar body to rozmiar kawalka ktory przesylamy (my przesylamy calosc) + HI + 2_bajty_rozmiaru pRozmiar_16 = (unsigned __int16*)(inform+1); pRozmiar_16[0]=rozmiar+20; inform[0] = 0x02; inform[3] = 0x01; inform[4] = 0x00; inform[5] = 0x06;//czemu 0x04? sizeof("a.txt") to pewnie 6 inform[6] = 'a'; inform[7] = '.'; inform[8] = 't'; inform[9] = 'x'; inform[10] = 't'; inform[11] = '\0'; inform[12] = 0xC3;//length inform[17] = 0x48;//zrobmy tak zeby byl tylko jeden kawalek Body - mamy bardzo maly plik, do tego tak jest mega prosciej - nie bedziemy pare razy wysylac tylko raz memcpy(inform+20,bufor,rozmiar); send(sck,inform,rozmiar+20,0); recv(sck,inc,7,0); if(inc[0]==0x90) { inform[0] = 0x02; inform[1] = 0x04; inform[2] = 0x22; inform[3] = 0x01; inform[4] = 0x00; inform[5] = 0x06; inform[6] = 'a'; } delete []inform; closesocket(sck); WSACleanup(); BluetoothFindDeviceClose(m_bt_dev); BluetoothFindRadioClose(m_bt); CloseHandle(m_radio); system("pause"); return 0; } </div>
试试其它关键字
蓝牙模块
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3