草庐IT

swift - 扫描 BLE 外围设备并连接到它

coder 2023-09-16 原文

对 BLE 和一般的移动应用程序编码还比较陌生。我尝试了几个演示并遇到了这个 https://github.com/RickRedSix/BLE4.0-iOS-Swift-Demo 问题是它没有连接到我的 BLE 设备,所以我假设它只扫描 BLE 设备创建者当时拥有的,所以我总是收到错误“只能在开机状态下接受此命令”。我需要更改代码中的任何部分以使其扫描另一个外围设备吗?我是否需要在某处指定设备的 UUID、MAC 地址或其他唯一信息?

感谢对此问题的任何帮助

最佳答案

大约一年前,我开发了一个应用程序来扫描和连接到 BLE 设备。发布该项目的代码。如果您按原样复制粘贴它可能无法正常工作。但我 100% 确定这正是您想要实现的目标。

我也用了“https://github.com/RickRedSix/BLE4.0-iOS-Swift-Demo”作为引用。

希望这能帮助您解决问题。如果您有任何疑问,请告诉我。

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 = "?"

    }

    print("STATE: " + msg)

}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {



    peripheralNames.append(peripheral.name ?? "Unknown Device")
    print(peripheral)


    self.myBluetoothPeripheral.append(peripheral)      //save peripheral

    print(peripheralNames)
    myTableView.reloadData()
}

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
    print("Connected to \(peripheral.name ?? "Uknown Device")")
    availableDeviceLabel.text = "Connected to \(peripheral.name ?? "Uknown Device")"
    isMyPeripheralConected = true //when connected change to true
    peripheral.delegate = self
    peripheral.discoverServices(nil)
}

func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
    isMyPeripheralConected = false //and to falso when disconnected
}


func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {

    print("Services:\(String(describing: peripheral.services)) and error\(String(describing: error))")
    if let services = peripheral.services {
        for service in services {
            peripheral.discoverCharacteristics(nil, for: service)
        }
    }

}

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {


    print("peripheral:\(peripheral) and service:\(service)")

    for characteristic in service.characteristics!
    {
        peripheral.setNotifyValue(true, for: characteristic)
    }

}

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {

    let readValue = characteristic.value

    print("Response from BLE Device: \(readValue)")

}

//if you want to send an string you can use this function.
func writeValue() {

    if isMyPeripheralConected {

        //check if myPeripheral is connected to send data

        //let dataToSend: Data = "Hello World!".data(using: String.Encoding.utf8)!

        //myBluetoothPeripheral.writeValue(dataToSend, for: myCharacteristic, type: CBCharacteristicWriteType.withoutResponse)    //Writing the data to the peripheral

    } else {
        print("Not connected")
    }
}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    //let indexPath = NSIndexPath(forRow: tag, inSection: 0)
    //let cell = tableView.cellForRow(at: indexPath) as! gpsSettingsCustomCell!
    //print(cell?.cellLabel.text ?? "")

    self.myBluetoothPeripheral[indexPath.row].delegate = self

    manager.stopScan()                          //stop scanning for peripherals
    manager.connect(myBluetoothPeripheral[indexPath.row], options: nil)

    myTableView.deselectRow(at: indexPath, animated: true)
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 60
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return peripheralNames.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = gpsSettingsCustomCell(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 60), title: "Hello")
    cell.cellLabel.text = peripheralNames[indexPath.row]
    cell.cellLabel2.text = "Bluetooth Device"
    return cell

}

class BLEDeviceCustomCell: UITableViewCell {
//var cellButton: UIButton!
var cellLabel: UILabel!
var cellLabel2: UILabel!

init(frame: CGRect, title: String) {
    super.init(style: .default, reuseIdentifier: "cell")
    //super.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")

    cellLabel = UILabel(frame: CGRect(x: 10, y: 10, width: self.frame.width, height: 20))
    cellLabel.font = UIFont(name: "Titillium-Regular", size: 14.0)
    //cellLabel= UILabel(frame: CGRectMake(self.frame.width - 100, 10, 100.0, 40))
    cellLabel.textColor = UIColor.black
    //cellLabel.font = //set font here

    cellLabel2 = UILabel(frame: CGRect(x: 10, y: 30, width: self.frame.width, height: 20))
    cellLabel2.font = UIFont(name: "Titillium-Regular", size: 14.0)
    cellLabel2.textColor = UIColor.gray
    //cellButton = UIButton(frame: CGRectMake(5, 5, 50, 30))
    //cellButton.setTitle(title, forState: UIControlState.Normal)

    addSubview(cellLabel)
    addSubview(cellLabel2)
    //addSubview(cellButton)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
}

关于swift - 扫描 BLE 外围设备并连接到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53526621/

有关swift - 扫描 BLE 外围设备并连接到它的更多相关文章

  1. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  2. ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) - 2

    我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类

  3. 【鸿蒙应用开发系列】- 获取系统设备信息以及版本API兼容调用方式 - 2

    在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList​()Obt

  4. ruby - 我的 Ruby IRC 机器人没有连接到 IRC 服务器。我究竟做错了什么? - 2

    require"socket"server="irc.rizon.net"port="6667"nick="RubyIRCBot"channel="#0x40"s=TCPSocket.open(server,port)s.print("USERTesting",0)s.print("NICK#{nick}",0)s.print("JOIN#{channel}",0)这个IRC机器人没有连接到IRC服务器,我做错了什么? 最佳答案 失败并显示此消息::irc.shakeababy.net461*USER:Notenoughparame

  5. ruby-on-rails - 禁用设备的 :confirmable on-the-fly to batch-generate users - 2

    Devise是一个Ruby库,它为我提供了这个User类:classUser当写入:confirmable时,注册时会发送一封确认邮件。上周我不得不批量创建300个用户,所以我在恢复之前注释掉了:confirmable几分钟。现在我正在为用户批量创建创建一个UI,因此我需要即时添加/删除:confirmable。(我也可以直接修改Devise的源码,但我宁愿不去调和它)问题:如何即时添加/删除:confirmable? 最佳答案 WayneConrad的解决方案:user=User.newuser.skip_confirmation

  6. ruby-on-rails - 连接字符串时如何在 <%=%> block 内输出 html_safe? - 2

    考虑一下:现在这些情况:#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2我需要用其他字符串输出URL。我如何保证&符号不会被转义?由于我无法控制的原因,我无法发送&。求助!把我的头发拉到这里:\编辑:为了澄清,我实际上有一个像这样的数组:@images=[{:id=>"fooid",:url=>"http://

  7. ruby - Faye WebSocket,关闭处理程序被触发后重新连接到套接字 - 2

    我有一个super简单的脚本,它几乎包含了FayeWebSocketGitHub页面上用于处理关闭连接的内容:ws=Faye::WebSocket::Client.new(url,nil,:headers=>headers)ws.on:opendo|event|p[:open]#sendpingcommand#sendtestcommand#ws.send({command:'test'}.to_json)endws.on:messagedo|event|#hereistheentrypointfordatacomingfromtheserver.pJSON.parse(event.d

  8. ruby-on-rails - 什么会导致与 APNS 的连接间歇性断开连接? - 2

    我有一个ruby​​脚本可以打开与Apple推送服务器的连接并发送所有待处理的通知。我看不出任何原因,但当Apple断开我的脚本时,我遇到了管道损坏错误。我已经编写了我的脚本来适应这种情况,但我宁愿只是找出它发生的原因,这样我就可以在第一时间避免它。它不会始终根据特定通知断开连接。它不会以特定的字节传输大小断开连接。一切似乎都是零星的。您可以在单个连接上发送的数据传输或有效负载计数是否有某些限制?看到人们的解决方案始终保持一个连接打开,我认为这不是问题所在。我看到连接在3次通知后断开,我看到它在14次通知后断开。我从未见过它能超过14点。有没有人遇到过这种类型的问题?如何处理?

  9. ruby - 如何断开现有的 ruby​​ 续集与数据库的连接? - 2

    我的意思是之前建立的那个DB=Sequel.sqlite('my_blog.db')或DB=Sequel.connect('postgres://user:password@localhost/my_db')或DB=Sequel.postgres('my_db',:user=>'user',:password=>'password',:host=>'localhost')等等。Sequel::Database类没有名为“disconnect”的公共(public)实例方法,尽管它有一个“connect”。也许有人已经遇到过这个问题。我将不胜感激。 最佳答案

  10. ruby-on-rails - 遗留数据库的 ActiveRecord 连接表 - 2

    我有一个遗留数据库,我正在努力让ActiveRecord使用它。我遇到了连接表的问题。我有以下内容:classTvShow然后我有一个名为tvshowlinkepisode的表,它有2个字段:idShow、idEpisode所以我有2个表和它们之间的连接(多对多关系),但是连接使用非标准外键。我的第一个想法是创建一个名为TvShowEpisodeLink的模型,但没有主键。我的想法是,由于外键是非标准的,我可以使用set_foreign_key并进行一些控制。最后,我想说一些类似TvShow.find(:last).episodes或Episode.find(:last).tv_sho

随机推荐