CoreBluetooth не принимает команды в состоянии соединения - PullRequest
0 голосов
/ 27 апреля 2019

В моем UIViewController у меня есть 2 функции, которые в основном одинаковы. Один запускается при нажатии кнопки, другой запускается при открытии приложения из DeepLink. Обе функции делают то же самое; отправив en print текст на принтер через Bluetooth.

Теперь странная часть в том, что когда я нажимаю кнопку печати через кнопку, все идет хорошо. Но в случае, если я вызываю функцию через deeplink (myApp: // printthistext), ядро-bluetooth терпит неудачу со следующим кодом:

printthistext (printed variable of text to be printed)
print failed
2019-04-27 19:30:48.066081 Example[775:150612] [CoreBluetooth] API MISUSE: 
<CBPeripheral: 0x1740e5300, identifier = D87FD0A6-D92B-40C0-DAFA, name = Black BT Printer, state = disconnected> can only 
accept commands while in the connected state
2019-04-27 19:30:48.068590 Example[775:150611] [CoreBluetooth] XPC 
connection invalid

Когда я смотрю на значок Bluetooth, я вижу, что я подключен.

Что может привести к тому, что соединение станет недействительным, когда я вызываю приложение из URL схемы? На самом деле обе функции практически одинаковы.

AppDelegate.m:

import UIKit
import Printer

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {

    let geturl = url.host?.removingPercentEncoding;
    let vc = ViewController()
    vc.printOrder(url: geturl!)

    return true
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    return true
}

ViewController.m:

import UIKit
import Printer

class ViewController: UIViewController {

let pm = PrinterManager()

override func viewDidLoad() {
    super.viewDidLoad()
}

func printOrder(url: String){

    print(url)
    if pm.canPrint{
        var receipt = Receipt(
        .title(url)
        )

        receipt.feedLinesOnTail = 2
        receipt.feedPointsPerLine = 60

        pm.print(receipt)
    } else {
        print("print failed")

    }
}


@IBAction func touchPrint(sender: UIButton) {

    if pm.canPrint {

        var receipt = Receipt(
            .title("test")
        )

        receipt.feedLinesOnTail = 2
        receipt.feedPointsPerLine = 60

        pm.print(receipt)
        print("print success")
    } else {

        performSegue(withIdentifier: "ShowSelectPrintVC", sender: nil)
    }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let vc = segue.destination as? PrinterTableViewController {
        vc.sectionTitle = "Choose Printer"
        vc.printerManager = pm
    }
}
}

Я использую примеры файлов Keving Gong: https://github.com/KevinGong2013/Printer/

...