Using Bluez for Native bluetooth support

Put problems with the Android NDK in this forum.

Using Bluez for Native bluetooth support

Postby timoshenko_av » Mon Nov 08, 2010 10:14 pm

Hello!
I try to add bluetooth func to my app, but when I try to find awail devices using this code hci_get_route(NULL) always return -1. Can enyone help me,please!
P.S. I try this code on Ubuntu 10.10 and it works. Maybe in Android I have to build Bluez some another way that I do it now?
Syntax: [ Download ] [ Hide ]
Using cpp Syntax Highlighting
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/socket.h>
  5. #include <bluetooth/bluetooth.h>
  6. #include <bluetooth/hci.h>
  7. #include <bluetooth/hci_lib.h>
  8.  
  9. int main(int argc, char **argv)
  10. {
  11.     inquiry_info *ii = NULL;
  12.     int max_rsp, num_rsp;
  13.     int dev_id, sock, len, flags;
  14.     int i;
  15.     char addr[19] = { 0 };
  16.     char name[248] = { 0 };
  17.  
  18.     dev_id = hci_get_route(NULL);
  19.     sock = hci_open_dev( dev_id );
  20.     if (dev_id < 0 || sock < 0) {
  21.         perror("opening socket");
  22.         exit(1);
  23.     }
  24.  
  25.     len  = 8;
  26.     max_rsp = 255;
  27.     flags = IREQ_CACHE_FLUSH;
  28.     ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info));
  29.    
  30.     num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
  31.     if( num_rsp < 0 ) perror("hci_inquiry");
  32.  
  33.     for (i = 0; i < num_rsp; i++) {
  34.         ba2str(&(ii+i)->bdaddr, addr);
  35.         memset(name, 0, sizeof(name));
  36.         if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name),
  37.             name, 0) < 0)
  38.         strcpy(name, "[unknown]");
  39.         printf("%s  %s\n", addr, name);
  40.     }
  41.  
  42.     free( ii );
  43.     close( sock );
  44.     return 0;
  45. }
  46.  
Parsed in 0.010 seconds, using GeSHi 1.0.8.4
timoshenko_av
Freshman
Freshman
 
Posts: 8
Joined: Tue Oct 05, 2010 10:02 am

Top

Re: Using Bluez for Native bluetooth support

Postby pelya » Sat Nov 27, 2010 8:38 pm

If you're using HTC device you-re out of luck - they have broken bluetooth libraries, search Market for "wiimote" and you'll find lot of apps which will connect your Wii remote to your Android over bluetooth, every app has the note that it doesn't work on HTC devices.
pelya
Master Developer
Master Developer
 
Posts: 323
Joined: Mon Nov 23, 2009 11:31 am

Re: Using Bluez for Native bluetooth support

Postby timoshenko_av » Thu Dec 09, 2010 8:52 am

Thanks for your answer!Is this "feature" only on HTC devices?
timoshenko_av
Freshman
Freshman
 
Posts: 8
Joined: Tue Oct 05, 2010 10:02 am

Top

Return to NDK Problems

Who is online

Users browsing this forum: No registered users and 3 guests