Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Objective-C中以本机方式列出蓝牙设备_Objective C_Cocoa_Bluetooth - Fatal编程技术网

在Objective-C中以本机方式列出蓝牙设备

在Objective-C中以本机方式列出蓝牙设备,objective-c,cocoa,bluetooth,Objective C,Cocoa,Bluetooth,我正在尝试编写一个非常简单的终端应用程序,它将定期扫描蓝牙设备,并显示范围内每个蓝牙设备的蓝牙网络地址(十六进制数字)。我的目标平台是MacOSX,所以我假设这将涉及Objective-C。我没有Objective-C方面的任何经验(虽然我掌握了C的所有基础知识),但这似乎应该是非常简单的 我在哪里可以找到文档和示例代码(或教程,或一些回答者过去使用过的代码),以快速、自然地列出蓝牙设备 以下Mac开发中心参考资料可能会引起您的兴趣。它有点深入,但有代码示例 不知道任何示例代码,但您需要使用IO

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


我在哪里可以找到文档和示例代码(或教程,或一些回答者过去使用过的代码),以快速、自然地列出蓝牙设备

以下Mac开发中心参考资料可能会引起您的兴趣。它有点深入,但有代码示例


不知道任何示例代码,但您需要使用IOBluetooth框架中的IOBluetoothDeviceInquiry类。《蓝牙设备访问指南》有一个。

您可以使用Gamekit Api和btstack框架完成此操作

但这是一个巨大的挑战

祝你一切顺利


如果你能把它弄出去。。请张贴您的经验。我也在寻找一个。

使用蓝牙和Objective-C可以通过IOBluetooth框架实现

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

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

下面是一些使用
IOBluetoothDeviceInquiry
获取范围内每个蓝牙设备地址的示例代码。通过以下方式开始查询过程:

IOBluetoothDeviceInquiry *inquirer = [IOBluetoothDeviceInquiry inquiryWithDelegate:self];
// Configure further here if necessary
[inquirer start];
现在,您可以使用
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];
}

命令行应用程序通常不使用RunLoop,但依赖于主线程的RunLoop。这个需求没有文档记录,它可能会导致各种依赖于环境的行为,但大多数情况下会导致搜索失败。特别是,设置自己的线程并驱动其运行循环是不够的

我是通过反复试验才发现这一点的。但为了人类的利益,我们可以找到一个搜索蓝牙设备的简单命令行应用程序的完整示例

相关部分是委托处理程序

// The purpose of this delegate is to finish the RunLoop once the inquiry completes.

@interface DiscoveryFinisher : NSObject<IOBluetoothDeviceInquiryDelegate>
- (void) deviceInquiryComplete: (IOBluetoothDeviceInquiry *)sender error: (IOReturn)error aborted: (BOOL)aborted;
@end

@implementation DiscoveryFinisher
- (void)deviceInquiryComplete: (IOBluetoothDeviceInquiry *)sender error: (IOReturn)error aborted: (BOOL)aborted
{
    CFRunLoopStop(CFRunLoopGetCurrent());
}
@end

在这里,委托仅用于最终停止RunLoop,但您可以实现协议的其他部分,例如持续更新设备列表。警告一句,因为这在多线程应用程序中最有用:如果这样做,您必须保持对主线程的了解,以便正确驱动运行循环。一般来说,最安全的方法是在一个单独的过程中进行设备搜索。

我找不到任何用于设备发现的代码-我有点累了,稍后我会更好地查看。谢谢你知道吗,你是对的。它提到了用于设备发现的BluetoothUI框架,但没有关于如何使用它的示例。我将继续寻找和编辑我所发现的这篇文章,因为我现在对如何做到这一点感兴趣。现在进行研究还为时过早你找到什么了吗?我还在四处寻找解决办法,但运气不好!这可能被改写成Swift吗?谢谢或者更具体地说,我没有受过Obj-C方面的教育,但确实理解桥接头。这两个文件是否都在头文件中,或者它们位于何处?谢谢
int main(int argc, const char* argv[])
{
    @autoreleasepool {
        DiscoveryFinisher* df = [[DiscoveryFinisher alloc] init];
        IOBluetoothDeviceInquiry* bdi = [[IOBluetoothDeviceInquiry alloc] initWithDelegate: df];
        [bdi setUpdateNewDeviceNames:NO];
        if ([bdi start] != kIOReturnSuccess) {
            return -1;
        }

        CFRunLoopRun();
        [bdi stop];

        NSArray *foundDevices = [bdi foundDevices];
        //... do something with them
    }

    return 0;
}