6

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

Torsten Robitzki
  • 3,041
  • 1
  • 21
  • 35
  • You can check [blucat](https://github.com/ieee8023/blucat) project once if it solves your purpose. – Mohit Jain Jan 22 '15 at 12:34
  • @MohitJain To me it looks like bluecat is written in java. So I had to adapt C/CPP to Java to use a objective C Library. – Torsten Robitzki Jan 22 '15 at 12:42
  • OK. From my understanding what you need is actually some library (possibly written in objective-C) which has some interfaces so that it can be called from C. You can take some open-source objective-C code for BLE and plug this into your code. (possibly) – Mohit Jain Jan 22 '15 at 12:55
  • 1
    @MohitJain That's the route I'm currently following. But I wanted to avoid to learn obj-c and because the documentation im cited, explicitly stated that the API is accessible from C/C++, I am asking here, _how_ to use the BLE API from C :-) So, what I need is a solution. Preferable one that I can write in C ;-) – Torsten Robitzki Jan 22 '15 at 13:07

2 Answers2

5

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.

Community
  • 1
  • 1
Henrik
  • 4,254
  • 15
  • 28
  • Hi @Henrik, that makes a lot of sense. So it's not possible to use C/C++ to access the Bluetooth LE API, right? – Torsten Robitzki Jan 29 '15 at 07:51
  • 1
    Right, at least I can't find any reference to a C++ API in the CoreBluetooth documentation. I reread your post now, and see that you're actually interested in Bluetooth LE, not classic Bluetooth, so I'm sorry my answer didn't solve your problem. – Henrik Jan 29 '15 at 07:53
  • 1
    It did solved my problem by pointing out that I was barking up the wrong tree ;-) – Torsten Robitzki Jan 29 '15 at 07:54
  • You'll probably want to write some [Objective-C++](http://stackoverflow.com/questions/3684112/what-is-objective-c) glue to make use of CoreBluetooth. – Henrik Jan 29 '15 at 08:19
1

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];
}
Community
  • 1
  • 1
WINSergey
  • 1,977
  • 27
  • 39