我有一台热蓝牙打印机。我正在做一个定制的应用程序,应该打印账单。我已经编写了连接到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)
    }
}

最佳答案

短篇故事:
蓝牙热敏打印机只有在使用bluetooth 4.0 LE版本或制造商在Mfi Program中注册的情况下才能使用iOS。
Bluetooth LE 4.0设备不在iOS上的“设置”=>Bluetooth中列出。
如果您的bluetooth热敏打印机具有bluetooth 4.0 LE,则可以使用
Objective C sample code
Swift sample code
长话短说:
iOS和macOS SDKs支持Core Bluetooth framework的蓝牙4.0 LE设备和External Accessory framework的其他蓝牙版本设备。
外部附件框架要求蓝牙热敏打印机的制造商在Mfi Program中注册。
只有像爱普生,星微,斑马,比克龙等主要制造商注册了Mfi程序。
如果你从一家小型制造商那里在线购买一台廉价的蓝牙热敏打印机,那么它只能在使用蓝牙4.0le的情况下使用iOS,因为它使用的核心蓝牙框架不需要注册到Mfi程序。

关于swift - 如何将Thermal Bluetooth Printer连接到iOS设备,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47056562/

10-11 19:29