在Objective-C中本地列出蓝牙设备

JP.*_*JP. 7 cocoa bluetooth objective-c

我正在尝试编写一个非常简单的终端应用程序,它将定期扫描蓝牙设备并显示范围内每个蓝牙设备的蓝牙网络地址(十六进制数字).我的目标平台是Mac OS X,所以我认为这将涉及Objective-C.我没有Objective-C的任何经验(虽然我有C的所有基础知识),但这看起来应该非常简单.

我在哪里可以找到快速本地列出蓝牙设备的文档和示例代码(或教程,或者过去曾经使用过的代码)?

Dan*_*est 6

使用带有Objective-C的蓝牙可以通过IOBluetooth框架实现.

一些用于基本操作的有用类的示例是:

  • IOBluetoothDevice
    • 连接方法
    • [IOBluetoothDevice pairedDevices] 返回配对设备的NSArray
    • 很多其他的东西
  • IOBluetoothDeviceInquiry
    • 寻找可用的设备
  • IOBluetoothHostController
    • powerState物业可以告诉您自己的蓝牙是打开还是关闭

以下是一些示例代码,IOBluetoothDeviceInquiry用于获取范围内每个蓝牙设备的地址.开始查询过程,例如:

IOBluetoothDeviceInquiry *inquirer = [IOBluetoothDeviceInquiry inquiryWithDelegate:self];
// Configure further here if necessary
[inquirer start];
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用以下IOBluetoothDeviceInquiryDelegate方法获取找到的设备的地址:

#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];
}
Run Code Online (Sandbox Code Playgroud)


Bra*_*nar 5

以下Mac Dev Center参考可能对您感兴趣.它有点深入,但确实有代码示例.

蓝牙设备访问指南简介