![](/img/trans.png)
[英]How to scan 'Lower Energy Bluetooth Device' in minimize mode by using Core Bluetooth Framework?
[英]how can scan bluetooth devices using core bluetooth sdk?
我目前正在创建一个可以使用蓝牙设备的iPhone应用程序(Xcode 6.2,IOS 8.2)! 该应用程序的主要目标是仅搜索可用的蓝牙设备,并且每当您从蓝牙设备超出范围时,都会弹出一条警报消息。当您进入范围时,它将自动连接。
我在这里看到的唯一解决方案(将我的应用保留在AppStore上)是尝试扫描可用的蓝牙设备!
我尝试使用CoreBluetooth框架,但未获得可用设备的列表! 我想在显示器上打我的头,对此问题我感到非常沮丧。
我的代码在这里:
#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方法成功调用,但是仅显示一个设备,我向您显示日志:
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;
}
我试图在过去两天找到另一台设备。
任何意见,将不胜感激!
谢谢!
您的代码看起来正确。
关于为什么第二个设备可能不显示的一些提示:
如果您发现第二个设备使用另一个第三方iOS蓝牙扫描应用程序(例如LightBlue) ,则它可能已连接到该设备(如果您点击它以获取更多详细信息)。 连接外围设备时,它通常会停止播发广告(建立连接通常是播发广告的唯一原因)。 如果该设备不再做广告,则无法通过扫描找到它。 如果是这种情况,并且该设备已经连接到iOS设备,则可以使用CBCentralManager
的方法-retrieveConnectedPeripheralsWithServices:
或-retrieveConnectedPeripherals
(自iOS 7起不推荐使用)来获取已经由另一个应用程序连接到iOS的外围设备。
外设的广告发布速度可能很慢,iOS设备发现它花费的时间非常长(而且有点运气)。 在这种情况下,请尝试在外围设备上选择一个较短的广告间隔(假设您控制外围设备的固件)。
请注意,如果只需要扫描其他外围设备,则不需要CBPeripheralManager
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.