从C / C ++扫描BLE设备

时间:2015-01-20 09:09:22

标签: c macos bluetooth-lowenergy core-bluetooth

来自"Bluetooth Device Access Guide",我已经读过蓝牙API应该可以从C或C ++访问。我在IOBluetooth框架中发现了一些与蓝牙相关的C-header(IOBluetoothUserLib.h,Bluetooth.h),其中包含用于定义搜索creteria的枚举和数据但是我找不到任何需要这种枚举的函数或者数据结构作为参数。根据文档,我将不得不创建一个CBCentralManager但我找不到从C或C ++这样做的方法。

背景:我们使用OS / X作为开发平台,用于开发支持BLE的微控制器。要更新此微控制器上的固件,我想编写一个BLE引导加载程序,我希望有一个命令行客户端来更新固件。所有的代码都是用C ++编写的,我不想为这个小任务学习objectiv-C。

任何指针,文档,示例?

谢谢

的Torsten

2 个答案:

答案 0 :(得分:3)

  

根据文档,我将不得不创建一个CBCentralManager但我找不到从C或C ++这样做的方法。

您所参考的文档适用于经典蓝牙,IOBluetooth框架具有一些功能。 CBCentralManager是CoreBluetooth的管理员,仅适用于蓝牙LE。

对于经典蓝牙,您需要的管理器是来自IOKit框架的HID管理器,可以找到其文档here。如果你四处搜索,你会发现很多关于IOKit和IOHIDManager(12)的C ++用法的例子。

IOKit实际上可以为您提供所需的所有功能,但IOBluetooth提供了一些蓝牙特定功能。来自Developing Bluetooth Applications

  

虽然您不需要使用蓝牙API来访问HID级设备,但您可以选择使用蓝牙框架中的功能或方法来增强用户体验。例如,您的应用程序可以提供蓝牙特定信息,让用户知道设备是否不支持特定服务。

答案 1 :(得分:1)

我同意Henrik你需要一些胶水。查看RedBearLab guys work并精确到课堂。

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];
}
相关问题