I just purchased a one of the WiTilt v2.5 sensors, and I was wondering if anyone on here had experience interfacing with the sensor via a program rather than a hyperterminal application. From my limited experience with Bluetooth programming, I assume that since Serial-over-Bluetooth is possible with the WiTilt sensor that the Bluetooth module on the sensor most likely uses RFCOMM sockets (as opposed to L2CAP sockets) in order to transmit the data from the sensor to another Bluetooth device.
How I’m looking at things now, I should just be able to perform a device discovery (assuming only one Bluetooth device is within range) and connect to that device and read what it transmits. While this is a far from complete program (I’d have to take into account multiple Bluetooth devices and also the WiTilt’s menu interface), I feel that this is a pretty good start and am open to any suggestions or comments:
/*Using the BlueZ Bluetooth stack - http://www.bluez.org/ */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <sys/socket.h>
int main(int argc, char **argv)
{
char address[18], buf[1024];
int device, socket, num_return, status, bytes_read;
const int length = 8, max_return = 255;
inquiry_info *info = NULL;
struct sockaddr_rc addr = {0};
/*If an address was specified via the command prompt, try and locate the device with the target address*/
if(argc > 1) {
if((device = hci_devid(argv[1])) < 0) {
perror("Error opening device");
exit(1);
}
}
/*If no address was specified on the command prompt, grab the first active device connected to the computer (hci0 for instance)*/
else{
if((device = hci_get_route(NULL)) < 0) {
perror("Error opening device");
exit(1);
}
}
/*Open up a socket file descriptor to the device, which will be used by some BlueZ commands*/
if((socket = hci_open_dev(device)) < 0) {
perror("Error opening socket");
exit(1);
}
/*Dynamically allocate memory for the inquiry info*/
info = (inquiry_info *)malloc(max_return * sizeof(inquiry_info));
/*Perform the device discovery for length * 1.28 seconds, with up to max_return devices returned, using the device id found previously*/
if((num_return = hci_inquiry(device, length, max_return, NULL,
&info, IREQ_CACHE_FLUSH)) < 0) {
perror("HCI inquiry error");
}
/*We'll assume only one device was found via the inquiry to keep things simple*/
ba2str(&(info)->bdaddr, address);
printf("Found device: %s", address);
free(info);
close(socket);
/*Reinitialize with a new socket file descriptor to connect to the target Bluetooth device*/
if((socket = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
perror("Error creating socket");
exit(1);
}
/*Set the appropriate values of the structure*/
addr.rc_family = AF_BLUETOOTH; //Address family Bluetooth
addr.rc_channel = (uint8_t) 1; //RFCOMM Channel 1
str2ba(address, &addr.rc_bdaddr); //Address of target device
/*Try and establish a connection to the device*/
if((status = connect(s, (struct sockaddr *)&addr, sizeof(addr))) < 0) {
perror("Error connecting");
exit(1);
}
memset(buf, 0, sizeof(buf));
/*Print out anything returned*/
while((bytes_read = read(client, buf, sizeof(buf))) > 0) {
printf("Data: %s", bytes_read);
}
/*Close the socket file descriptor*/
close(socket);
return 0;
}
Cheers.