scan.c 2.96 KB
Newer Older
Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
1 2 3 4 5 6 7
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
8 9 10 11 12 13
#include <errno.h>
#include <bluetooth/rfcomm.h>
#include <bluetooth/l2cap.h>

#include <stdbool.h>

Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
14 15 16

int main(int argc, char ** argv){
	
Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
17
	inquiry_info *detectedDevices;
Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
18
	int max_rsp, num_rsp;
Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
19 20 21 22 23 24
	int localBluetoothAdapter, sock, discoveryTime, flags, clientSock;

	/**
	 * localBluetoothAdapter: Local Bluetooth adapter resource number
	 * 
	 */
Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
25 26 27 28

	char addr[19] = {0};
	char name[248] = {0};

Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
29 30 31 32 33
	/**
	 * Retrieve the resource number for the first available local 
	 * Bluetooth adapter
	 */
	localBluetoothAdapter = hci_get_route(NULL);
Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
34

Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
35 36 37 38 39 40 41
	/**
	 * 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));
Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
42 43 44
		exit(1);
	}
	
Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
45 46 47 48 49 50 51 52 53 54
	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);
Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
55

Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
56 57 58 59 60 61 62 63 64
	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);
Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
65 66

		memset(name, 0, sizeof(name));
Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
67 68 69 70 71 72

		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);
Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
73 74

	}
Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
75 76 77 78 79 80 81 82 83 84 85
	
	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");	

Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
86 87
	close(sock);

Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101
	//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));
Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
102

Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	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);
Nisarg Bhavsar's avatar
Nisarg Bhavsar committed
122
}