简体   繁体   English

BLE:如何获得正确的服务 UUID 和特征 UUID?

[英]BLE: How to get correct service UUID and characteristic UUID?

I am a new in Bluetooth connection and I want to add second device in my iOS project.我是蓝牙连接的新手,我想在我的 iOS 项目中添加第二个设备。 I already have one device, and the new device is very similar with first one, but little bit deferent.我已经有一个设备,新设备与第一个设备非常相似,但有点不同。 I have one process for the two devices, and I did not change a lot of code, just created all value for the new device.我有两个设备的一个进程,我没有更改很多代码,只是为新设备创造了所有价值。 My all devices have different name and identifier, first device is working fine.我的所有设备都有不同的名称和标识符,第一个设备工作正常。

For creating UUID values, I used UUID generator ( https://www.guidgenerator.com/online-guid-generator.aspx ).为了创建 UUID 值,我使用了 UUID 生成器 ( https://www.guidgenerator.com/online-guid-generator.aspx )。

class BleConstants: NSObject {
let deviceTwoServiceUUID = “59DE3994-6A63-4654-8FF0-F85C5163B2F5”
let deviceTwoFirstCharacteristicUUID = “59DE3994-6A63-4654-8FF0-F85C5163B2F6”
let deviceTwoSecondCharacteristicUUID = “59DE3994-6A63-4654-8FF0-F85C5163B2F7”
let deviceOneServiceUUID = “A6AF4483-E210-457B-B9D6-B8A621513D1D”
let deviceOneFirstCharacteristicUUID = “A6AF4483-E210-457B-B9D6-B8A621513D2D”
let deviceOneSecondCharacteristicUUID = “A6AF4483-E210-457B-B9D6-B8A621513D2D”
}

class BleManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
@objc private(set) static var sharedInstance = BleManager()
var cbManager : CBCentralManager? = nil
var currentPeripheral : CBPeripheral? = nil
var secondService : CBService? = nil
var firstService : CBService? = nil
var secondFirstCharacteristic : CBCharacteristic!
var secondSecondCharacteristic : CBCharacteristic!
var firstFirstCharacteristic : CBCharacteristic!
var firstSecondCharacteristic : CBCharacteristic!

func initCentralManager() {
    if cbManager == nil {
        cbManager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionRestoreIdentifierKey : “MyApp”, CBCentralManagerOptionShowPowerAlertKey: true])            
    }

func deinitCentralManager() {
    cbManager = nil
}

func isBluetoothAvailable() -> Bool {
    return cbManager?.state == CBManagerState.poweredOn
}

func scan() {
    if (cbManager != nil && (cbManager?.isScanning)!) {
        return
    }
    
    discoveredPeripherals.removeAll()
    
    let serviceUUIDs = [CBUUID(string: BleConstants.deviceTwoServiceUUID), CBUUID(string: BleConstants.deviceOneServiceUUID)]
    
    cbManager?.scanForPeripherals(withServices: serviceUUIDs,
                                  options: [CBCentralManagerScanOptionAllowDuplicatesKey : 1])
}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
    if(!discoveredPeripherals.contains(peripheral)) {
        discoveredPeripherals.append(peripheral)
    }
}

func stopScan() {
    if cbManager != nil && (cbManager?.isScanning)! {
        cbManager?.stopScan()
    }
}

func connect(peripheral: CBPeripheral) {
    if cbManager?.state == CBManagerState.poweredOn {
        if currentPeripheral == nil || currentPeripheral?.state != CBPeripheralState.connected {
            cbManager?.connect(peripheral, options: nil)
        } else {
            cbManager?.cancelPeripheralConnection(peripheral)
        }
    }
}

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
Device.savePeripheralString(peripheral: peripheral.identifier.uuidString)
    AutoConnect.stop()
DeviceUpdate.updateProgress = .None
Device.isDongleConnected = true
currentPeripheral = peripheral
currentPeripheral?.delegate = self
currentPeripheral?.discoverServices(nil)
disableSleep()
}

func disableSleep() {
    UIApplication.shared.isIdleTimerDisabled = true
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
 if error != nil {
        return
    }

    if let services = peripheral.services {

        for service in services {
            if service.uuid.uuidString == BleConstants.deviceTwoServiceUUID {
        Device.dongleType = port.second
                    secondService = service
                peripheral.discoverCharacteristics(nil, for: service)
        }
            if service.uuid.uuidString == BleConstants.deviceOneServiceUUID {
        Device.dongleType = port.first
                firstService = service
                peripheral.discoverCharacteristics(nil, for: service)
            } else {
                Log.bt("didDiscoverServices for peripheral not found \(peripheral.identifier.uuidString)")
            }
        }
    }
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {

    for characteristic in service.characteristics! {
        if characteristic.uuid.uuidString == BleConstants.deviceOneFirstCharacteristicUUID {
     firstCharacteristic = characteristic
        }  
    else if characteristic.uuid.uuidString == BleConstants.deviceOneSecondCharacteristicUUID {
           firstSecondCharacteristic = characteristic
        else if characteristic.uuid.uuidString == BleConstants.deviceTwoFirstCharacteristicUUID {
            secondFirstCharacteristic = characteristic
        } else if characteristic.uuid.uuidString == BleConstants.deviceTwoSecondCharacteristicUUID {
    secondSecondCharacteristic = characteristic
        } else {
            Log.bt("didDiscoverCharacteristics not found \(characteristic.uuid.uuidString)")
        }
    }
    
    if Device.dongleType == .deviceTwo {
         openPortDeviceTwo()
    } else {
        openPortDeviceOne()
    }
}

} }

Data from logs:来自日志的数据:

For first device:对于第一台设备:

  1. isBluetoothAvailable() isBluetoothAvailable()
  2. scan()扫描()
  3. CentralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) (peripheral <CBPeripheral: 0x267hg5670, identifier = 98HG761CE-56C3-K767-26HJ-E51BA678Gh56, name = DeviceOne, state = disconnected) CentralManager(_中央:CBCentralManager,didDiscover 外围设备:CBPeripheral,advertiseData:[String:Any],rssi RSSI:NSNumber)(外围设备 <CBPeripheral:0x267hg5670,标识符 = 98HG761CE-56C3-K767-26HJ-E51BA678Gh56,名称 = DeviceOne,Z9ED39E2EA931586B3EZ6 =531586B3EZ6断开连接)
  4. CentralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) CentralManager(_central:CBCentralManager,didDiscover 外围设备:CBPeripheral,advertisingData:[String:Any],rssi RSSI:NSNumber)
  5. scan()扫描()
  6. CentralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) (peripheral <CBPeripheral: 0x267hg5670, identifier = FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901, name = SecondDevice, state = disconnected>) CentralManager(_中央:CBCentralManager,didDiscover 外围设备:CBPeripheral,advertiseData:[String:Any],rssi RSSI:NSNumber)(外围设备 <CBPeripheral:0x267hg5670,标识符 = FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901,名称 = SecondDevice,Z9ED39E2EA931586B7EZ8 = EA931586B769A断开连接>)
  7. stopScan()停止扫描()
  8. connect(peripheral: CBPeripheral) (currentPeripheral nil, currentPeripheral nil)连接(外设:CBPeripheral)(currentPeripheral nil,currentPeripheral nil)
  9. CentralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral CentralManager(_central: CBCentralManager, didConnect 外围设备: CBPeripheral
  10. centralManager(_:didConnect:) - didConnectPeripheral FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901 Second device centralManager(_:didConnect:) - didConnectPeripheral FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901 第二个设备
  11. savedPeripheralString FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901 Second device savedPeripheralString FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901 第二个设备
  12. Bluetooth stop() currentPeripheral Optional(<CBPeripheral: 0x2jk875fe0, identifier = FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901, name = SecondDevice, state = connected>)蓝牙 stop() currentPeripheral Optional(<CBPeripheral: 0x2jk875fe0, identifier = FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901, name = SecondDevice, state = connected>)
  13. disableSleep()禁用睡眠()
  14. peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)外围设备(_外围设备:CBPeripheral,didDiscoverServices 错误:错误?)
  15. peripheral(_:didDiscoverServices:) - didDiscoverServices for peripheral FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901 peripheral(_:didDiscoverServices:) - 外围设备的 didDiscoverServices FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901
  16. peripheral(_:didDiscoverServices:) - didDiscoverServices for peripheral.services Optional([<CBService: 0x2jkki2dc0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5>, <CBService: 0xhj67c240, isPrimary = YES, UUID = Device Information>]) [<CBService: 0x5678f2dc0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5>, <CBService: 0x28186c240, isPrimary = YES, UUID = Device Information>], peripheral.services is Optional([<CBService: 0xhjy62dc0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5>, <CBService: 0x2hju7240, isPrimary = YES, UUID = Device Information>])) service.uuid.uuidString is 59DE3994-6A63-4654-8FF0-F85C5163B2F5 and services is [<CBService: 0xhj7892dc0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5>, <CBService: 0xhjk678340, isPrimary = YES, UUID = Device Information>] peripheral(_:didDiscoverServices:) - didDiscoverServices for peripheral.services 可选([<CBService: 0x2jkki2dc0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5>, <CBService: 0xhj67c240, isPrimary = YES, UUID = Device信息>]) [<CBService: 0x5678f2dc0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5>, <CBService: 0x28186c240, isPrimary = YES, UUID = Device Information>], peripheral.services 是可选的([ <CBService: 0xhjy62dc0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5>, <CBService: 0x2hju7240, isPrimary = YES, UUID = Device Information>])) service.uuid.uuidString 为 59DE3994-6A63-4654 -8FF0-F85C5163B2F5 和服务是 [<CBService: 0xhj7892dc0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5>, <CBService: 0xhjk678340, isPrimary = YES, UUID = Device Information>]
  17. peripheral(_:didDiscoverServices:) - didDiscoverServices SecondDevice 59DE3994-6A63-4654-8FF0-F85C5163B2F5外围设备(_:didDiscoverServices :) - didDiscoverServices SecondDevice 59DE3994-6A63-4654-8FF0-F85C5163B2F5
  18. peripheral(_:didDiscoverServices:) - didDiscoverServices for peripheral not found FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901外围设备(_:didDiscoverServices :) - 未找到外围设备的 didDiscoverServices FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901

For second device:对于第二个设备:

  1. isBluetoothAvailable() isBluetoothAvailable()
  2. scan()扫描()
  3. CentralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) (peripheral <CBPeripheral: 0xh6789a40, identifier = 98HG761CE-56C3-K767-26HJ-E51BA678Gh56, name = DeviceOne, state = disconnected) CentralManager(_ 中央:CBCentralManager,didDiscover 外围设备:CBPeripheral,广告数据:[String:Any],rssi RSSI:NSNumber)(外围设备 <CBPeripheral:0xh6789a40,标识符 = 98HG761CE-56C3-K767-26HJ-E51BA678Gh56,名称 = DeviceOne,Z9ED39E2EA931586B56EZA98 =断开连接)
  4. scan()扫描()
  5. CentralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) (peripheral <CBPeripheral: 0x678jhs0, identifier = H56KIL35-7835-7JKL-2B11-HJKLIYTAA400, name = ThirdDevice, state = disconnected) CentralManager(_ 中央:CBCentralManager,didDiscover 外围设备:CBPeripheral,广告数据:[String:Any],rssi RSSI:NSNumber)(外围设备 <CBPeripheral:0x678jhs0,标识符 = H56KIL35-7835-7JKL-2B11-HJKLIYTAA400,名称 = ThirdDevice,Z9ED39E2EA93124EF573E6断开连接)
  6. CentralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) CentralManager(_central:CBCentralManager,didDiscover 外围设备:CBPeripheral,advertisingData:[String:Any],rssi RSSI:NSNumber)
  7. stopScan()停止扫描()
  8. connect(peripheral: CBPeripheral) currentPeripheral nil, currentPeripheral nil连接(外设:CBPeripheral) currentPeripheral nil,currentPeripheral nil
  9. CentralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral CentralManager(_central: CBCentralManager, didConnect 外围设备: CBPeripheral
  10. centralManager(_:didConnect:) - didConnectPeripheral H56KIL35-7835-7JKL-2B11-HJKLIYTAA400 centralManager(_:didConnect:) - didConnectPeripheral H56KIL35-7835-7JKL-2B11-HJKLIYTAA400
  11. savedPeripheralString H56KIL35-7835-7JKL-2B11-HJKLIYTAA400已保存外设字符串 H56KIL35-7835-7JKL-2B11-HJKLIYTAA400
  12. Bluetooth stop() - currentPeripheral Optional(<CBPeripheral: 0x78jkl680, identifier = H56KIL35-7835-7JKL-2B11-HJKLIYTAA400, name = ThirdDevice, state = connected>)蓝牙 stop() - currentPeripheral Optional(<CBPeripheral: 0x78jkl680, identifier = H56KIL35-7835-7JKL-2B11-HJKLIYTAA400, name = ThirdDevice, state = connected>)
  13. disableSleep()禁用睡眠()
  14. peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)外围设备(_外围设备:CBPeripheral,didDiscoverServices 错误:错误?)
  15. peripheral(_:didDiscoverServices:) - didDiscoverServices for peripheral H56KIL35-7835-7JKL-2B11-HJKLIYTAA400 peripheral(_:didDiscoverServices:) - 外围设备 H56KIL35-7835-7JKL-2B11-HJKLIYTAA400 的 didDiscoverServices
  16. **peripheral(_:didDiscoverServices:) - didDiscoverServices for peripheral.services Optional([<CBService: 0xlki8901c0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5 - same like for secondDevice>, <CBService: 0x281111180, isPrimary = YES, UUID = Device Information>]) services is [<CBService: 0x2827444c0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5 - same like for secondDevice>, <CBService: 0x282744580, isPrimary = YES, UUID = Device Information>], peripheral.services is Optional([<CBService: 0xlki8901c0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5 - same like for secondDevice>, <CBService: 0x281111180, isPrimary = YES, UUID = Device Information>])) service.uuid.uuidString is 59DE3994-6A63-4654-8FF0-F85C5163B2F5 - same like for secondDeviceand services is [<CBService: 0x27893kdc0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5 - same like for secondDevice>, <CBService: 0x679kh8580, isPrimary = YES, UUID = Device Information>] **peripheral(_:didDiscoverServices:) - 用于 peripheral.services 的 didDiscoverServices 可选([<CBService: 0xlki8901c0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5 - 与 secondDevice> 相同,<CBService: 0x281111180, isPrimary = YES, UUID = Device Information>]) 服务为 [<CBService: 0x2827444c0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5 - 与 secondDevice 相同>, <CBService: 0x282744580, isPrimary = YES, UUID = 设备信息>], peripheral.services 是可选的([<CBService: 0xlki8901c0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0-F85C5163B2F5 - 与 secondDevice> 相同, <CBService: 0x281111180, isPrimary = YES, UUID = 设备信息>])) service.uuid.uuidString 为 59DE3994-6A63-4654-8FF0-F85C5163B2F5 - 与 secondDevice 和服务相同,为 [<CBService: 0x27893kdc0, isPrimary = YES, UUID = 59DE3994-6A63-4654-8FF0- F85C5163B2F5 - 与 secondDevice> 相同,<CBService: 0x679kh8580, isPrimary = YES, UUID = Device Information>]
  17. peripheral(_:didDiscoverServices:) - didDiscoverServices ThirdDevice 59DE3994-6A63-4654-8FF0-F85C5163B2F5 - same like for secondDevice外围设备(_:didDiscoverServices :) - didDiscoverServices ThirdDevice 59DE3994-6A63-4654-8FF0-F85C5163B2F5 - 与 secondDevice 相同
  18. peripheral(_:didDiscoverServices:) - didDiscoverServices for peripheral not found H56KIL35-7835-7JKL-2B11-HJKLIYTAA400**外围设备(_:didDiscoverServices :) - 未找到外围设备的 didDiscoverServices H56KIL35-7835-7JKL-2B11-HJKLIYTAA400 **

Based on logs information for second device in 16th step app connected to my first service UUID and first characteristic UUID, that is wrong!根据连接到我的第一个服务 UUID 和第一个特征 UUID 的第 16 步应用程序中第二个设备的日志信息,这是错误的!

Do you have idea, Did I create UUIDs correct?你知道吗,我创建的 UUID 是否正确?

PS: Android app works fine with both devices. PS:Android 应用程序适用于两种设备。

Thanks a lot!非常感谢!

Receiving the same service and characteristic UUID for multiple BLE devices is perfectly normal and only means that all the devices offer exactly the same service.为多个 BLE 设备接收相同的服务和特征 UUID 是完全正常的,仅意味着所有设备都提供完全相同的服务。

For example : If you have two devices which measure the heart rate of a person, eg a smartwatch, both devices might offer the heart rate service with the same UUID.例如:如果您有两个测量一个人的心率的设备,例如智能手表,两个设备可能都提供具有相同 UUID 的心率服务。

If you want to differentiate between the device you can use the identifier which you received in step 3 of your provided log.如果您想区分设备,可以使用您在提供的日志的第 3 步中收到的标识符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM