如何将Thermal Bluetooth Printer连接到iOS设备

iDe*_*per 0 printing bluetooth thermal-printer swift

我有这款Thermal Bluetooth 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)
    }
}
Run Code Online (Sandbox Code Playgroud)

Gio*_*gio 5

简短说明: 蓝牙热敏打印机仅在使用Bluetooth 4.0 LE版本或制造商已在Mfi程序中注册时才能与iOS一起使用。

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

如果您的蓝牙热敏打印机具有Bluetooth 4.0 LE,则可以使用Objective C示例代码Swift示例代码来适应Apple的官方 示例代码。

简而言之 iOS和macOS SDK支持具有Core Bluetooth框架的 Bluetooth 4.0 LE设备和具有External Accessory框架的其他Bluetooth版本设备。外部附件框架要求蓝牙热敏打印机的制造商已在Mfi程序中注册。只有主要的制造商,如爱普生,Star Micronics,Zebra,Bixolon才能在Mfi计划中注册。

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