在iOS蓝牙外围设备上查找TxPower

在iOS蓝牙外围设备上查找TxPower,ios,swift,bluetooth,bluetooth-lowenergy,core-bluetooth,Ios,Swift,Bluetooth,Bluetooth Lowenergy,Core Bluetooth,我正在构建一个iOS应用程序(在Swift中),它同时充当核心蓝牙外围设备和中心设备。我需要确定外设的txPower,但我不一定需要将它发送到另一个设备的中央(尽管那会很好);我很乐意只确定发送设备本身的外围设备的txPower 我知道我可以从特定于制造商的数据中确定txPower,但我无法确定如何读取设备的特定于制造商的数据,而且我的central接收到的广告数据只包括外围设备的UUID,而不是特定于制造商的数据 以下是我设置外围设备的方式: peripheral = CBPeripheral

我正在构建一个iOS应用程序(在Swift中),它同时充当核心蓝牙外围设备和中心设备。我需要确定外设的txPower,但我不一定需要将它发送到另一个设备的中央(尽管那会很好);我很乐意只确定发送设备本身的外围设备的txPower

我知道我可以从特定于制造商的数据中确定txPower,但我无法确定如何读取设备的特定于制造商的数据,而且我的central接收到的广告数据只包括外围设备的UUID,而不是特定于制造商的数据

以下是我设置外围设备的方式:

peripheral = CBPeripheralManager(delegate: self, queue: nil)
myService = CBMutableService(type: CBUUID(string: "109F17E4-EF68-43FC-957D-502BB0EFCF47"), primary: true)
peripheral.addService(myService)
以下是我如何设置我的中央数据库:

centralManager.scanForPeripheralsWithServices([myCustomServiceUUID], options: nil)
然后在PeripheraldManagerDipDateState中:

    if peripheral.state == CBPeripheralManagerState.PoweredOn {

        self.peripheral.startAdvertising([CBAdvertisementDataServiceUUIDsKey: [myService.UUID] ])}
。。。在CentralManagerDipDateState中:

    if central.state == CBCentralManagerState.PoweredOn {

        centralManager.scanForPeripheralsWithServices([myCustomServiceUUID], options: [
            CBCentralManagerScanOptionAllowDuplicatesKey : NSNumber(bool: true)
            ])}
DidDiscoveryPeripheral委托中生成的advertisementData NSDictionary为:

["kCBAdvDataServiceUUIDs": (
"109F17E4-EF68-43FC-957D-502BB0EFCF47"), "kCBAdvDataIsConnectable": 1]

同样,任何允许我在外设的广告包中添加特定于制造商的数据,或允许我在发送设备上读取外设特定于制造商的数据或txPower的解决方案都将非常棒。非常感谢

根据我的经验,使用CoreBluetooth API无法从其他iOS外围设备获取TX功率级别或任何制造商数据,因为它会从您在DidDiscovery外围设备回调中收到的扫描结果中删除。但是,您可以获得外设的RSSI值,也许您可以根据您想要实现的目标来使用它

但我不确定您希望如何根据制造商数据确定TX功率水平。BLE规范为其定义了一个单独的特性


希望这有帮助

根据我的经验,使用CoreBluetooth API无法从其他iOS外围设备获取TX功率级别或任何制造商数据,因为它会从您在DidDiscovery外围设备回调中收到的扫描结果中删除。但是,您可以获得外设的RSSI值,也许您可以根据您想要实现的目标来使用它

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
但我不确定您希望如何根据制造商数据确定TX功率水平。BLE规范为其定义了一个单独的特性

希望这有帮助

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
使用此方法,您可以获得外围设备正在广告的所有数据

试着看看你得到了什么回应。这将允许您读取数据

NSString * abc = [RSSI stringValue] ;

NSLog(@" \n I see an advertisement with \n identifer: %@, \n state: %@ , \n name: %@, \n services: %@, \n RSSI : %@ \n ,\n description: %@ \n",
          [peripheral identifier],
          [peripheral state],
          [peripheral name],
          [peripheral services],
          abc,
          [advertisementData description]);
在advertisementData描述日志中,您将获得具有如下描述值的密钥:

{
    kCBAdvDataIsConnectable = 1;
    kCBAdvDataLocalName = givenName;
    kCBAdvDataManufacturerData = <given data>;
    kCBAdvDataServiceUUIDs = (0536);
    kCBAdvDataTxPowerLevel = some value ;
}
使用此方法,您可以获得外围设备正在广告的所有数据

试着看看你得到了什么回应。这将允许您读取数据

NSString * abc = [RSSI stringValue] ;

NSLog(@" \n I see an advertisement with \n identifer: %@, \n state: %@ , \n name: %@, \n services: %@, \n RSSI : %@ \n ,\n description: %@ \n",
          [peripheral identifier],
          [peripheral state],
          [peripheral name],
          [peripheral services],
          abc,
          [advertisementData description]);
在advertisementData描述日志中,您将获得具有如下描述值的密钥:

{
    kCBAdvDataIsConnectable = 1;
    kCBAdvDataLocalName = givenName;
    kCBAdvDataManufacturerData = <given data>;
    kCBAdvDataServiceUUIDs = (0536);
    kCBAdvDataTxPowerLevel = some value ;
}

我正在创建代码以查找设备的距离。而且我在广告中得到的数据很少。不是KCBADVDATAXPOWERLLEVEL键。那么如何找到TxPower值呢?你弄明白了吗?或者有其他方法可以找到它吗?我正在创建代码来查找设备的距离。而且我在广告中得到的数据很少。不是KCBADVDATAXPOWERLLEVEL键。那么如何找到TxPower值呢?你弄明白了吗?或任何其他方式找到它?应选择答案!谢谢你,兄弟!应该选择答案!谢谢你,兄弟!