Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scanning for BLE Devices from C/C++

from the "Bluetooth Device Access Guide", I've read that the Bluetooth API should be accessable from C or from C++. I've found some C-headers (IOBluetoothUserLib.h, Bluetooth.h) in the IOBluetooth framework that are related to Bluetooth and contain enumerations and data structured to define search creteria but I fail to find any function that takes such enumeration or data structure as parameter. According to the documentation I would have to create a CBCentralManager but I fail to find a way to do so from C or C++.

Background: We use OS/X as a developing plattform for devlopment of BLE enabled microcontrollers. To update firmware on this microcontrollers I want to write a BLE bootloader and I want to have a commandline client to update the firmware. All of the code is written in C++ and I wouldn't like to learn objectiv-C for this small task.

Any pointers, documentation, examples?

thank you

Torsten

like image 893
Torsten Robitzki Avatar asked Jan 20 '15 09:01

Torsten Robitzki


People also ask

How do you detect BLE devices?

To find BLE devices, you use the startScan() method. This method takes a ScanCallback as a parameter. You must implement this callback, because that is how scan results are returned.

What is a BLE scan?

Bluetooth Low Energy, or BLE for short, is a Bluetooth standard designed specifically for sensors and devices to transmit wireless data in the most energy-efficient way possible. There are various sensors with many different properties that can and must fulfil corresponding tasks depending on the area of application.


2 Answers

According to the documentation I would have to create a CBCentralManager but I fail to find a way to do so from C or C++.

The documentation you refer to is for classic Bluetooth, for which the IOBluetooth framework has some functionality. CBCentralManager is the manager from CoreBluetooth, which is for Bluetooth LE only.

For classic Bluetooth, the manager you want is the HID Manager from the IOKit framework, documentation for which can be found here. If you search around, you'll find lots of examples of C++ usage of IOKit and IOHIDManager (1, 2).

IOKit may in fact give you all the functionality you need, but IOBluetooth supplies some Bluetooth specific features. From Developing Bluetooth Applications:

Although you don’t need to use the Bluetooth API to access a HID-class device, you may choose to use functions or methods from the Bluetooth framework to enhance the user’s experience. For example, your application can provide Bluetooth-specific information that lets the user know if a device doesn’t support a particular service.

like image 191
Henrik Avatar answered Oct 19 '22 12:10

Henrik


I agreed with Henrik you'll need some glue. Look at RedBearLab guys work and precise to class.

ofxBLE. h/mm


// C++ interface //
// (Obj-C may be a superset of C, but this just makes interopability
// easier with oF)
class ofxBLE {
    protected:
        ofxBLEDelegate *btDelegate;
    public:
        ofxBLE();
        ~ofxBLE();
        void scanPeripherals();
        void sendPosition(uint8_t x, uint8_t y);
    bool isConnected();
};



...
- (void)bleDidDisconnect {
    NSLog(@"->Disconnected");
}
- (void)bleDidReceiveData:(unsigned char *)data length:(int)length {   
}
@end
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
//= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
//                                          C++ class implementation
ofxBLE::ofxBLE() {
    btDelegate = [[ofxBLEDelegate alloc] init];
}
ofxBLE::~ofxBLE() {

}
void ofxBLE::scanPeripherals(){
    [btDelegate scanForPeripherals];
}

void ofxBLE::sendPosition(uint8_t x, uint8_t y) {
    // position should be NORMALIZED to between 0 and 255 BEFORE
    // passing into this method!
    [btDelegate sendPositionX:x withY:y];
}
like image 38
WINSergey Avatar answered Oct 19 '22 13:10

WINSergey