[SOLUCIONADO] Qué debo leer para hacer un codigo de bluetooth en C

Iniciado por Hackmundy, Septiembre 23, 2016, 06:09:51 AM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

Septiembre 23, 2016, 06:09:51 AM Ultima modificación: Septiembre 26, 2016, 05:07:00 AM por Stiuvert
Hola como estan debo hacer un codigo que busque los dispocitivos cercanos mediante bluetooth y que se emparejen, pero no tengo ni la remota idea como empezar alguien me podria ayudar diciendo los temas que debo leer ya que en google no he encontrado mucha informacion..

saludos.


Un Hacker sabe de la materia;
Un Lamer Cree Saberlo;
Y yo solo aprendo.

Septiembre 23, 2016, 10:29:10 AM #1 Ultima modificación: Septiembre 23, 2016, 10:34:06 AM por grep
En Linux, para descubrir los dispositivos cercanos se suele hacer:

Código: php
hcitool scan


o

Código: php
hcitool inq



hcitool es un comando que forma parte del "BlueZ Package", el cual brinda soporte desde el user space al BlueZ kernel subsystem.  El repositorio lo puedes navegar en el siguiente enlace:

No tienes permitido ver los links. Registrarse o Entrar a mi cuenta

Al código fuente de hcitool, y la función de scan, lo encuentras en el mismo repositorio:

No tienes permitido ver los links. Registrarse o Entrar a mi cuenta

Las funciones importantes son:

Citar
Local Bluetooth adapters are assigned identifying numbers starting with 0, and a program must specify which adapter to use when allocating system resources. Usually, there is only one adapter or it doesn't matter which one is used, so passing NULL to hci_get_route will retrieve the resource number of the first available Bluetooth adapter.

Código: c
int hci_get_route( bdaddr_t *bdaddr );


* It is not a good idea to hard-code the device number 0, because that is not always the id of the first adapter. For example, if there were two adapters on the system and the first adapter (id 0) is disabled, then the first available adapter is the one with id 1.

If there are multiple Bluetooth adapters present, then to choose the adapter with address ``01:23:45:67:89:AB", pass the char * representation of the address to hci_devid and use that in place of hci_get_route.

Citar
Código: c
int hci_open_dev( int dev_id );


Most Bluetooth operations require the use of an open socket. hci_open_dev is a convenience function that opens a Bluetooth socket with the specified resource number [2]. To be clear, the socket opened by hci_open_dev represents a connection to the microcontroller on the specified local Bluetooth adapter, and not a connection to a remote Bluetooth device. Performing low level Bluetooth operations involves sending commands directly to the microcontroller with this socket.

Citar
hci_inquiry performs a Bluetooth device discovery and returns a list of detected devices and some basic information about them in the variable ii. On error, it returns -1 and sets errno accordingly.

Código: c
int hci_inquiry(int dev_id, int len, int max_rsp, const uint8_t *lap, inquiry_info **ii, long flags);


hci_inquiry is one of the few functions that requires the use of a resource number instead of an open socket, so we use the dev_id returned by hci_get_route. The inquiry lasts for at most 1.28 * len seconds, and at most max_rsp devices will be returned in the output parameter ii, which must be large enough to accommodate max_rsp results. We suggest using a max_rsp of 255 for a standard 10.24 second inquiry.

Citar
Once a list of nearby Bluetooth devices and their addresses has been found, the program determines the user-friendly names associated with those addresses and presents them to the user. The hci_read_remote_name function is used for this purpose.

Código: c
int hci_read_remote_name(int sock, const bdaddr_t *ba, int len, char *name, int timeout)


hci_read_remote_name tries for at most timeout milliseconds to use the socket sock to query the user-friendly name of the device with Bluetooth address ba. On success, hci_read_remote_name returns 0 and copies at most the first len bytes of the device's user-friendly name into name. On failure, it returns -1 and sets errno accordingly.

declaradas en hci_lib.h, y:

Citar
The basic data structure used to specify a Bluetooth device address is the bdaddr_t. All Bluetooth addresses in BlueZ will be stored and manipulated as bdaddr_t structures.

Código: c
int ba2str( const bdaddr_t *ba, char *str );


str2ba takes an string of the form ``XX:XX:XX:XX:XX:XX", where each XX is a hexadecimal number specifying an octet of the 48-bit address, and packs it into a 6-byte bdaddr_t. ba2str does exactly the opposite.

declarada en bluetooth.h.

Además necesitas del tipo de dato "inquiry_info" definido en hci.h:

Citar
The inquiry_info structure is defined as

Código: c
typedef struct {
    bdaddr_t    bdaddr;
    uint8_t     pscan_rep_mode;
    uint8_t     pscan_period_mode;
    uint8_t     pscan_mode;
    uint8_t     dev_class[3];
    uint16_t    clock_offset;
} __attribute__ ((packed)) inquiry_info;


For the most part, only the first entry - the bdaddr field, which gives the address of the detected device - is of any use. Occasionally, there may be a use for the dev_class field, which gives information about the type of device detected (i.e. if it's a printer, phone, desktop computer, etc.) and is described in the Bluetooth Assigned Numbers [3]. The rest of the fields are used for low level communication, and are not useful for most purposes. The interested reader can see the Bluetooth Core Specification [4] for more details.


Para poder usar estos headers quizás debas instalar el paquete libbluetooth-dev:

Código: php
apt-get install libbluetooth-dev


Y para poder compilar necesitas ejecutar el siguiente comando:

Código: php
gcc -o simplescan simplescan.c -lbluetooth


Puedes ver los ejemplos en las siguientes fuentes:
No tienes permitido ver los links. Registrarse o Entrar a mi cuenta
No tienes permitido ver los links. Registrarse o Entrar a mi cuenta
No tienes permitido ver los links. Registrarse o Entrar a mi cuenta
No tienes permitido ver los links. Registrarse o Entrar a mi cuenta

Saludos

Muchas Gracias si habia leido algo de eso, pero lamentablemente necesito hacerlo en windows,  voy a ver si se puede copiar esos header en windows.

saludos


Un Hacker sabe de la materia;
Un Lamer Cree Saberlo;
Y yo solo aprendo.