iOS Swift 3-打印机蓝牙 [英] ios swift 3 - printer bluetooth
问题描述
我正在尝试创建一个使用打印机通过蓝牙打印的应用程序
I'm trying to create an app that print via bluetooth with a printer
在xcode上,我能够连接打印机,还可以查看服务和uuid的内容,但是问题是当我尝试查看服务的特性时,我发现nil 有人对这个问题有想法吗?
on xcode, i'm able to connect the printer, and also to see the services and the uuid's, but the problem is that when i try to see the characteristic of the services i found nil do anyone have a idea about that problem ?
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
for service in peripheral.services! {
print("Service \(service)\n")
print("Discovering Characteristics for Service : \(service.uuid)")
print(service.characteristics)
}
}
override func viewDidLoad() {
super.viewDidLoad()
centralManager = CBCentralManager(delegate: self, queue: nil)
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if #available(iOS 10.0, *){
switch (central.state) {
case CBManagerState.poweredOff:
print("CBCentralManagerState.PoweredOff")
case CBManagerState.unauthorized:
print("CBCentralManagerState.Unauthorized")
break
case CBManagerState.unknown:
print("CBCentralManagerState.Unknown")
break
case CBManagerState.poweredOn:
print("CBCentralManagerState.PoweredOn")
centralManager.scanForPeripherals(withServices: nil, options: nil)
centralManager.scanForPeripherals(withServices: nil, options: nil)
case CBManagerState.resetting:
print("CBCentralManagerState.Resetting")
case CBManagerState.unsupported:
print("CBCentralManagerState.Unsupported")
break
}}else{
switch central.state.rawValue{
case 0:
print("CBCentralManagerState.Unknown")
break
case 1:
print("CBCentralManagerState.Resetting")
case 2:
print("CBCentralManagerState.Unsupported")
break
case 3:
print("This app is not authorised to use Bluetooth low energy")
break
case 4:
print("Bluetooth is currently powered off.")
case 5:
print("Bluetooth is currently powered on and available to use.")
self.centralManager.scanForPeripherals(withServices: nil, options: nil)
break
default:
break
}}}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print("name : \(peripheral.name )")
let device = (advertisementData as NSDictionary)
.object(forKey: CBAdvertisementDataLocalNameKey)
as? NSString
if device?.contains(BEAN_NAME) == true {
self.centralManager.stopScan()
self.peripheral = peripheral
self.peripheral.delegate = self
print("peripheral: \(self.peripheral)")
centralManager.connect(peripheral, options: nil)
print("peripheral: \(self.peripheral)")
}
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
peripheral.delegate = self
peripheral.discoverServices(nil)
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
print(error)
for characteristic in service.characteristics! {
print(anything)
}
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
print("Sent")
}
推荐答案
您缺少didDiscoverServices
中的重要步骤-您需要致电discoverCharacteristics:for:
-
You are missing an important step in your didDiscoverServices
- you need to call discoverCharacteristics:for:
-
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
for service in peripheral.services! {
print("Service \(service)\n")
print("Discovering Characteristics for Service : \(service.uuid)")
print(service.characteristics)
}
}
然后您将调用您的peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?)
委托方法
You will then get a call to your peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?)
delegate method
这篇关于iOS Swift 3-打印机蓝牙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!