蓝牙低能耗iOS如何获取服务名称

蓝牙低能耗iOS如何获取服务名称,ios,iphone,objective-c,bluetooth-lowenergy,Ios,Iphone,Objective C,Bluetooth Lowenergy,我有一个蓝牙设备和一些服务。我可以将我的应用程序连接到此设备,并且可以查看我的BT设备的所有服务,但我想查看各种服务的名称,例如: Device Information UUID : 0x180A 我认为这是可能的,因为一些通用的应用程序允许您查看它。请按照下面的说明进行操作,很明显: 首先是: @interface ViewController () <CBCentralManagerDelegate, CBPeripheralDelegate> //centralManag

我有一个蓝牙设备和一些服务。我可以将我的应用程序连接到此设备,并且可以查看我的BT设备的所有服务,但我想查看各种服务的名称,例如:

Device Information 
UUID : 0x180A
我认为这是可能的,因为一些通用的应用程序允许您查看它。

请按照下面的说明进行操作,很明显:

首先是:

@interface ViewController () <CBCentralManagerDelegate, CBPeripheralDelegate>

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

@end

@implementation ViewController
{
    CBCentralManager *centralManager;
}
initWithDelegate:队列:选项:通过CBCentralManagerDelegate调用:

- (void) centralManagerDidUpdateState:(CBCentralManager *)central
{
    [centralManager scanForPeripheralsWithServices:nil //Services
                                           options:nil];
}
- (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]
}
扫描外围设备服务:选项:通过CBCentralManagerDelegate呼叫:

- (void) centralManagerDidUpdateState:(CBCentralManager *)central
{
    [centralManager scanForPeripheralsWithServices:nil //Services
                                           options:nil];
}
- (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:调用外围设备:didDiscoverServices:来自CBPeripheralDelegate

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

    for (CBService *service in peripheral.services)
    {
        NSLog(@"Discovered service %@", service);
        NSLog(@"Discovered service %@", service.UUID);
    }
}
这就是你想要的

了解以下各项的属性非常重要:

识别服务

  • UUID属性
  • 外围属性
  • 原始财产
访问服务数据

  • 特性特性
  • includedServices属性