我有一个设备(后来命名为“扫描仪”)和其他设备(后来命名为“石头”)。扫描仪可以扫描宝石并在显示器上显示其信息。扫描仪可以通过蓝牙发送宝石信息,我想读取这些数据并在我的应用程序中使用它。我开始执行代码连接,但是我发现了一个问题。这里是我的代码:为什么我没有通过CoreBluetooth获得配对设备的实例?
import UIKit
import CoreBluetooth
class BluetoothViewController: UIViewController {
var manager:CBCentralManager!
var peripheral:CBPeripheral!
override func viewDidLoad() {
super.viewDidLoad()
manager = CBCentralManager(delegate: self, queue: nil)
}
}
extension BluetoothViewController: CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == .poweredOn {
central.scanForPeripherals(withServices: nil, options: nil)
}
else {
print("Bluetooth not available.")
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print("peripheral \(peripheral)")
print("rssi \(RSSI)")
print("advertisementData: \(advertisementData)")
print("--------")
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print(peripheral)
}
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
print(peripheral)
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
print(peripheral)
}
}
extension BluetoothViewController: CBPeripheralDelegate {
}
问题是扫描仪没有出现在
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber)
和
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)
永远不会调用。我的iPhone是用扫描仪和扫描仪连接
注意告诉我的连接工作。
我想达到什么目的?
我想创建视图 - 控制哪些检查扫描仪连接。
if connected then
get scan information
else
find device and connect
get scan information
请帮帮我。 :)
编辑
在LightBlue Explorer app我的扫描仪设备没有出现。 但与此app完美的作品。
也许使用CoreBluetooth是一种错误的方式做到这一点。什么是更好的。如果以上应用程序的创建者可以传达您的意见,那么可以这样做。
您的扫描仪设备是否宣传BLE服务?试试应用商店中的LightBlue应用。 – Paulw11
@ Paulw11如果没有? –
为了调用didConnect方法,您需要明确地连接到'didDiscoverPeripheral'中的外设。不知道为什么你的设备没有被发现。这些“宝石”究竟是什么? – Mark