how can scan bluetooth devices using core bluetooth sdk?

无人久伴 提交于 2020-01-14 04:06:19

问题


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.

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

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.

My code is here:

 #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 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.

any advice would be appreciated!

thanks!


回答1:


Your code looks correct.

Some hints on why the second device might not appear:

  • 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.

  • 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).

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



来源:https://stackoverflow.com/questions/29299592/how-can-scan-bluetooth-devices-using-core-bluetooth-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!