BLE:如何获得正确的服务UUID和特征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. scan()
  3. CentralManager(_ central:CBCentralManager,didDiscover外设:CBPeripheral,advertisemeData:[String:Any],rssi rssi:NSNumber)(外设<CBPeripheral:0x267hg5670,标识符=98HG761CE-56C3-K767-26HJ-E51BA678Gh56,名称=DeviceOne,状态=断开连接)
  4. CentralManager(_ central:CBCentralManager,didDiscover外设:CBPeripheral,advertisemeData:[String:Any],rssi rssi:NSNumber)
  5. scan()
  6. CentralManager(_ central:CBCentralManager,didDiscover外设:CBPeripheral,advertisemeData:[String:Any],rssi rssi:NSNumber)(外设<CBPeripheral:0x267hg5670,标识符=FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901,名称=SecondDevice,状态=断开连接>)
  7. stopScan()
  8. connect(外设:CBPeripheral)(currentPeripheral nil,currentPeripheral-nil)
  9. CentralManager(_ central:CBCentralManager,didConnect外设:CBPeripheral
  10. centralManager(_:didConnect:)-diConnectPeripheral FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901第二设备
  11. saved外围字符串FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901第二个设备
  12. 蓝牙停止()currentPeripheral可选(<CBPeripheral:0x2jk875fe0,标识符=FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901,名称=SecondDevice,状态=已连接>)
  13. disableSleep()
  14. 外设(_外设:CBPeripheral,didDiscoverServices错误:错误?)
  15. 外设(_:didDiscoverServices:)-用于外设FJ1478HJ-EH8J-6709-1FH0-1456HGJ0BC901的didDiscoveryServices
  16. 外设(_:didDiscoveryServices:)-用于外设的didDiscoverServices。服务可选([lt;CBService:0x2jkki2dc0,isPrimary=YES,UUID=59DE3994-6A63-4654-8FF0-F85C5163B2F5>,<CBService:0xhj67c240,isPrimare=YES,UUID=Device Information>])[lt;CBService:0x5678f2dc0,isPrimary=YES,UUID=59DE3994-6A63-4654-8FF0-F85C5163B2F5>,<CBService:0x28186c240,isPrimary=YES,UUID=设备信息>],外围设备服务是可选的([lt;CBAService:0xhjy62dc0,isPrimary=YES,UUID=59DE3994-6 A63-4654-8 FF0-F85C5163B2F5>;,&l特;CBService:0x2hju7240,isPrimary=YESservice.uuid.uuidString为59DE3994-6A63-4654-8FF0-F85C5163B2F5,服务为[lt;CBService:0xhj7892dc0,isPrimary=YES,uuid=59DE3994-6 A63-4654-8 FF0-F85C5163B2F5>,<CBService:0xhjk678340,isPrimare=YES,uuid=设备信息>]
  17. 外围设备(_:didDiscoveryServices:)-didDiscoverServices第二设备59DE3994-6A63-4654-8FF0-F85C5163B2F5
  18. 外设(_:didDiscoveryServices:)-未找到外设的didDiscoverServices FJ1478HJ-EH8J-6709-1FHO-1456HGJ0BC901

对于第二个设备:

  1. isBluetoothAvailable()
  2. scan()
  3. CentralManager(_ central:CBCentralManager,didDiscover外设:CBPeripheral,advertisemeData:[String:Any],rssi rssi:NSNumber)(外设<CBPeripheral:0xh6789a40,标识符=98HG761CE-56C3-K767-26HJ-E51BA678Gh56,名称=DeviceOne,状态=断开连接)
  4. scan()
  5. CentralManager(_ central:CBCentralManager,didDiscover外设:CBPeripheral,advertisemeData:[String:Any],rssi rssi:NSNumber)(外设<CBPeripheral:0x678jhs0,标识符=H56KIL35-7835-7JKL-2B11-HJKLIYTAA400,名称=ThirdDevice,状态=断开连接)
  6. CentralManager(_ central:CBCentralManager,didDiscover外设:CBPeripheral,advertisemeData:[String:Any],rssi rssi:NSNumber)
  7. stopScan()
  8. connect(外设:CBPeripheral)currentPeripheral nil,currentPeriphere nil
  9. CentralManager(_ central:CBCentralManager,didConnect外设:CBPeripheral
  10. centralManager(_:didConnect:)-diConnectPeripheral H56KIL35-7835-7JKL-2B11-HJKLIYTAA400
  11. saved外围字符串H56KIL35-7835-7JKL-2B11-HJKLIYTAA400
  12. 蓝牙停止()-currentPeripheral可选(<CBPeripheral:0x78jkl680,标识符=H56KIL35-7835-7JKL-2B11-HJKLIYTAA400,名称=ThirdDevice,状态=已连接>)
  13. disableSleep()
  14. 外设(_外设:CBPeripheral,didDiscoverServices错误:错误?)
  15. 外设(_:didDiscoverServices:)-用于外设H56KIL35-7835-7JKL-2B11-HJKLIYTAA400的didDiscoveryServices
  16. **外设(_:didDiscoveryServices:)-用于外设的didDiscoverServices。服务可选([<CBService:0xlki8901c0,isPrimary=YES,UUID=59DE3994-6A63-4654-8FF0-F85C5163B2F5-与第二设备相同>,<CBAService:0x2811111180,isPrimare=YES,UUID=设备信息>])服务是[<CBService:0x22827444c0,isPrimary=YES,UUID=59DE3994-6A63-4654-8FF0-F85C5163B2F5-与第二设备相同>,<CBAService:0x282744580,isPrimare=YES,UUID=设备信息>],peripheral.services是可选的([<CBService:0xlki8901c0,isPrimary=YES,UUID=59DE3994-6A63-4654-8FF0-F85C5163B2F5-与第二设备相同>,<CBAService:0x2811111180,isPrimare=YES,UUID=设备信息>])service.uuid.uuidString为59DE3994-6A63-4654-8FF0-F85C5163B2F5-与secondDevice相同,服务为[lt;CBService:0x27893kdc0,isPrimary=YES,uuid=59DE3994-6 A63-4654-8 FF0-F85C5163B2F5-与secondDevice>相同,<CBService:0x679kh8580,isPrimary=YES,uuid=Device Information>]
  17. 外围设备(_:didDiscoverServices:)-didDiscoverServices第三设备59DE3994-6A63-4654-8FF0-F85C5163B2F5-与第二设备相同
  18. 外设(_:didDiscoveryServices:)-未找到外设的didDiscoverServices H56KIL35-7835-7JKL-2B11-HJKLIYTAA400**

根据第16步应用程序中连接到我的第一个服务UUID和第一个特征UUID的第二个设备的日志信息,这是错误的!

你知道吗,我创建的UUID正确吗?

第S页:Android应用程序适用于这两种设备。

非常感谢!

为多个BLE设备接收相同的服务和特征UUID是完全正常的,这只意味着所有设备都提供完全相同的服务。

例如:如果你有两个测量一个人心率的设备,例如智能手表,这两个设备可能都提供相同UUID的心率服务。

如果您想区分设备,可以使用标识符,该标识符是您在提供的日志的步骤3中收到的。

最新更新