首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在ios value中将BLE设备的值写入字符

如何在ios value中将BLE设备的值写入字符
EN

Stack Overflow用户
提问于 2019-09-18 04:34:52
回答 1查看 5.6K关注 0票数 3

我正在开发iOS的BLE应用程序来给手机充电。我做了一切正确的事情来发现自己的特点。首先扫描外围设备并连接到它。发现没有响应属性的通知和写入的服务(FFB0)和特征(FFB1,FFB2)。

我找到了客户端特征配置描述符。我想发送命令到PCB,以解锁充电,我想写值的FFB2特性,但外围没有响应。这是我的密码。

我已经搜索了与此相关的所有内容,但我没有找到任何解决办法,如果有人给出这个问题的解决方案,这将对我有帮助。

这是由client:提供的文档

每个PCB上都有一个BLE4.0蓝牙模块,每个蓝牙模块有一个单独的12字节长的地址码,PCB数据通道,Service是OxFFBO,包括两个特征值,它是OxFFBI和OxFFB2,通信数据的长度为1-20字节。OxFFB1是应用程序数据的通道,download.OxFBB2是蓝牙上传数据的通道。

打开电源进入空闲模式,当处于空闲模式时,两个灯将flash.it发送一个ID代码(一个字节)到手机,手机中的应用程序将得到ID代码(一个字节)。可以通过电话设置设置ID代码。它意味着通过ID代码发送相应的解锁指令。

解锁指令为0x55,0xe1,(ID0+1),时间,下面是解锁指令的解释:

ID0是一个ID代码," time“是用户解锁时间(5-120分钟),请注意它是二进制到十六进制转换,time==05是5分钟,time==6是6分钟,时间==A是10分钟,等等。如果PCB的ID0为0xff,则解锁pcb 60分钟为:0x55,0xe10x00,0x3c;如果PCB的ID0为0x05,则解锁pcb 10分钟为:0x55,0xe1 ,0x06,0x0a。

当PCB上的MCU接收到解锁指令时,它将启动打开输出和定时。当定时时间结束时,系统再次进入空闲模式。在空闲模式下,长按键进入关闭睡眠时间模式。

uart的波特率为9,600。APP向单片机发送的查询命令为0x550x01单片机应响应应用程序的信息格式: 0x55,0x02,bat_level,xH,XL (xH XL表示电池的当前电压值,xH -表示高8位,XL--代表低8位)

代码语言:javascript
运行
复制
import CoreBluetooth

let batteryServiceCBUUID = CBUUID(string: "FFB0")
let batteryServiceRequestCBUUID = CBUUID(string: "FFB1")
let batteryServiceRequestCBUUID2 = CBUUID(string: "FFB2")

 class ChargingViewController: UIViewController , CBPeripheralDelegate 
 ,CBCentralManagerDelegate {    

  override func viewDidLoad() {
    super.viewDidLoad()
 centralManager = CBCentralManager(delegate: self, queue: nil, options: nil)  
 }
 func centralManagerDidUpdateState(_ central: CBCentralManager) {

    switch central.state {
    case .unknown:
        print("central.state is .unknown")
    case .resetting:
        print("central.state is .resetting")
    case .unsupported:
        print("central.state is .unsupported")
    case .unauthorised:
        print("central.state is .unauthorised")
    case .poweredOff:
        print("central.state is .poweredOff")
    case .poweredOn:


        centralManager.scanForPeripherals(withServices: [batteryServiceCBUUID])

        print("central.state is .poweredOn")
    @unknown default:
        fatalError()
    }

    print("state: \(central.state)")

 }
 func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, 
 advertisementData: [String : Any], rssi RSSI: NSNumber) {

    print(peripheral)
    batteryServicePeripheral = peripheral
    batteryServicePeripheral.delegate = self
    centralManager.stopScan()
    centralManager.connect(batteryServicePeripheral)

 }
  func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {

    print("Connected=======>\(String(describing: batteryServicePeripheral))")
    batteryServicePeripheral.delegate = self
    batteryServicePeripheral.discoverServices([batteryServiceCBUUID])

}

func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, 
 error: Error?) {

    print("Fail To Connect=======>\(String(describing: error))")
}


func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: 
CBPeripheral, error: Error?) {

    if error == nil {

        print("Disconnected========>\(peripheral)")
    }else {

        print("Disconnected========>\(String(describing: error))")
    }
 }
// MARK: -  CBPeripheral Delegate Methods

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {

    guard let services = peripheral.services else { return }

    for service in services {

        print("SPAKA:PERIPHERALSERVICES============>\(service)")
        peripheral.discoverCharacteristics(nil, for: service)

    }

}

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

    if let characteristics = service.characteristics {
        //else { return }

        for characteristic in characteristics {

            print(characteristic)


            if characteristic.uuid == batteryServiceCBUUID {

               peripheral.setNotifyValue(true, for: characteristic)

            }

            if characteristic.uuid == batteryServiceRequestCBUUID2 {

                batteryCharacteristics = characteristic


                let str1 = "55e100"
                let data = String(format: "%@%@",str1,hexTimeForChar)

                guard let valueString = data.data(using: String.Encoding.utf8)  else 
    {return}



                peripheral.writeValue(valueString, for: characteristic , type: 
        CBCharacteristicWriteType.withoutResponse)
                print("Value String===>\(valueString.debugDescription)")
                peripheral.setNotifyValue(true, for: characteristic)

            }

        }
    }
    peripheral.discoverDescriptors(for: batteryCharacteristics)
}


func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {

    if error == nil {
        print("Message sent=======>\(String(describing: characteristic.value))")
    }else{

        print("Message Not sent=======>\(String(describing: error))")
    }




}

func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: 
CBCharacteristic, error: Error?) {

    if error == nil {
        print("SPAKA : IS NOTIFYING UPDATED STATE ======>\(characteristic.isNotifying)")
        print("SPAKA : UPDATED DESCRIPTION ======>\(String(describing: 
 characteristic.description))")


    }else{
        print("SPAKA : ERRORUPDATEDNOTIFICATION\(String(describing: error))")
    }


}


func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {

    print("SPAKA: UPDATED VALUE RECEIVED========>\(String(describing: characteristic.value))")
    print("characteristic UUID: \(characteristic.uuid), value: \(characteristic.value)")

    guard let str = characteristic.value else { return  }

    if let string = String(bytes: str, encoding: .utf8) {
        print("SPAKA==========>:::\(string)")
    } else {
        print("not a valid UTF-8 sequence")
    }



}

func peripheral(_ peripheral: CBPeripheral, didDiscoverDescriptorsFor characteristic: CBCharacteristic, error: Error?) {

    guard let desc = batteryCharacteristics.descriptors else { return }


    for des in desc {

        print("BEGIN:SPAKA DESCRIPTOR========>\(des)")
        discoveredDescriptor = des

        print("Descriptor Present Value and uuid: \(des.uuid), value: \(String(describing: des.value))")
        peripheral.readValue(for: discoveredDescriptor)


    }


}

func peripheral(_ peripheral: CBPeripheral, didWriteValueFor descriptor: CBDescriptor, error: Error?) {
    if let error = error {
        print("Failed… error: \(error)")
        return
    }

    print("Descriptor Write Value uuid: \(descriptor.uuid), value: \(String(describing: descriptor.value))")
}

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor descriptor: CBDescriptor, error: Error?) {

    if let error = error {
        print("Failed… error: \(error)")
        return
    }

    print("Descriptor Updated Value uuid: \(descriptor.uuid), value: \(String(describing: descriptor.value))")

}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-18 07:03:01

关于PDB的信息有些难以破译,但是包含了很好的信息。听起来好像多氯联苯包含蓝牙到UART模块.一个特征是向PCB发送数据,一个特征是接收来自PCB的数据。

我仍然不明白需要发送到PCB的命令。如果您可以访问Android代码,那么您可以在那里找到答案。

下面是潜在可能的最低限度代码:

代码语言:javascript
运行
复制
let batteryServiceUUID = CBUUID(string: "FFB0")
let rxCharacteristicUUID = CBUUID(string: "FFB1")
let txCharacteristicUUID = CBUUID(string: "FFB2")

var centralManager: CBCentralManager!
var batteryPeripheral: CBPeripheral? = nil
var batteryService: CBService? = nil
var txCharacteristic: CBCharacteristic? = nil
var rxCharacteristic: CBCharacteristic? = nil

func centralManagerDidUpdateState(_ central: CBCentralManager) {
    if central.state == .poweredOn {
        centralManager.scanForPeripherals(withServices: [batteryServiceUUID])
    }
}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
    batteryPeripheral = peripheral
    batteryPeripheral!.delegate = self
    centralManager.stopScan()
    centralManager.connect(batteryPeripheral!)
}

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
    batteryPeripheral!.discoverServices([batteryServiceUUID])
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
    peripheral.discoverCharacteristics(nil, for: peripheral.services![0])
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
    for characteristic in service.characteristics! {
        if characteristic.uuid == rxCharacteristicUUID {
            rxCharacteristic = characteristic
            peripheral.setNotifyValue(true, for: rxCharacteristic!)
        } else if (characteristic.uuid == txCharacteristicUUID) {
            txCharacteristic = characteristic
        }
    }
    sendInitialCommand()
}

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
    if let value = characteristic.value {
        print("Data received")
        print(value as NSData)
    }
}

func sendInitialCommand() {
    let cmdBytes: [UInt8] = [0x55, 0xe1, 0x00, 0x0a]
    let cmd = Data(cmdBytes)
    batteryPeripheral!.writeValue(cmd, for: txCharacteristic!, type: .withoutResponse)
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57985152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档