【问题标题】:iOS CoreBluetooth print CBService and CBCharacteristiciOS CoreBluetooth 打印 CBService 和 CBCharacteristic
【发布时间】:2015-09-29 22:21:19
【问题描述】:

我正在使用 Swift 开发一个 iOS 应用程序来集成蓝牙打印机来打印信息。我用过CoreBluetooth框架但不知道哪个服务、特征我可以写value打印出来

//find CBService
func peripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!) {
    //println("CBPeripheralDelegate didDiscoverServices")

    for service in peripheral.services {

        println("Service: Discover service \(service)")
        println("Service: UUID \(service.UUID) ")

        peripheral.discoverCharacteristics(nil, forService: service as! CBService)

    }
}

//find CBCharacteristics
func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!) {

    //if service.UUID == CBUUID(string: "18F0"){
        for characteristic in service.characteristics {

            let chara: CBCharacteristic? = characteristic as? CBCharacteristic
            println("Char: service \(service.UUID) Discover char \(chara)")
            println("Char: UUID \(chara!.UUID)")

            peripheral.readValueForCharacteristic(chara)

            /*
            println("0")

            switch chara!.UUID {

            case CBUUID(string: "2AF1"):
                println("1")

                var rawArray:[UInt8] = [0x01];
                let data = NSData(bytes: &rawArray, length: rawArray.count)

                peripheral.writeValue(data, forCharacteristic: chara, type: CBCharacteristicWriteType.WithoutResponse)

            default: println("")
            }
            */
        }
    //}

}

系统显示结果如下:

服务:发现服务 服务:UUID 电池 服务:发现服务 服务:UUID 1803 服务:发现服务 服务:UUID 1802 服务:发现服务 服务:UUID 1811 服务:发现服务 服务:UUID 1804 服务:发现服务 服务:UUID 18F0 服务:发现服务 服务:UUID 设备信息 服务:发现服务 服务:UUID E7810A71-73AE-499D-8C15-FAA9AEF0C3F2

字符:发现字符 Char:UUID 电池电量 字符:发现字符 字符:UUID 2A06 字符:发现字符 字符:UUID 2A06 字符:发现字符 字符:UUID 2A47 字符:发现字符 字符:UUID 2A46 字符:发现字符 字符:UUID 2A48 字符:发现字符 字符:UUID 2A45 字符:发现字符 字符:UUID 2A44 字符:发现字符 字符:UUID 2A07 字符:发现字符 字符:UUID 2AF1 字符:发现字符 字符:UUID 2AF0 字符:发现字符 字符:UUID 系统 ID 字符:发现字符 字符:UUID 制造商名称字符串 字符:发现字符 字符:UUID BEF8D6C9-9C21-4C9E-B632-BD58C1009F9F

哪位蓝牙专家可以指导一下,谢谢

【问题讨论】:

  • 您需要打印机供应商提供有关如何与其产品交互的信息。我的猜测是你写信给特征BEF8D6C9-9C21-4C9E-B632-BD58C1009F9F,因为那是唯一的非标准
  • 像 Paulw11。还请查看您的CBCharacteristic 中的properties,如果您只能阅读或写入、通知等,它应该会给您。
  • @Paulw11 我尝试修改为使用“BEF8D6C9-9C21-4C9E-B632-BD58C1009F9F”和 var rawArray:[UInt8] = [0x01]; let data = NSData(bytes: &rawArray, length: rawArray.count) peripheral.writeValue(data, forCharacteristic: chara, type: CBCharacteristicWriteType.WithoutResponse)
  • @Paulw11 但是,不工作,有没有可能我的 writeValue 使用错误或必须修改任何代码?
  • @KevinChang 您尝试过 Lite blue 应用程序吗?它将引导您了解特征并向您展示您可以编写的特征

标签: ios swift printing core-bluetooth


【解决方案1】:

短版 只需写信给任何具有属性WriteWriteWithoutResponse 的非标准服务。确保第一次写入少于 20 个字节,以避免超过缓冲区限制。

加长版: 我的蓝牙热敏打印机(QSPrinter 5801 Bluetooth LE)也遇到了同样的问题,它与这个问题的设备具有完全相同的服务和特性。 我发现:

具有特征2AF1 的服务18F0 具有属性WriteWithoutResponse

服务E7810A71-73AE-499D-8C15-FAA9AEF0C3F2有特色 BEF8D6C9-9C21-4C9E-B632-BD58C1009F9F 有属性 Read+Write+Notify+Indicate

(其中Write 表示Write with response)。

这是一个特性的官方属性列表:

Broadcast = 0x01,
Read = 0x02,
WriteWithoutResponse = 0x04,
Write = 0x08,
Notify = 0x10,
Indicate = 0x20,
AuthenticatedSignedWrites = 0x40,
ExtendedProperties = 0x80

通过使用位域乘法,一个属性可以有多个值。 例如属性 0x30 在二进制中是 00110000,这意味着通知 + 指示。您需要获取二进制数(8位)并将二进制位从右到左放置在从上到下的值列表中:


0 Broadcast
0 Read
0 WriteWithoutResponse
0 Write
1 Notify
1 Indicate
0 AuthenticatedSignedWrites
0 ExtendedProperties

最后我发现我可以使用以下方法成功写入任一特征:


let bytesToPrint: [UInt8] = [27, 64, 10, 10, 10, 99, 105, 97, 111, 10, 10, 10]
let data = Data(bytes: bytesToPrint)

//Using Characteristic 2AF1 with Properties WriteWithoutResponse peripheral.writeValue(data, for: characteristic, type: CBCharacteristicWriteType.withoutResponse)

//Using Characteristic BEF8D6C9-9C21-4C9E-B632-BD58C1009F9F with Properties Write peripheral.writeValue(data, for: characteristic, type: CBCharacteristicWriteType.withResponse)

问题是我同时发送了太多字节的数据。每个低功耗蓝牙设备都有不同的缓冲区大小。我听说有人发现设备的缓冲区限制为 20、40、100、400 字节。 在我的情况下,限制是 100,所以一旦我开始发送少于 100 个字节,蓝牙设备就会正确反应。 我的建议是尝试发送少于 20 个字节开始,一旦它工作你可以增加发送的字节,直到它停止工作,你会知道你已经达到缓冲区限制。

【讨论】:

    【解决方案2】:

    试试浅蓝色应用程序 (iOS),你会一一找到合适的特性来编写。它具有从应用程序写入值的选项

    【讨论】:

    • 从问题中可以看出,已经以编程方式找到了特征。正如我在回答中解释的那样,问题实际上是写入了太多字节并超出了缓冲区限制。
    猜你喜欢
    • 2020-12-14
    • 2017-09-20
    • 2021-02-17
    • 1970-01-01
    • 2015-07-07
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多