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?
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.
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.
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.
Using bluetooth with Objective-C can be achieved with the IOBluetooth framework.
An example of some useful classes for basic operation are:
IOBluetoothDevice
[IOBluetoothDevice pairedDevices]
returns an NSArray of paired devicesIOBluetoothDeviceInquiry
IOBluetoothHostController
powerState
property can tell you if your own bluetooth is on or offHere 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];
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With