CoreBluetooth在连接状态下不会接受命令

时间:2019-04-27 17:46:13

标签: ios swift core-bluetooth

在我的UIViewController中,我有2个基本相同的功能。单击按钮会触发一个事件,而从DeepLink中打开该应用程序则触发另一个事件。两种功能都在做同样的事情。通过Bluetooth发送打印文本到打印机。

现在奇怪的是,当我通过按钮单击“打印”时,一切正常。但是,如果我通过deeplink(myApp:// printthistext)调用该函数,则核心蓝牙将失败,并显示以下代码:

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

当我查看我的蓝牙图标时,可以看到已连接。

当我从方案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/

0 个答案:

没有答案