简体   繁体   中英

How to connect a bluetooth input device to a computer?

I have created a bluetooth input device (stylus) and would like to connect it to both a Mac and Windows (and preferably Linux in the future).

Is there an ideal software / language to use to create a cross-platform application? I have considered writing native applications for each, but I don't feel the application will be so complex that this is absolutely necessary.

The application will take the input data of the BT device and use it to move the cursor around the screen and provide click and pressure functionality.

Thank you in advance.

I don't know how you device is set up.

However, if you managed to put on it a PIC (Such as the Arduino ATMega328 ) with at least one serial interface , you could be able to connect it to your PC via Universal Serial Bus (USB).

After that you will be able to open a pipe to your device in many languages.

C is always a good choice both for Linux and OS X, using POSIX libraries will make it even easier.

This snippet I wrote taking some tips online may help to get started

int init_port (const char * port_name, int baud) {
    /* Main vars */
    struct termios toptions;
    int stream;
    /* Port data */
    speed_t brate = baud;

    if ((stream = apri_porta(port_name)) < 1)
        return 0;

    if (tcgetattr(stream, &toptions) < 0) {
        printf("Error");
        return 0;
    }
    /* INITIALIZING BAUD RATE */
    cfsetispeed(&toptions, brate);
    cfsetospeed(&toptions, brate);

    // IMPORTANT BLOCK OF OPTIONS TO MAKE TX AND RX WORKING
    toptions.c_cflag &= ~PARENB;
    toptions.c_cflag &= ~CSTOPB;
    toptions.c_cflag &= ~CSIZE;
    toptions.c_cflag |= CS8;

    toptions.c_cflag &= ~CRTSCTS;

    toptions.c_cflag |= CREAD | CLOCAL;
    toptions.c_iflag &= ~(IXON | IXOFF | IXANY);

    toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    toptions.c_oflag &= ~OPOST;

    toptions.c_cc[VMIN]  = 0;
    toptions.c_cc[VTIME] = 0;

    tcsetattr(stream, TCSANOW, &toptions);
    if (tcsetattr(stream, TCSAFLUSH, &toptions) < 0) {
        printf("Error");
        return 0;
    }

    return stream;
}

int open_port (const char * port_name) {

    int stream;

    stream = open(port_name, O_RDWR | O_NONBLOCK );

    if (stream == -1)  {
        printf("apri_porta: Impossibile aprire stream verso '%s'\n", port_name);
        return -1;
    }

    return stream;
}

int close_port (int stream) {
    return (close(stream));
}

int write_to_port(int stream, char * str) {
    int len = (int)strlen(str);
    int n = (int)write(stream, str, len);
    if (n != len)
        return 0;
    return 1;
}

int read_from_port(int fd, char * buf, int buf_max, char until) {

    int timeout = 5000;
    char b[1];
    int i=0;

    do {
        int n = (int)read(fd, b, 1);
        if( n==-1) return -1;
        if( n==0 ) {
            usleep( 1 * 1000 );
            timeout--;
            continue;
        }
        buf[i] = b[0];
        i++;
    } while( b[0] != until && i < buf_max && timeout > 0 );

    buf[i] = 0;  // null terminate the string
    return 0;
}

Objective-C (OS X) has got a good library which works like a charm ( ORSSerialPort )

However, if you would like to have a cross-platform solution, Java is the best choice either for Windows, OS X and Linux.

I hope this helped you and others to get started.

Feel free to PM me if you need further help.

Best regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM