标签: cbperipheralmanager

在 iOS 上的广告中发送蓝牙 LE 数据

我的应用程序作为蓝牙 LE 外围设备运行,我试图在广告中发送几个字节的自定义数据。

func btStartBroadcasting(peripheral: CBPeripheralManager!) {

    // create an array of bytes to send
    var byteArray = [UInt8]()
    byteArray.append(0b11011110); // 'DE'
    byteArray.append(0b10101101); // 'AD'

    // convert that array into an NSData object
    var manufacturerData = NSData(bytes: byteArray,length: byteArray.count)

    // define a UIUD for the service
    let theUUid = CBUUID(NSUUID: uuid)

    // build the bundle of data
    let dataToBeAdvertised:[String: AnyObject!] = [
        CBAdvertisementDataLocalNameKey : "I wish this worked",
        CBAdvertisementDataManufacturerDataKey : manufacturerData,
        CBAdvertisementDataServiceUUIDsKey : [theUUid],
    ]

    peripheral.startAdvertising(dataToBeAdvertised)

}
Run Code Online (Sandbox Code Playgroud)

但 CBAdvertisementDataManufacturerDataKey 中的数据集似乎被删除并且没有通过无线电发送出去。我已经阅读了苹果文档和网上能找到的所有关于此问题的小片段。共识似乎是核心蓝牙忽略数据,因为仅支持 …

core-bluetooth bluetooth-lowenergy ios-bluetooth swift cbperipheralmanager

5
推荐指数
1
解决办法
6066
查看次数

Error Domain=CBErrorDomain Code=7 "指定的设备已与我们断开连接

我们需要从蓝牙设备获取数据到 IOS 设备core_bluetooth.frameworks。我们正在使用. didDisconnectPeripheral 每次都在 didConnectPeripheral 之后调用。我们得到的错误是错误是Error Domain=CBErrorDomain Code=7 "The specified device has disconnected from us." UserInfo={NSLocalizedDescription=The specified device has disconnected from us.}

我们试过的代码是://in ViewDidLoad

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerOptionShowPowerAlertKey, nil];
     self.central=[[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];
     //self.central=[[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)];
     self.discoveredPeripherals=[NSMutableArray new];
Run Code Online (Sandbox Code Playgroud)

//蓝牙开/关

-(void) centralManagerDidUpdateState:(CBCentralManager *)central {
NSString *stateString = nil;
    switch(central.state)
    {
        case CBCentralManagerStateResetting:
            stateString = @"The connection with the system service was momentarily lost, update imminent.";
            break;
        case CBCentralManagerStateUnsupported:
            stateString = @"The …
Run Code Online (Sandbox Code Playgroud)

core-bluetooth bluetooth-lowenergy cbcentralmanager cbperipheralmanager

5
推荐指数
0
解决办法
3008
查看次数

如何在CBPeripheralManager处于活动状态时提高CBCentralManager的性能

我们创建了一个iOS应用程序,它实现了一个CBCentralManager来连接我们创建的设备,以10Hz的频率传输数据.这些数据通过并快速显示非常重要,因此我们围绕此建立了严格的延迟检查,如果错过了太多的点,或者如果本地时钟检测到传入值减慢,我们将会出错并断开连接.

客户端已经要求我们顶级实现第二个iOS应用程序,它将观察第一个.我们在原始应用程序中实现了一个CBPeripheralManager,它可以广告,可以连接,并定期将其数据发布到一些传出特性.

我们发现我们似乎无法将观察者iOS应用程序连接到原始iOS应用程序(即,原始iOS应用程序同时具有与设备的CBCentral连接和同时活动的观察者应用程序的CBPeripheral连接),绊倒我们对来自设备的传入数据的延迟检查.

我已经尝试了我能想到的一切,我为CBPeripheralManager和CBCentralManager使用了单独的队列,如下所示:

    q = dispatch_get_global_queue(QOS_CLASS_UTILITY, 0);
    ptr_CBPeriphMgr = [[CBPeripheralManager alloc] initWithDelegate:self queue:q];
Run Code Online (Sandbox Code Playgroud)

也,

  • 我记录并记录了所有内容,验证我的代码没有花太长时间
  • 我将几乎所有的代码都移出了BLE处理程序,使它们非常轻巧而不会阻塞,
  • 我尝试了低优先级的单独队列(上面显示的例子)
  • 我已经尝试将我的CBPeripheralManager数据速率降低到涓流,一秒钟更新几次
  • 我已经尝试在建立CBPeripheralManager连接后暂停延迟检查三秒钟(这非常不理想),但问题似乎是随机发生的,而不仅仅是在连接之后.

似乎无论我尝试什么,在外围设备和中央连接都处于活动状态4-5分钟后(我们有一个循环,其中第二个应用程序每隔五秒重复连接并断开连接,以挑战设备连接)我的传入值更新自中央设备减速到大约1/4或1/5速度,或者它们停止一整秒,然后几乎同时进行三到四次更新 - 这两次更新都会延迟我们的延迟检查.这就像是一些队列被填满并且表现平坦,但正如我上面提到的,我认为我正在使用单独的队列.

我在我的智慧结束...有没有人想过如何在iOS应用程序中优先考虑我的中心功能而不是我的外围功能,或以某种方式提高性能以防止这成为一个问题并保持我的应用程序响应10Hz来自设备的更新,即使被视为外围设备?

(编辑说我们正在重复连接/断开第二个应用程序...也许我在正确断开连接后没有清理,垃圾堆积并搞砸了BLE?这可以解释为什么问题似乎发生在4之后无论第二次连接的数据更新频率如何,均为-5分钟.)

ios bluetooth-lowenergy cbperipheral cbcentralmanager cbperipheralmanager

5
推荐指数
1
解决办法
345
查看次数

外围没有连接 - 斯威夫特

在我的Swift应用程序中,我有一个应该找到蓝牙设备并连接到它的视图.蓝牙设备已开机.我能够扫描并找到该设备,然后我调用该功能连接到它,但我得不到任何反馈.我不确定为什么它无法连接.

didFailToConnectPeripheral或didConnectPeripheral都没有返回任何值.

我如何让它工作?

import UIKit
import CoreBluetooth

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {

    var manager: CBCentralManager!


    override func viewDidLoad() {
        super.viewDidLoad()
        manager = CBCentralManager (delegate: self, queue: nil)
    }


    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {

        print("Peripheral: \(peripheral)")

        manager.connectPeripheral(peripheral, options:nil)

        manager.stopScan()

    }



    func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
        print("connected!")
    }

    func centralManager(central: CBCentralManager, didDisconnectPeripheral
        peripheral: CBPeripheral, error: NSError?) {
            print("disconnected!")
    }


    func centralManager(central: CBCentralManager,
        didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
            print("failed")
    }


    func …
Run Code Online (Sandbox Code Playgroud)

core-bluetooth bluetooth-lowenergy cbperipheral swift cbperipheralmanager

4
推荐指数
1
解决办法
3727
查看次数

iOS蓝牙peripheralManagerDidUpdateState从未被调用

我正在尝试将 iPhone 设置为心率监视器并使用标准心率服务发送信息,以便我在 PC 上运行的应用程序可以检索数据。我是 iOS 新手,但之前我已经在 Android 和 Windows 上运行过蓝牙功能。我正在关注这里的信息,但我在第一个障碍处就跌倒了......

根据文档中的指定,我首先调用

CBPeripheralManager *myPeripheralManager = [[CBPeripheralManager alloc] initWithDelegate:(id<CBPeripheralManagerDelegate>)self queue:nil options:nil];
Run Code Online (Sandbox Code Playgroud)

我还实现了 PeripheralManagerDidUpdateState 回调,但问题就在这里。这个回调永远不会运行!我等了几分钟,什么也没发生。我怀疑我错过了一个非常简单的步骤,因为我搜索了一整天,但在网上什么也没找到!谁能解释一下在代码中和用我的 iPhone 进行实际操作时要采取的明确步骤吗?在这个回调运行之前它必须有连接吗?它必须与任何东西配对吗?

这是完整的代码。一切运行良好,我没有收到任何错误。

#import "ViewController.h"
@import CoreBluetooth;

@interface ViewController()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"App running...");

    //Create the peripheral manager
    CBPeripheralManager *myPeripheralManager = [[CBPeripheralManager alloc] initWithDelegate:(id<CBPeripheralManagerDelegate>)self queue:nil options:nil];
}

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
    int state = peripheral.state;
    NSLog(@"Peripheral manager state …
Run Code Online (Sandbox Code Playgroud)

bluetooth objective-c ios core-bluetooth cbperipheralmanager

2
推荐指数
1
解决办法
2368
查看次数

由于 API 滥用,CoreBluetooth 与未使用的外围设备断开连接

我正在尝试从带有 CoreBluetooth 的 iPad 连接到 MacBook Pro。

这是我的代表团CBCentralManagerDelegate

extension MasterViewController: CBCentralManagerDelegate {
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        if central.state == .poweredOn {
            print("Scanning for peripherals")
            central.scanForPeripherals(withServices: nil, options: nil)
            Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.stopScan), userInfo: nil, repeats: true)
        }
    }

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        print("Did discover peripheral", peripheral)

        central.connect(peripheral, options: nil)

    }

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        print("Did connect to ", peripheral)

        peripheral.delegate …
Run Code Online (Sandbox Code Playgroud)

ios core-bluetooth swift cbperipheralmanager

2
推荐指数
1
解决办法
5327
查看次数

CoreBluetooth:无法发现已发现外围设备的特征

我写了一些CoreBluetooth代码,我可以发现设备,但我似乎无法发现特征无法发现我发现的外围设备的有人有一个好的示例代码可以用来验证我的代码吗?

\n\n

这是我写的:

\n\n
#import "ViewController.h"\n@property(nonatomic, strong) CBCentralManager* centralmanager;\n@end\n\n@implementation ViewController\n\n- (void)viewDidLoad {\n    [super viewDidLoad];\n    self.centralmanager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];\n}\n\n- (void)centralManagerDidUpdateState:(CBCentralManager *)central\n{\n    CBCentralManagerState currentState =  central.state;\n    NSLog(@"state %i", currentState);\n\n    switch (central.state)\n    {\n        case CBCentralManagerStatePoweredOn:\n        {\n            NSDictionary *options = @{\n                                      CBCentralManagerScanOptionAllowDuplicatesKey: @YES\n                                      };\n            [self.centralmanager scanForPeripheralsWithServices:nil options:options];\n            NSLog(@"I just started scanning for peripherals");\n            break;\n        }\n    }\n}\n- (void)centralManager:(CBCentralManager *)central\n  didConnectPeripheral:(CBPeripheral *)peripheral{\n    NSLog(@"connected!");\n}\n\n- (void) centralManager:(CBCentralManager *)central\n  didDiscoverPeripheral:(CBPeripheral *)peripheral\n      advertisementData:(NSDictionary *)advertisementData\n                   RSSI:(NSNumber *)RSSI\n{\n    [self.centralmanager connectPeripheral:peripheral options:nil];\n\n    if ([[advertisementData description] containsString:@\xe2\x80\x9ckeyword\xe2\x80\x9d]) {\n\n\n         NSLog(@"peripheral count …
Run Code Online (Sandbox Code Playgroud)

ios core-bluetooth cbperipheral cbperipheralmanager

0
推荐指数
1
解决办法
2862
查看次数