Commit 1f73f740 authored by Nisarg Bhavsar's avatar Nisarg Bhavsar

some work

parent 02f970da
# FYDP
FYDP
Makefile:
Compiles scan.c into scan executable, uses bluetooth adapter
to locate available bluetooth devices
......@@ -5,44 +5,118 @@
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <errno.h>
#include <bluetooth/rfcomm.h>
#include <bluetooth/l2cap.h>
#include <stdbool.h>
int main(int argc, char ** argv){
inquiry_info *ii = NULL;
inquiry_info *detectedDevices;
int max_rsp, num_rsp;
int dev_id, sock, len, flags;
int i;
int localBluetoothAdapter, sock, discoveryTime, flags, clientSock;
/**
* localBluetoothAdapter: Local Bluetooth adapter resource number
*
*/
char addr[19] = {0};
char name[248] = {0};
dev_id = hci_get_route(NULL);
sock = hci_open_dev(dev_id);
/**
* Retrieve the resource number for the first available local
* Bluetooth adapter
*/
localBluetoothAdapter = hci_get_route(NULL);
if(dev_id < 0 || sock < 0){
perror("opening socket");
/**
* Allocate resources to use chosen adapter
*/
sock = hci_open_dev(localBluetoothAdapter);
if(localBluetoothAdapter < 0 || sock < 0){
fprintf(stderr, "error code %d: %s\n", errno, strerror(errno));
exit(1);
}
len = 8;
max_rsp = 255;
flags = IREQ_CACHE_FLUSH;
ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info));
discoveryTime = 5; //hci_inquiry lasts for 1.26 * discoveryTime (s)
max_rsp = 255;
flags = IREQ_CACHE_FLUSH; //Cache of previously detected devices cleared
detectedDevices = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info));
/**
* Perform Bluetooth discovery
*/
num_rsp = hci_inquiry(localBluetoothAdapter,
discoveryTime, max_rsp, NULL, &detectedDevices, flags);
num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
if(num_rsp < 0) perror("hci_inquiry");
if(num_rsp < 0) {
fprintf(stderr, "hci_inquiry; error code %d: %s\n", errno, strerror(errno));
exit(1);
}
//Go over all detected devices and print out friendly-name
for(int i = 0; i < num_rsp; i++){
//Convert from bdaddr_t to char*
ba2str(&(detectedDevices+i)->bdaddr, addr);
for(i = 0; i < num_rsp; i++){
ba2str(&(ii+i)->bdaddr, addr);
memset(name, 0, sizeof(name));
if(hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name),name, 0) < 0)
strcpy(name, "[unknown]");
printf("%s, %s\n", addr, name);
if(hci_read_remote_name(sock, &(detectedDevices+i)->bdaddr,
sizeof(name),name, 0) < 0){
strcpy(name, "[unknown]");
}
printf("%i: %s, %s\n", i, addr, name);
}
free(ii);
int userDevice;
if(num_rsp > 0){
printf("Which device would you like to pair to: \n");
scanf("%i", &userDevice);
ba2str(&(detectedDevices+userDevice)->bdaddr, addr);
printf("Pairing to: %s\n", addr);
}
else
printf("No Bluetooth-enabled devices found\n");
close(sock);
return 0;
//Open a L2CAP socket
sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
struct sockaddr_l2 local_addr, client_addr;
int opt = sizeof(client_addr);
char buffer[1024];
bool wrongConnection = false;
local_addr.l2_family = AF_BLUETOOTH;
local_addr.l2_bdaddr = *BDADDR_ANY;
local_addr.l2_psm = htobs(0x1001);
fprintf(stderr, "Binding new socket\n");
bind(sock, (struct sockaddr *)&local_addr, sizeof(local_addr));
do{
fprintf(stderr, "Listening on new socket\n");
listen(sock, 2);
clientSock = accept(sock, (struct sockaddr *)&client_addr, &opt);
ba2str( &client_addr.l2_bdaddr, buffer);
wrongConnection = !(strcmp(buffer, addr) == 0);
if(wrongConnection){
fprintf(stderr, "Incorrect connection, closing.\n");
close(clientSock);
}
}while(wrongConnection);
fprintf(stderr, "accepted connection from %s\n", buffer);
close(sock);
close(clientSock);
free(detectedDevices);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment