Mudanças entre as edições de "Uso do Bluetooth com App Inventor e programa C"
Ir para navegação
Ir para pesquisar
(17 revisões intermediárias pelo mesmo usuário não estão sendo mostradas) | |||
Linha 1: | Linha 1: | ||
+ | |||
+ | =instalar ferramentas e bibliotecas= | ||
+ | |||
+ | sudo apt-get install bluez-utils | ||
+ | sudo apt-get install libbluetooth3-dev | ||
=Programa servidor em C= | =Programa servidor em C= | ||
Linha 8: | Linha 13: | ||
#include <unistd.h> | #include <unistd.h> | ||
#include <stdlib.h> | #include <stdlib.h> | ||
+ | #include <string.h> | ||
#include <sys/socket.h> | #include <sys/socket.h> | ||
#include <bluetooth/bluetooth.h> | #include <bluetooth/bluetooth.h> | ||
Linha 49: | Linha 55: | ||
// read data from the client | // read data from the client | ||
− | bytes_read = read(client, buf, sizeof(buf)); | + | bytes_read = read(client, buf, sizeof(buf)+1); |
if( bytes_read > 0 ) { | if( bytes_read > 0 ) { | ||
printf("received [%s]\n", buf); | printf("received [%s]\n", buf); | ||
} | } | ||
+ | //envia uma resposta | ||
+ | strcpy(buf,"ALO CELULAR!"); | ||
+ | write (client, buf, sizeof(buf)); | ||
+ | sleep(10); | ||
// close connection | // close connection | ||
close(client); | close(client); | ||
Linha 59: | Linha 69: | ||
return 0; | return 0; | ||
} | } | ||
− | < | + | </syntaxhighlight> |
+ | |||
+ | =Instalação do servico bluetooth serial= | ||
− | Antes de executar o programa deve-se incluir o serviço de serial: | + | Antes de executar o programa, deve-se incluir o serviço de serial: |
sdptool add SP | sdptool add SP | ||
+ | |||
+ | Para conferir o serviço: | ||
+ | |||
+ | sdptool browse local | ||
+ | |||
+ | =Conferindo o MAC do servidor= | ||
+ | |||
+ | |||
+ | hcitool dev | ||
+ | |||
+ | Este endereço deverá ser usado no App Inventor | ||
+ | |||
+ | =Pingando em um dispositivo remoto= | ||
+ | |||
+ | Primeiro descubra o dispositivo. TORNE-O VISÌVEL e faça: | ||
+ | |||
+ | hcitool scan | ||
+ | |||
+ | Depois pingue no dispositivo: | ||
+ | |||
+ | |||
+ | sudo l2ping <endereço> | ||
+ | |||
+ | =Para compilar o programa= | ||
+ | |||
+ | gcc rcomm-server.c -lbluetooth -o rcomm-server | ||
+ | |||
+ | =Programa teste app inventor= | ||
+ | |||
+ | O programa abaixo testa a transmissão bluetooth via App Inventor. O botão conectar permite realizar a conexão dos dispositivos (smartphone com PC). O botão sendo hello envia uma mensagem texto para o programa C. | ||
+ | |||
+ | [[Arquivo:Fig1-Bluetooth-PRG1.png|700px]] | ||
+ | |||
+ | O componente timer verifica periodicamente a chegada de mensagens do programa C. Se uma mensagem chegar, ela será mostrada no display. | ||
+ | |||
+ | [[Arquivo:Fig2-Bluetooth-PRG1.png|700px]] | ||
+ | |||
+ | =Links de referência para construir um servidor com entradas multiplexadas= | ||
+ | |||
+ | [http://www.gnu.org/software/libc/manual/html_node/Waiting-for-I_002fO.html] | ||
+ | |||
+ | [http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#select] | ||
+ | |||
+ | [http://books.google.com.br/books?id=dmt_mERzxV4C&pg=PA129&lpg=PA129&dq=select+function+FD_SET&source=bl&ots=dr8YZMIHNF&sig=JAnmAc4Xo6UUDwrAILskwW2uBqQ&hl=en&sa=X&ei=dwjTUJfvLYXO9ATUtYCoBg&ved=0CFAQ6AEwBTgK#v=onepage&q=select%20function%20FD_SET&f=false] | ||
+ | |||
+ | http://www.piaccess.com/trac/wiki/Linux_BlueZ_Bluetooth_Serial_Setup |
Edição atual tal como às 11h07min de 20 de março de 2013
instalar ferramentas e bibliotecas
sudo apt-get install bluez-utils sudo apt-get install libbluetooth3-dev
Programa servidor em C
O código do servidor foi retirado de [1]:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
int main(int argc, char **argv)
{
struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
char buf[1024] = { 0 };
int s, client, bytes_read;
socklen_t opt = sizeof(rem_addr);
// allocate socket
if ((s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM))==-1){
printf("Problemas na criação do socket\n");
exit(-1);
}
// bind socket to port 1 of the first available
// local bluetooth adapter
loc_addr.rc_family = AF_BLUETOOTH;
loc_addr.rc_bdaddr = *BDADDR_ANY;
//str2ba( endereco, &loc_addr.rc_bdaddr );
loc_addr.rc_channel = (uint8_t) 1;
if (bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr))==-1){
printf("Problemas no bind\n");
exit(-1);
}
printf("Preparando para listen..\n");
// put socket into listening mode
if (listen(s, 1)==-1)
printf("Problemas de listen...\n");
printf("Preparando para accept..\n");
// accept one connection
client = accept(s, (struct sockaddr *)&rem_addr, &opt);
printf("Preparando para leitura..\n");
ba2str( &rem_addr.rc_bdaddr, buf );
fprintf(stderr, "accepted connection from %s\n", buf);
memset(buf, 0, sizeof(buf));
// read data from the client
bytes_read = read(client, buf, sizeof(buf)+1);
if( bytes_read > 0 ) {
printf("received [%s]\n", buf);
}
//envia uma resposta
strcpy(buf,"ALO CELULAR!");
write (client, buf, sizeof(buf));
sleep(10);
// close connection
close(client);
close(s);
return 0;
}
Instalação do servico bluetooth serial
Antes de executar o programa, deve-se incluir o serviço de serial:
sdptool add SP
Para conferir o serviço:
sdptool browse local
Conferindo o MAC do servidor
hcitool dev
Este endereço deverá ser usado no App Inventor
Pingando em um dispositivo remoto
Primeiro descubra o dispositivo. TORNE-O VISÌVEL e faça:
hcitool scan
Depois pingue no dispositivo:
sudo l2ping <endereço>
Para compilar o programa
gcc rcomm-server.c -lbluetooth -o rcomm-server
Programa teste app inventor
O programa abaixo testa a transmissão bluetooth via App Inventor. O botão conectar permite realizar a conexão dos dispositivos (smartphone com PC). O botão sendo hello envia uma mensagem texto para o programa C.
O componente timer verifica periodicamente a chegada de mensagens do programa C. Se uma mensagem chegar, ela será mostrada no display.
Links de referência para construir um servidor com entradas multiplexadas
http://www.piaccess.com/trac/wiki/Linux_BlueZ_Bluetooth_Serial_Setup