(IOS)如何检查一个iPhone的配对的蓝牙设备? [英] (iOS) How do I check an iPhone's paired Bluetooth devices?

查看:1363
本文介绍了(IOS)如何检查一个iPhone的配对的蓝牙设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用CoreBluetooth(如我的应用程序必须符合为App Store)检查所有当前配对并连接蓝牙设备。这是不应该要求任何蓝牙扫描,对不对?我只是想看看有什么系统与配对。如果我不能做到这一点,一个扫描是第二个选项。

我试图不工作。它说,蓝牙未接通电源和崩溃,但CBCentralManager的状态吧!如何解决这个问题,还是我完全偏离了轨道?任何想法

所有这一切都在的ViewController现在:

   - (无效)viewDidLoad中{    [超级viewDidLoad中];
    self.centralManager = [[CBCentralManager页头] initWithDelegate:自队列:dispatch_get_main_queue()];    [self.centralManager retrieveConnectedPeripherals] //使系统调用didRetrieveConnectedPeripherals
} - (无效)centralManager:(CBCentralManager *)中央didRetrieveConnectedPeripherals:(NSArray的*){外设
    的NSLog(@didRetrieveConnectedPeripherals称为);
    对于(CBPeripheral *一个在外设){
        的NSLog(a.name); //只要登录名,现在,看它是否认可它
    } //但它永远不会结束日志记录什么,我有一对/一个BT键盘与iPhone 5相连
} //我得到一个错误在某些时候,< CBConcreteCentralManager:0x71ab020>未开启 - (无效)centralManagerDidUpdateState:(CBCentralManager *){经理
    如果([经理状态] == CBCentralManagerStatePoweredOff)的NSLog(@CBCentralManagerStatePoweredOff);
    如果([经理状态] == CBCentralManagerStatePoweredOn)的NSLog(@CBCentralManagerStatePoweredOn); //这是记录的内容,当我在iPhone 5运行
    如果([经理状态] == CBCentralManagerStateResetting)的NSLog(@CBCentralManagerStateResetting);
    如果([经理状态] == CBCentralManagerStateUnauthorized)的NSLog(@CBCentralManagerStateUnauthorized);
    如果([经理状态] == CBCentralManagerStateUnknown)的NSLog(@CBCentralManagerStateUnknown);
    如果([经理状态] == CBCentralManagerStateUnsupported)的NSLog(@CBCentralManagerStateUnsupported);
}


解决方案

我一直在工作,这也,并希望一些我学到的东西会有所帮助。

东西夫妇:

1)你可能得到,因为你叫蓝牙未通电错误 [self.central retrieveConnectedPeripherals] 立即初始化CBCentralManager后。你需要给CBCentralManager足够的时间来连接到蓝牙硬件。

试试这个,

   - (无效)viewDidLoad中{
    [超级viewDidLoad中];
    self.centralManager = [[CBCentralManager页头] initWithDelegate:自队列:无];
}
- (无效)centralManagerDidUpdateState:(CBCentralManager *){经理
   开关(manager.state){
        案例CBCentralManagerStatePoweredOn:
             的NSLog(@CBCentral经理通电);
             [self.centralManager retrieveConnectedPeripherals]
             打破;
        案例CBCentralManagerStatePoweredOff:
             ...等等。
}

此外,要知道,CBCentralManager适用于带有蓝牙4.0的iOS设备。目前,蓝牙4.0安装在iPhone 4S的/ 5,iPod的4和iPad 3/4 /迷你。所以,要知道,你的应用程序将无法在iPad的1/2,iPhone的2/3,和iPod 2/3工作。

不过搞清楚了这一点,但它也有可能是你可能看不到你的键盘,因为它不是一个蓝牙设备4。让我知道你发现什么。

I want to use CoreBluetooth (as my app must be eligible for the App Store) to check all of the currently paired and connected Bluetooth devices. This should not require any Bluetooth scanning, right? I just want to see what the system is paired with. If I can't do that, a scan is the second option.

What I'm trying is not working. It says that Bluetooth is not powered on and crashes, but the CBCentralManager's state is on! Any ideas on how to fix this, or am I totally off track?

All of this in ViewController for now:

    - (void)viewDidLoad{

    [super viewDidLoad];
    self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];

    [self.centralManager retrieveConnectedPeripherals]; //makes the system call didRetrieveConnectedPeripherals
}

- (void)centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray *)peripherals{
    NSLog(@"didRetrieveConnectedPeripherals called");
    for (CBPeripheral *a in peripherals){
        NSLog(a.name); //just log the name for now to see if it recognized it
    } //but it never ends up logging anything, and I have a BT keyboard paired/connected with the iPhone 5
} //and I get an error at some point, <CBConcreteCentralManager: 0x71ab020> is not powered on

- (void)centralManagerDidUpdateState:(CBCentralManager *)manager{
    if ([manager state] == CBCentralManagerStatePoweredOff) NSLog(@"CBCentralManagerStatePoweredOff");
    if ([manager state] == CBCentralManagerStatePoweredOn) NSLog(@"CBCentralManagerStatePoweredOn"); //this is what gets logged when I run it on an iPhone 5
    if ([manager state] == CBCentralManagerStateResetting) NSLog(@"CBCentralManagerStateResetting");
    if ([manager state] == CBCentralManagerStateUnauthorized) NSLog(@"CBCentralManagerStateUnauthorized");
    if ([manager state] == CBCentralManagerStateUnknown) NSLog(@"CBCentralManagerStateUnknown");
    if ([manager state] == CBCentralManagerStateUnsupported) NSLog(@"CBCentralManagerStateUnsupported");
}

解决方案

I've been working on this also, and hopefully some of what I've learned will help.

Couple of things:

1) You're likely getting the "Bluetooth not powered on" error because you're calling [self.central retrieveConnectedPeripherals] immediately after initializing the CBCentralManager. You need to give the CBCentralManager enough time to connect to the bluetooth hardware.

Try this instead,

- (void)viewDidLoad{
    [super viewDidLoad];
    self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)manager {
   switch (manager.state) {
        case CBCentralManagerStatePoweredOn:
             NSLog(@"CBCentral Manager powered on");
             [self.centralManager retrieveConnectedPeripherals];
             break;
        case CBCentralManagerStatePoweredOff:
             ...etc.
}

Also, be aware that CBCentralManager works for iOS devices with Bluetooth 4.0. Currently, Bluetooth 4 is installed on iPhone 4S/5, iPod 4, and iPad 3/4/mini. So, be aware that your App will not work on iPad 1/2, iPhone 2/3, and iPod 2/3.

Still figuring this out, but it is also possible that you may not see your keyboard because it is not a Bluetooth 4 device. Let me know what you find out.

这篇关于(IOS)如何检查一个iPhone的配对的蓝牙设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆