如何将热量蓝牙打印机连接到iOS设备

时间:2017-11-01 13:56:04

标签: swift printing bluetooth thermal-printer

我有这款热敏蓝牙打印机Impress+。我正在制作一个应该打印账单的自定义应用程序。我已编写代码将其连接到iPhone,但它从未出现在搜索中。它永远不会到达didDiscoverPeripheral。我不知道什么是错的。以下是我搜索蓝牙设备的代码。请帮忙。任何帮助将受到高度赞赏。

import UIKit
import CoreBluetooth

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
    var centralManager: CBCentralManager!
    var peripheral: CBPeripheral!
    var writeCharacteristic: CBCharacteristic!
    var service: CBService!
    var characteristic: CBCharacteristic!

    var bluetoothAvailable = false
    let message = "1"

    @IBOutlet weak var labelDeviceName: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        centralManager = CBCentralManager(delegate: self, queue: nil)
    }

    func centralManagerDidUpdateState(_ central: CBCentralManager)
    {
        print("Checking state")
        switch (central.state)
        {
        case .poweredOff:
            print("CoreBluetooth BLE hardware is powered off")

        case .poweredOn:
            print("CoreBluetooth BLE hardware is powered on and ready")
            bluetoothAvailable = true;

        case .resetting:
            print("CoreBluetooth BLE hardware is resetting")

        case .unauthorized:
            print("CoreBluetooth BLE state is unauthorized")

        case .unknown:
            print("CoreBluetooth BLE state is unknown");

        case .unsupported:
            print("CoreBluetooth BLE hardware is unsupported on this platform");

        }
        if bluetoothAvailable == true
        {
            discoverDevices()
        }
    }

    private func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)
    {
        // Stop scanning
        self.centralManager.stopScan()
        print("Stopped Scanning")
        peripheral.discoverServices([CBUUID(string: "2220")])
        print("CONNECTED!!")
        print("Device Name:",peripheral.name!)
        self.labelDeviceName.text = peripheral.name
    }

    func discoverDevices() {
        print("Discovering devices")
        centralManager.scanForPeripherals(withServices: nil, options: nil)
    }
}

1 个答案:

答案 0 :(得分:4)

短篇小说: 蓝牙热敏打印机只有在使用Bluetooth 4.0 LE版本或制造商在Mfi Program中注册的情况下,才能与iOS一起使用。

无论如何,iOS上的“设置” =>“蓝牙”中都没有列出蓝牙LE 4.0设备。

如果您的蓝牙热敏打印机具有Bluetooth 4.0 LE,则可以使用 Objective C sample codeSwift sample code

长话短说: iOS和macOS SDK支持带有Core Bluetooth framework的Bluetooth 4.0 LE设备以及带有External Accessory framework的其他Bluetooth版本设备。 外部附件框架要求在Mfi Program中注册蓝牙热敏打印机的制造商。 只有主要的制造商,如爱普生,Star Micronics,Zebra,Bixolon才能在Mfi计划中注册。

如果您从一家小型制造商在线购买了廉价的蓝牙热敏打印机,则它仅在使用Bluetooth 4.0 LE的iOS上运行,因为它使用的是Core Bluetooth框架,不需要向Mfi程序进行任何注册。