ios - 从 iPhone 应用程序搜索并连接蓝牙设备

标签 ios objective-c core-bluetooth

我正在尝试从我的应用程序使用 iPhone/iPad 设备的蓝牙,并搜索范围内附近的设备并连接到该设备。

与范围内的非 iOS 手机或 Mac 一样,它应该显示可用的设备,以及何时需要从应用程序进行连接。

我使用核心蓝牙框架尝试了以下代码:

    _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
    _centralManager.delegate = self;

- (void)DidUpdateState:(CBCentralManager *)central
{
    if (central.state != CBCentralManagerStatePoweredOn) {
        return;
    }

    //start scanning
    [self.centralManager scanForPeripheralsWithServices:nil options:nil];
    
}

但是没有委托(delegate)被调用。我在 iPad 4 上运行应用程序,我有 LE Android 手机(兼容蓝牙 4.0 LE)。

scanForPeripheralsWithServices:nil 我正在设置 nil 以显示所有可用设备。

最佳答案

您将不得不使用[CBPeripheralManager]

设置委托(delegate)后,触发[scanForPeripheralsWithServices:options:] 然后在委托(delegate)方法[didDiscoverPeripheral]中,您将获得每个外围设备的名称。然后您可以向用户显示可用设备列表,然后在 CBCentralManager 中使用函数 connectPeripheral

您可以找到用 Objective-C 编写的很棒的教程 here .

关于ios - 从 iPhone 应用程序搜索并连接蓝牙设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38970794/

相关文章:

ios - Quickblox WebRTC iOS : Receiver connection closes, 无法接听电话

objective-c - 在 UIBackgroundTaskIdentifier 中调度对主队列的调用

ios - 使用蓝牙 LE 在 iOS 和 Android 之间进行通信

ios - 如何制作类似 Instagram 的标签页?

iphone - iOS 如何检查当前是否正在通话

iphone - 如何将 subview 放置在屏幕上的任何位置, 'Docking Views'

iPhone iOS如何实例化一个黑白CGColorSpaceRef?

iphone - 未连接蓝牙设备不再可用时 IOS 收到通知

ios - CoreBluetooth - 后台模式下的 didDiscoverPeripheral 在没有 LightBlue 的情况下不会被调用

ios - 我收到一条警告,将我的 OPTIONAL 值与非零值进行比较将始终返回 true