我正在尝试创建一个使用打印机通过蓝牙进行打印的应用
在 xcode 上,我可以连接打印机,还可以查看服务和 uuid,但问题是当我尝试查看服务的特征时,我发现没有 有人知道这个问题吗?
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: -
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?) 委托(delegate)方法
关于ios swift 3 - 打印机蓝牙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40102369/
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R
classPacketdefinitialize(name,age,number,array)@name=name@age=age@number=number@neighbors=arrayendendp1=Packet.new("n1",5,2,[1,2,3,4])putsp1.name我有上面的代码,但是每当我执行puts语句时,我都会收到nameisnotamethod的错误。我不知道任何其他方式来打印p1的名称。如何打印姓名? 最佳答案 这里的问题是,虽然您拥有实例变量,但您并未使它们可访问。attr_reader:vari
我正在使用mechanize登录网站,然后检索页面。我遇到了一些问题,我怀疑这是由于cookie中的某些值造成的。当Mechanize登录网站时,我假设它存储了cookie。如何通过Mechanize打印出存储在cookie中的所有数据? 最佳答案 代理有一个cookie方法。agent=Mechanize.newpage=agent.get("http://www.google.com/")agent.cookiesagent.cookies.to_scookie返回一个Mechanize::Cookiesobject
有没有办法快速将表格格式的ruby哈希打印到文件中?如:keyAkeyBkeyC...1232343451253474456...其中散列的值是不同大小的数组。还是使用双循环是唯一的方法?谢谢 最佳答案 试试我写的这个gem(在表中打印散列、ruby对象、ActiveRecord对象):http://github.com/arches/table_print 关于ruby-如何以表格格式快速打印Ruby哈希值?,我们在StackOverflow上找到一个类似的问题:
如果我有一些测试,例如require_relative"Line"require_relative"LineParser"describeLinedoit"Canbecreated"doload"spec_helper.rb"@line.class.should==Lineendit"Canbeparsed"do...如何打印出测试组名称-在本例中为“Line”。我尝试添加:before:alldoputs"In#{self.class}"end但这给出了:InRSpec::Core::ExampleGroup::Nested_3,而不是Line 最佳答案
我有一个执行mysql的小ruby脚本导入方式:mysql-u-p-h,但利用Open3.popen3这样做。这就是我到目前为止所拥有的:mysqlimp="mysql-u#{mysqllocal['user']}"mysqlimp这实际上是在做工作,但有一件事困扰着我,与我希望看到的输出有关。如果我将第一行更改为:mysqlimp="mysql-v-u#{mysqllocal['user']}"#notethe-v然后整个脚本永远挂起。我猜,发生这种情况是因为读流和写流相互阻塞,我也猜想stdout需要定期冲洗,以便stdin将继续被消耗。也就是说,只要stdout的buffer已满
require'pp'p*1..10这会打印出1-10。为什么这么简洁?您还可以用它做什么? 最佳答案 它是“splat”运算符。它可用于分解数组和范围并在赋值期间收集值。这里收集赋值中的值:a,*b=1,2,3,4=>a=1b=[2,3,4]在此示例中,内部数组([3,4])中的值被分解并收集到包含数组中:a=[1,2,*[3,4]]=>a=[1,2,3,4]您可以定义将参数收集到数组中的函数:deffoo(*args)pargsendfoo(1,2,"three",4)=>[1,2,"three",4]
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visitthehelpcenter.关闭10年前。我想从给定的哈希键中打印出一个键,但找不到简单的解决方案:myhash=Hash.newmyhash["a"]="bar"#notworkingmyhash.fetch("a"){|k|putsk}#working,butuglyifmyhash.has_key("a")?puts"a"end还有其他办法吗?
我正在尝试使用以下代码打印1到50之间的所有数字:[1..50].each{|n|putsn}但是控制台打印[1..50]我想打印这样的东西1个2个3个4个...50 最佳答案 试试下面的代码:(1..50).each{|n|putsn}问题是您使用的是[]定界符而不是()定界符。 关于ruby-打印一个范围内的数字,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/14528573
假设我在我的Rails应用中某处定义了一个名为bla的函数。在ruby或rails中有没有一种方法可以动态/以编程方式打印用于定义该函数的代码?例如:defblaputs"HiThere"end然后如果我调用一个函数,例如get_definition:putsget_definition(:bla)这会打印出来"puts\"HiThere\""有规范的方法吗?我以前实际上不需要这样做,而且我知道这在Rails中并不是很常见的做法。我也不想使用元(反射)编程定义我的方法,然后存储用于定义我的方法的字符串。感谢您的帮助! 最佳答案