从 C/C++ 扫描 BLE 设备

Scanning for BLE Devices from C/C++

"Bluetooth Device Access Guide",我读到蓝牙 API 应该可以从 C 或 C++ 访问。我在 IOBluetooth 框架中发现了一些与蓝牙相关的 C-headers (IOBluetoothUserLib.h, Bluetooth.h) 并且包含枚举和数据结构来定义搜索条件,但我找不到任何功能以此类枚举或数据结构作为参数。根据文档,我必须创建一个 CBCentralManager,但我无法从 C 或 C++ 中找到这样做的方法。

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

有任何指示、文档、示例吗?

谢谢

托斯滕

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++.

您参考的文档适用于经典蓝牙,IOBluetooth 框架对此具有一些功能。 CBCentralManager 是来自 CoreBluetooth 的管理器,仅适用于低功耗蓝牙。

对于经典蓝牙,您需要的管理器是来自 IOKit 框架的 HID 管理器,可以找到其文档 here. If you search around, you'll find lots of examples of C++ usage of IOKit and IOHIDManager (1, 2)。

IOKit 实际上可以为您提供所需的所有功能,但 IOBluetooth 提供了一些蓝牙特定功能。来自 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.

我同意 Henrik you'll need some glue. Look at RedBearLab guys work 并精确到 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];
}