Reputation: 941
I have this Thermal Bluetooth Printer Impress+. I am making a custom application which should print bills. I have written code to connect it to iPhone but it never shows up in the search. It never reaches didDiscoverPeripheral. I don't know whats wrong. Below is my code for searching bluetooth devices. Please help. Any help will be highly appreciated.
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)
}
}
Upvotes: 2
Views: 8581
Reputation: 13539
Short story: A bluetooth thermal printer can work with iOS only if it uses the Bluetooth 4.0 LE version or if the manufacturer registered in the Mfi Program.
Bluetooth LE 4.0 devices aren't listed in the Settings => Bluetooth on iOS anyway.
If your bluetooth thermal printer has Bluetooth 4.0 LE you can adapt the official Apple sample code using Objective C sample code or in Swift sample code
Long story: iOS and macOS SDKs supports Bluetooth 4.0 LE devices with the Core Bluetooth framework and other Bluetooth versions devices with the External Accessory framework. The External Accessory framework requires that the manufacturer of the bluetooth thermal printer is registered in the Mfi Program. Only the major manufacturers like Epson, Star Micronics, Zebra, Bixolon are registered with Mfi Program.
If you buy a cheap bluetooth thermal printer online from a minor manufacturer it will only work with iOS if it uses Bluetooth 4.0 LE since it uses the Core Bluetooth framework which does not require any registration to the Mfi Program.
Upvotes: 8