Swift 扫描可扩展外围设备并连接到它
一般来说,对可编程和编码移动应用程序来说是相当新的。我试过几个演示,发现了这个 问题是它没有连接到我的BLE设备,所以我假设它只扫描创建人当时拥有的BLE设备,所以我总是得到错误“只能在通电状态下接受此命令”。代码中是否有任何部分需要更改以使其扫描另一个外围设备?我是否需要在某处指定设备的UUID、MAC地址或其他唯一信息Swift 扫描可扩展外围设备并连接到它,swift,xcode,bluetooth,bluetooth-lowenergy,Swift,Xcode,Bluetooth,Bluetooth Lowenergy,一般来说,对可编程和编码移动应用程序来说是相当新的。我试过几个演示,发现了这个 问题是它没有连接到我的BLE设备,所以我假设它只扫描创建人当时拥有的BLE设备,所以我总是得到错误“只能在通电状态下接受此命令”。代码中是否有任何部分需要更改以使其扫描另一个外围设备?我是否需要在某处指定设备的UUID、MAC地址或其他唯一信息 感谢您对这个问题的帮助大约一年前,我开发了一个应用程序,用于扫描和连接BLE设备。从该项目发布代码。如果按原样复制粘贴,则可能不起作用。但我100%肯定这正是你想要实现的 我
感谢您对这个问题的帮助大约一年前,我开发了一个应用程序,用于扫描和连接BLE设备。从该项目发布代码。如果按原样复制粘贴,则可能不起作用。但我100%肯定这正是你想要实现的 我也使用“”作为参考 希望这能帮助你解决你的问题。如果你有任何疑问,请告诉我
import UIKit
import CoreLocation
import CoreBluetooth
class BLEDeviceTableViewController: UIViewController, CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource, CBCentralManagerDelegate, CBPeripheralDelegate {
private var myTableView: UITableView!
var manager : CBCentralManager!
var myBluetoothPeripheral = [CBPeripheral]()
var myCharacteristic : CBCharacteristic!
var isMyPeripheralConected = false
var peripheralNames = [String]()
override func viewDidLoad() {
super.viewDidLoad()
let displayWidth: CGFloat = self.view.frame.width
manager = CBCentralManager(delegate: self, queue: nil)
myTableView = UITableView(frame: CGRect(x: 0, y: 135, width: displayWidth, height: (self.view.frame.height-(self.view.frame.height/3))-10))
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
myTableView.dataSource = self
myTableView.delegate = self
self.view.addSubview(myTableView)
// Do any additional setup after loading the view.
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
var msg = ""
switch central.state {
case .poweredOff:
msg = "Bluetooth is Off"
case .poweredOn:
msg = "Bluetooth is On"
manager.scanForPeripherals(withServices: nil, options: nil)
case .unsupported:
msg = "Not Supported"
default:
msg = "That message means that you should power on the bluetooth, and then search for the devices. Something like:
func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state {
case .unknown:
print("Unknown")
case .resetting:
print("Resetting")
case .unsupported:
print("Unsupported")
case .unauthorized:
print("Unauthorized")
case .poweredOff:
print("Powered Off")
central.stopScan()
case .poweredOn:
print("Powered On")
central.scanForPeripherals(withServices: nil, options: nil)
}
}
导入UIKit
导入核心定位
导入核心蓝牙
类BLEDeviceTableViewController:UIViewController、CLLocationManagerDelegate、UITableViewDelegate、UITableViewDataSource、CBCentralManagerDelegate、CBPeripheralDelegate{
私有变量myTableView:UITableView!
var经理:CBCentralManager!
var myBluetoothPeripheral=[CBPeripheral]()
var myCharacteristic:CBCharacteristic!
var IsMyPeripheralConnected=false
var peripheraldnames=[String]()
重写func viewDidLoad(){
super.viewDidLoad()
让displayWidth:CGFloat=self.view.frame.width
manager=CBCentralManager(代理:self,队列:nil)
myTableView=UITableView(帧:CGRect(x:0,y:135,宽度:displayWidth,高度:(self.view.frame.height-(self.view.frame.height/3))-10))
myTableView.register(UITableViewCell.self,强制重用标识符:“MyCell”)
myTableView.dataSource=self
myTableView.delegate=self
self.view.addSubview(myTableView)
//加载视图后执行任何其他设置。
}
func CentralManager数据状态(uCentral:CBCentralManager){
var msg=“”
切换中央状态{
案例.断电:
msg=“蓝牙已关闭”
案例.poweredOn:
msg=“蓝牙已开启”
manager.ScanFor外围设备(服务:无,选项:无)
案例。不支持:
msg=“不受支持”
违约:
msg=“该消息意味着您应该打开蓝牙,然后搜索设备。例如:
非常感谢您的回复Cedan。我在运行此程序时遇到错误,可能是因为代码没有补充其余的代码。这是您从Rick的代码中为项目Cedan更改的唯一部分吗?如果您将xcode文件上载到某个地方,我们将不胜感激。@Campbell好的,请给我一个小时左右的时间,我将与您分享一个实现此代码的Xcode项目,没有错误。上面的代码我刚从我的项目中复制,所以你说它无法工作,因为它与其余的代码没有连接。所以我很快会共享一个项目。@Campbell,所以我为你创建了一个测试项目。你可以在这个链接中找到它。请确保在或中遵循这些步骤为了让它正常工作。1.确保您在真实设备上运行此程序。2.为了让应用程序找到BLE设备,请在另一个设备上下载并安装LightBlue Explorer
,然后创建一个虚拟外围设备,以便运行代码的设备可以扫描并找到您创建的测试蓝牙外围设备。或者您也可以使用任何其他BLE设备比如健身带之类的。让我知道它是否有效。我理解这个想法,它真的很酷。我的问题是我想让应用程序连接到一个BLE模块,这样Lightblue explorer就不能在上面下载。非常感谢你的帮助,尽管这意味着很多。@Campbell你在使用什么样的BLE模块?当你运行我发给你的项目时,它是否显示BLE模块的名称???已尝试将这些案例添加到我的项目中,但仍收到与以前相同的错误。我已尝试将扫描设置为所有可用的外围设备,但仍不起作用。