簡體   English   中英

BLE:如何獲得正確的服務 UUID 和特征 UUID?

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

我是藍牙連接的新手,我想在我的 iOS 項目中添加第二個設備。 我已經有一個設備,新設備與第一個設備非常相似,但有點不同。 我有兩個設備的一個進程,我沒有更改很多代碼,只是為新設備創造了所有價值。 我的所有設備都有不同的名稱和標識符,第一個設備工作正常。

為了創建 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()
    }
}

}

來自日志的數據:

對於第一台設備:

  1. isBluetoothAvailable()
  2. 掃描()
  3. 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 外圍設備:CBPeripheral,advertisingData:[String:Any],rssi RSSI:NSNumber)
  5. 掃描()
  6. CentralManager(_中央:CBCentralManager,didDiscover 外圍設備:CBPeripheral,advertiseData:[String:Any],rssi RSSI:NSNumber)(外圍設備 <CBPeripheral:0x267hg5670,標識符 = FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901,名稱 = SecondDevice,Z9ED39E2EA931586B7EZ8 = EA931586B769A斷開連接>)
  7. 停止掃描()
  8. 連接(外設:CBPeripheral)(currentPeripheral nil,currentPeripheral nil)
  9. CentralManager(_central: CBCentralManager, didConnect 外圍設備: CBPeripheral
  10. centralManager(_:didConnect:) - didConnectPeripheral FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901 第二個設備
  11. savedPeripheralString FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901 第二個設備
  12. 藍牙 stop() currentPeripheral Optional(<CBPeripheral: 0x2jk875fe0, identifier = FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901, name = SecondDevice, state = connected>)
  13. 禁用睡眠()
  14. 外圍設備(_外圍設備:CBPeripheral,didDiscoverServices 錯誤:錯誤?)
  15. peripheral(_:didDiscoverServices:) - 外圍設備的 didDiscoverServices FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901
  16. 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. 外圍設備(_:didDiscoverServices :) - didDiscoverServices SecondDevice 59DE3994-6A63-4654-8FF0-F85C5163B2F5
  18. 外圍設備(_:didDiscoverServices :) - 未找到外圍設備的 didDiscoverServices FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901

對於第二個設備:

  1. isBluetoothAvailable()
  2. 掃描()
  3. CentralManager(_ 中央:CBCentralManager,didDiscover 外圍設備:CBPeripheral,廣告數據:[String:Any],rssi RSSI:NSNumber)(外圍設備 <CBPeripheral:0xh6789a40,標識符 = 98HG761CE-56C3-K767-26HJ-E51BA678Gh56,名稱 = DeviceOne,Z9ED39E2EA931586B56EZA98 =斷開連接)
  4. 掃描()
  5. CentralManager(_ 中央:CBCentralManager,didDiscover 外圍設備:CBPeripheral,廣告數據:[String:Any],rssi RSSI:NSNumber)(外圍設備 <CBPeripheral:0x678jhs0,標識符 = H56KIL35-7835-7JKL-2B11-HJKLIYTAA400,名稱 = ThirdDevice,Z9ED39E2EA93124EF573E6斷開連接)
  6. CentralManager(_central:CBCentralManager,didDiscover 外圍設備:CBPeripheral,advertisingData:[String:Any],rssi RSSI:NSNumber)
  7. 停止掃描()
  8. 連接(外設:CBPeripheral) currentPeripheral nil,currentPeripheral nil
  9. CentralManager(_central: CBCentralManager, didConnect 外圍設備: CBPeripheral
  10. centralManager(_:didConnect:) - didConnectPeripheral H56KIL35-7835-7JKL-2B11-HJKLIYTAA400
  11. 已保存外設字符串 H56KIL35-7835-7JKL-2B11-HJKLIYTAA400
  12. 藍牙 stop() - currentPeripheral Optional(<CBPeripheral: 0x78jkl680, identifier = H56KIL35-7835-7JKL-2B11-HJKLIYTAA400, name = ThirdDevice, state = connected>)
  13. 禁用睡眠()
  14. 外圍設備(_外圍設備:CBPeripheral,didDiscoverServices 錯誤:錯誤?)
  15. peripheral(_:didDiscoverServices:) - 外圍設備 H56KIL35-7835-7JKL-2B11-HJKLIYTAA400 的 didDiscoverServices
  16. **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. 外圍設備(_:didDiscoverServices :) - didDiscoverServices ThirdDevice 59DE3994-6A63-4654-8FF0-F85C5163B2F5 - 與 secondDevice 相同
  18. 外圍設備(_:didDiscoverServices :) - 未找到外圍設備的 didDiscoverServices H56KIL35-7835-7JKL-2B11-HJKLIYTAA400 **

根據連接到我的第一個服務 UUID 和第一個特征 UUID 的第 16 步應用程序中第二個設備的日志信息,這是錯誤的!

你知道嗎,我創建的 UUID 是否正確?

PS:Android 應用程序適用於兩種設備。

非常感謝!

為多個 BLE 設備接收相同的服務和特征 UUID 是完全正常的,僅意味着所有設備都提供完全相同的服務。

例如:如果您有兩個測量一個人的心率的設備,例如智能手表,兩個設備可能都提供具有相同 UUID 的心率服務。

如果您想區分設備,可以使用您在提供的日志的第 3 步中收到的標識符

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM