Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing Bluetooth Devices Natively In Objective-C

I'm trying to write a very simple terminal app that will scan for Bluetooth devices at regular intervals and display the Bluetooth network address (the hex digits) of every Bluetooth device within range. My target platform is Mac OS X, so I assume that this will involve Objective-C. I don't have any experience in Objective-C (though I have all the basics of C), but this seems like it should be pretty straightforward.

Where can I find documentation and example code (or a tutorial, or code that some answerer has used in the past) for listing Bluetooth devices quickly and natively?

like image 585
JP. Avatar asked Nov 12 '09 16:11

JP.


People also ask

How do I get a list of Bluetooth devices?

Swipe down from the top of the screen. Touch and hold Bluetooth . If your accessory is listed under "Available media devices," next to your device's name, tap Settings . If no accessories are listed under "Previously connected devices," tap See all.

Can you scan for Bluetooth devices?

Device discovery is a scanning procedure that searches the local area for Bluetooth-enabled devices and requests some information about each one. This process is sometimes referred to as discovering, inquiring, or scanning.

How to get list of Bluetooth devices in android?

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.


2 Answers

Using bluetooth with Objective-C can be achieved with the IOBluetooth framework.

An example of some useful classes for basic operation are:

  • IOBluetoothDevice
    • connection methods
    • [IOBluetoothDevice pairedDevices] returns an NSArray of paired devices
    • alot of other stuff
  • IOBluetoothDeviceInquiry
    • looks for available devices
  • IOBluetoothHostController
    • the powerState property can tell you if your own bluetooth is on or off

Here is some example code for using IOBluetoothDeviceInquiry to get the address of each bluetooth device in range. Start the inquiry process with something like:

IOBluetoothDeviceInquiry *inquirer = [IOBluetoothDeviceInquiry inquiryWithDelegate:self];
// Configure further here if necessary
[inquirer start];

Now, you can get the address of the found devices using the IOBluetoothDeviceInquiryDelegate methods:

#pragma mark - IOBluetoothDeviceInquiryDelegate Methods

- (void) deviceInquiryComplete:(IOBluetoothDeviceInquiry *)sender error:(IOReturn)error aborted:(BOOL)aborted {
    NSArray *devices = [sender foundDevices];
    for (IOBluetoothDevice *device in devices) {
        const BluetoothDeviceAddress *address = [device getAddress];
        // Do something with address
    }
    [sender performSelector:@selector(start) withObject:nil afterDelay:7];
}
like image 58
Daniel Vogelnest Avatar answered Sep 17 '22 13:09

Daniel Vogelnest


The following Mac Dev Center reference maybe of interest to you. It is a little in depth but does have code examples.

Introduction to Bluetooth Device Access Guide

like image 44
Brandon Bodnar Avatar answered Sep 19 '22 13:09

Brandon Bodnar