如何使用核心蓝牙SDK扫描蓝牙设备? [英] how can scan bluetooth devices using core bluetooth sdk?

查看:148
本文介绍了如何使用核心蓝牙SDK扫描蓝牙设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在创建一个可以使用蓝牙设备的iPhone应用程序(Xcode 6.2,IOS 8.2)!此应用程序的主要目标是仅搜索可用的蓝牙设备,并且每当您从蓝牙设备进入范围外时,都会弹出一条警报消息.当您进入范围时,它会自动连接.

I'm currently creating an iPhone app (Xcode 6.2, IOS 8.2) that could use Bluetooth devices! Main goal of this application is only search the available bluetooth devices and whenever you go to out of range from bluetooth devices one alert message pop up.and when ever you come in range automatically connected.

我在这里看到的唯一解决方案(将我的应用保留在AppStore上)是尝试扫描可用的蓝牙设备!

The only solution I see here (to keep my app on AppStore) is to try scan for available bluetooth devices!

我尝试使用CoreBluetooth框架,但没有获得可用设备的列表! 我想在显示器上打我的头,对此问题我感到非常沮丧.

I tried to use CoreBluetooth framework, but I don't get list of available devices! I wants to hit my head on my monitor i am very frustrated with this problem.

我的代码在这里:

 #import "ViewController.h"
 #import <CoreBluetooth/CoreBluetooth.h>
 #import <CoreLocation/CoreLocation.h>

 @interface ViewController ()     <CBCentralManagerDelegate,CBPeripheralDelegate,CBPeripheralManagerDelegate,UITableViewDataSource,UITableViewDelegate,CLLocationManagerDelegate>
{
CBCentralManager *mgr;
CBPeripheralManager *manager;

 }

- (void)viewDidLoad {

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

mgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
manager = [[CBPeripheralManager alloc]initWithDelegate:self queue:nil];

}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
NSString *messtoshow;

switch (central.state) {
    case CBCentralManagerStateUnknown:
    {
        messtoshow=[NSString stringWithFormat:@"State unknown, update imminent."];
        break;
    }
    case CBCentralManagerStateResetting:
    {
        messtoshow=[NSString stringWithFormat:@"The connection with the system service was momentarily lost, update imminent."];
        break;
    }
    case CBCentralManagerStateUnsupported:
    {
        messtoshow=[NSString stringWithFormat:@"The platform doesn't support Bluetooth Low Energy"];
        break;
    }
    case CBCentralManagerStateUnauthorized:
    {
        messtoshow=[NSString stringWithFormat:@"The app is not authorized to use Bluetooth Low Energy"];
        break;
    }
    case CBCentralManagerStatePoweredOff:
    {
        messtoshow=[NSString stringWithFormat:@"Bluetooth is currently powered off."];
         NSLog(@"%@",messtoshow);
        break;
    }
    case CBCentralManagerStatePoweredOn:
    {

        messtoshow=[NSString stringWithFormat:@"Bluetooth is currently powered on and available to use."];

        [mgr scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey :@YES}];

        NSLog(@"%@",messtoshow);
        break;

    }
}
}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {


NSLog(@"%@",[NSString stringWithFormat:@"%@",[advertisementData description]]);

NSLog(@"%@",[NSString stringWithFormat:@"Discover:%@,RSSI:%@\n",[advertisementData objectForKey:@"kCBAdvDataLocalName"],RSSI]);
NSLog(@"Discovered %@", peripheral.name);
[mgr  connectPeripheral:peripheral options:nil];
}

didDiscoverPeripheral方法成功调用,但是仅显示一个设备,我向您显示日志:

didDiscoverPeripheral method successfully called But shows only one device,i show you Log:

2015-03-27 17:10:48.137 StanBluetooth[853:109794] Discover:(null),RSSI:-68

2015-03-27 17:10:48.138 StanBluetooth[853:109794] Discovered peripheral E876A182-9DC1-26B4-3C89-DD20F5EAE88B

2015-03-27 17:10:48.139 StanBluetooth[853:109794] Discovered Amit’s MacBook Air

2015-03-27 17:10:48.140 StanBluetooth[853:109794] {
kCBAdvDataIsConnectable = 1;
}

我试图在过去两天找到另一台设备.

i am trying to find another device last 2 days.

任何建议将不胜感激!

谢谢!

推荐答案

您的代码看起来正确.

关于为什么第二个设备可能不显示的一些提示:

Some hints on why the second device might not appear:

  • 如果您发现第二个设备使用的是另一个第三方iOS蓝牙扫描应用程序,例如

  • If you found the second device using another 3rd party iOS Bluetooth scanning app like LightBlue, it might have connected to that device (if you tapped on it to get more details). When a peripheral device is connected to, very often, it stops advertising (connection establishment is usually the sole reason for advertising). If the device is not advertising any more, you cannot find it by scanning. If this is the case and the device is already connected to the iOS device, you could use CBCentralManager's methods -retrieveConnectedPeripheralsWithServices: or -retrieveConnectedPeripherals (deprecated since iOS 7) to get the peripherals that have already been connected to iOS by another app.

外围设备的发布速度可能很慢,以至于iOS设备需要花费很长的时间(有点运气)才能找到广告.在这种情况下,请尝试在外围设备上选择一个较短的广告间隔(假设您控制外围设备的固件).

The peripheral might be advertising at such a slow rate, that it just takes a really long time (and a bit of luck) to be found by the iOS device. If this is the case, try picking a shorter advertising interval on your peripheral (assuming you control the firmware of the peripheral).

请注意,如果只需要扫描其他外围设备,则不需要CBPeripheralManager.

Note you do not need the CBPeripheralManager if you only need to scan for other peripherals.

这篇关于如何使用核心蓝牙SDK扫描蓝牙设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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