蓝牙低功耗iOS如何获得服务名称

时间:2014-03-17 10:51:32

标签: ios iphone objective-c bluetooth-lowenergy

我有一台带有一些服务的蓝牙设备。我能够将我的应用程序与此设备连接,并且我能够看到我的BT设备的所有服务,但我希望看到各种服务的名称,例如:

Device Information 
UUID : 0x180A

我认为这是可能的,因为某些通用应用程序允许您查看它。

1 个答案:

答案 0 :(得分:0)

按照Apple tutorial,非常明确:

从:

开始
@interface ViewController () <CBCentralManagerDelegate, CBPeripheralDelegate>

//centralManager:willRestoreState:
@property (nonatomic,strong) CBPeripheral     *connectingPeripheral;

@end

@implementation ViewController
{
    CBCentralManager *centralManager;
}

然后:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Starting Up a Central Manager
    centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil
                                                    options: nil]; 
}

initWithDelegate:queue:options:通过CBCentralManagerDelegate调用:

- (void) centralManagerDidUpdateState:(CBCentralManager *)central
{
    [centralManager scanForPeripheralsWithServices:nil //Services
                                           options:nil];
}

scanForPeripheralsWithServices:options:通过CBCentralManagerDelegate调用:

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

    self.connectingPeripheral = peripheral;

    //You should connect to your BLE Device with:
    [centralManager connectPeripheral:self.connectingPeripheral
                              options: nil]
}

此调用的响应是通过委托connectPeripheral:options:

然后:

- (void) centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    //(...)
    [peripheral discoverServices:nil]; // Service
}

discoverServices:调用peripheral:didDiscoverServices:来自CBPeripheralDelegate

- (void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{

    for (CBService *service in peripheral.services)
    {
        NSLog(@"Discovered service %@", service);
        NSLog(@"Discovered service %@", service.UUID);
    }
}

你有你想要的东西!

了解CBService

的属性非常重要

识别服务

  • UUID属性
  • 周边财产
  • isPrimary property

访问服务数据

  • 特征属性
  • includedServices属性