Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of available Bluetooth devices?

I'm currently creating an iPhone app (Xcode 4.3.1, IOS 5) that could use Bluetooth devices! Main goal of this application is indoor navigation (GPS inside buildings isn't really accurate).

The only solution I see here (to keep my app on AppStore) is to try scan for available bluetooth devices!

I tried to use CoreBluetooth framework, but I don't get list of available devices! Maybe I don't use these functions correctly

#import <UIKit/UIKit.h> #import <CoreBluetooth/CoreBluetooth.h>  @interface AboutBhyperView : UIViewController <CBPeripheralDelegate, CBCentralManagerDelegate> {     CBCentralManager *mgr; } @property (readwrite, nonatomic) CBCentralManager *mgr; @end    - (void)viewDidLoad {     [super viewDidLoad];      mgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; }   - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {       NSLog([NSString stringWithFormat:@"%@",[advertisementData description]]); }  -(void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals{     NSLog(@"This is it!"); }   - (void)centralManagerDidUpdateState:(CBCentralManager *)central{      NSString *messtoshow;      switch (central.state) {         case CBCentralManagerStateUnknown:         {             messtoshow=[NSString stringWithFormat:@"State unknown, update imminent."];             break;         }         case CBCentralManagerStateResetting:         {             messtoshow=[NSString stringWithFormat:@"The connection with the system service was momentarily lost, update imminent."];             break;         }         case CBCentralManagerStateUnsupported:         {             messtoshow=[NSString stringWithFormat:@"The platform doesn't support Bluetooth Low Energy"];             break;         }         case CBCentralManagerStateUnauthorized:         {             messtoshow=[NSString stringWithFormat:@"The app is not authorized to use Bluetooth Low Energy"];             break;         }         case CBCentralManagerStatePoweredOff:         {             messtoshow=[NSString stringWithFormat:@"Bluetooth is currently powered off."];             break;         }         case CBCentralManagerStatePoweredOn:         {             messtoshow=[NSString stringWithFormat:@"Bluetooth is currently powered on and available to use."];             [mgr scanForPeripheralsWithServices:nil options:nil];             //[mgr retrieveConnectedPeripherals];  //--- it works, I Do get in this area!              break;         }         }     NSLog(messtoshow);  }  

I'm not sure about this line, how to pass correct parameters?

[mgr scanForPeripheralsWithServices:nil options:nil]; 

I took a look into apple reference .. and I still don't understand what's that CBUUID ??? Every device has some bluetooth id? where can I find it?

- (void)scanForPeripheralsWithServices:(NSArray *)serviceUUIDs options:(NSDictionary *)options; 

Parameters serviceUUIDs - An array of CBUUIDs the app is interested in. options - A dictionary to customize the scan, see CBCentralManagerScanOptionAllowDuplicatesKey.

Is there any other way to use Bluetooth on IOS? I mean, older frameworks that don't use BLE 4.0!

any advice would be appreciated!

thanks!

like image 836
mz87 Avatar asked Apr 16 '12 16:04

mz87


People also ask

How do I get a list of Bluetooth devices?

By using BluetoothAdapter method getBondedDevices(), we can get the Bluetooth paired devices list. Following is the code snippet to get all paired devices with name and MAC address of each device.

How do I get a list of Bluetooth devices on my Android phone?

The getBoundedDevices() method of BluetoothAdapter class provides a set containing list of all paired or bounded bluetooth devices. In this example, we are checking if the bluetooth is turned off, if yes then turn it on and list all the paired devices.

Can you search for Bluetooth devices?

Download a Bluetooth scanner app. For example, download LightBlue for iPhone, or get LightBlue for Android. This kind of app detects and lists all Bluetooth devices broadcasting nearby. When the item shows up on the list, try to locate it.

How do I find Bluetooth devices around me?

To find an active Bluetooth device, first make sure you have Bluetooth enabled on your smartphone. Next, download Wunderfind for your iPhone or Android device and launch the app. Immediately, you'll see a list of Bluetooth devices that your smartphone has detected using its built-in Bluetooth radio.


2 Answers

This guide looks promising for Bluetooth 3.0. Remember that the CoreBluetooth framework is ONLY for Bluetooth Low Energy (4.0). At bluetooth.com's dev-pages you can see some examples of globally defined services, and as Guan Yang mentionen, you can see that the heart rate service is 0x180D. UUID`s of the unit is defined by the manufacturer.

Here's a code snippet to maybe help you along the way.

// Initialize a private variable with the heart rate service UUID     CBUUID *heartRate = [CBUUID UUIDWithString:@"180D"];  // Create a dictionary for passing down to the scan with service method NSDictionary *scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];  // Tell the central manager (cm) to scan for the heart rate service [cm scanForPeripheralsWithServices:[NSArray arrayWithObject:heartRate] options:scanOptions] 
like image 98
chwi Avatar answered Oct 04 '22 04:10

chwi


in Bluetooth, there are "Services". A device publishes 1..N services, and each service has 1..M characteristics. Each service has an unique identifier, called UUID.

For example, a heart rate Bluetooth Sensor that offers the service "heart rate", publishes a service with UUID 0x180D.

(more services here)

So when you perform a search, you must provide a UUID as the criteria, telling which service to search.

like image 45
viktorvogh Avatar answered Oct 04 '22 04:10

viktorvogh