есть функция didDiscoverBLE
func didDiscoverBLE(_ peripheral: CBPeripheral!, address: String!, rssi: Int32) {
DispatchQueue.main.async(execute: {() -> Void in
if self.arrayPeripehral.contains(where: { $0.name == peripheral.name }) {
return
}
self.arrayPeripehral.append(peripheral)
let title: String = peripheral.name!
self.arrayPeripheralName.append(title)
let width = Int(self.view.bounds.size.width)
//int height = self.view.bounds.size.height;
let button1 = UIButton()
button1.frame = CGRect(x: CGFloat(30), y: CGFloat((self.arrayPeripheralName.count - 1) * 40), width: CGFloat(width - 60), height: CGFloat(40))
button1.backgroundColor = UIColor.black
button1.setTitle(title, for: .normal)
button1.contentHorizontalAlignment = .left
button1.addTarget(self, action: #selector(self.printBtn), for: .touchUpInside)
self.scrollView?.contentSize = CGSize(width: CGFloat(width), height: CGFloat(self.arrayPeripheralName.count * 40))
self.scrollView?.addSubview(button1)
})
}
@IBAction func printBtn(_ sender: Any) {
let btn = sender as! UIButton
if (myBle?.isOpened())! {
tfRec?.text = "Please disconnect first ...\r\n"
return
}
myBle?.stopScan()
let index: Int? = (arrayPeripheralName as NSArray).index(of: btn.title(for: .normal) as Any)
let peripheral: CBPeripheral? = arrayPeripehral[index!]
tfRec?.text = "Connecting...\r\n"
myQueue.async(execute: {() -> Void in
print("\(peripheral as Any)")
if (self.myBle?.open(peripheral))! {
self.myPos?.setIO(self.myBle)
self.myLabel?.setIO(self.myBle)
// ЛОГО
let img = UIImage(named: "dominate.png")
self.myPos?.pos_PrintPicture(img, x: -2, nWidth: 171, nHeight: 171, nBinaryAlgorithm: 1, nCompressMethod: 0)
let footer5 = "ФД 59924 ФП 41212312222 \r\n"
self.myPos?.pos_PrintText(UnsafeMutablePointer<Int8>(mutating: footer5)!, x: 10, nWidthTimes: 0, nHeightTimes: 0, nFontType: 0, nFontStyle: 80)
// Отступы снизу чека
self.myPos?.pos_PrintText(UnsafeMutablePointer<Int8>(mutating: "\r\n\r\n\r\n\r")!, x: 35, nWidthTimes: 0, nHeightTimes: 0, nFontType: 1, nFontStyle: 128)
} else {
DispatchQueue.main.async(execute: {() -> Void in
self.tfRec?.text = "Ошибка \r\n"
})
}
})
}
она передает параметры в кнопку printBtn
которая в свою очередь содержит dispatch и фишка такая кнопка добавляется на scrollView и по ее нажатию происходит 2 действия
- Создание пары по ble
- Отправка данных на печать
при чем когда соединяюсь первый раз ни чего не происходит, потом приходиться сделать дисконнект и по новой соединиться и только после этого идет печать…
а в идеале хотелось бы получить отдельно коннект и отдельно кнопку печати