- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试制作一个 iOS 应用程序,该应用程序使用 CoreBluetooth 库来启动/停止和传输来自配备 HM-10 BLE 4.0 模块的多个惯性测量单元的数据。我只从一个模块连接/写入/读取没有问题,但我无法获得与多个模块的连接。我需要做什么才能做到这一点?
我曾尝试创建多个 CBManagers,但没有成功,我只是尝试通过使用单击表格中的一行来连接到多个外围设备,
for peripheral in peripherals
{
CBManager.connect(myPeripheral)
}
这些选项都没有用,我希望我能得到一些关于下一步尝试的指导。我对 Swift 还很陌生,在过去几周才开始接触它。
这是我搜索蓝牙连接时的结果:
import UIKit
import CoreBluetooth
class ScanTableViewController: UITableViewController, CBCentralManagerDelegate {
var peripherals:[CBPeripheral] = []
var manager:CBCentralManager? = nil
var parentView:MainViewController? = nil
//This is my tableView where I try to connect to all the peripherals
// that come back in the table by just selecting one peripheral.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
for peripheral in peripherals{
manager?.connect(peripheral, options: nil)
}
}
// CBCentralManagerDelegate Methods
// I think this is where I'm having issues, how do I pass all the connected peripherals on to the next view?
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
//pass reference to connected peripheral to parent view
parentView?.mainPeripheral = peripheral
peripheral.delegate = parentView
peripheral.discoverServices(nil)
//set the manager's delegate view to parent so it can call relevant disconnect methods
manager?.delegate = parentView
parentView?.customiseNavigationBar()
if let navController = self.navigationController {
navController.popViewController(animated: true)
}
print("Connected to " + peripheral.name!)
}
}
这是我使用连接的类:
// MainViewController.swift
//Bo Heyse
import UIKit
import CoreBluetooth
class MainViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
var manager:CBCentralManager? = nil
var mainPeripheral:CBPeripheral? = nil
var mainCharacteristic:CBCharacteristic? = nil
let BLEService = "FFE0"
let BLECharacteristic = "FFE1"
@IBOutlet weak var recievedMessageText: UILabel!
//how do I get this function to send a connection to all the peripherals?
@IBAction func sendButtonPressed(_ sender: AnyObject) {
let helloWorld = "Hello World!"
let dataToSend = helloWorld.data(using: String.Encoding.utf8)
if (mainPeripheral != nil) {
mainPeripheral?.writeValue(dataToSend!, for: mainCharacteristic!, type: CBCharacteristicWriteType.withoutResponse)
} else {
print("haven't discovered device yet")
}
}
// MARK: - CBCentralManagerDelegate Methods
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
mainPeripheral = nil
customiseNavigationBar()
print("Disconnected" + peripheral.name!)
}
// MARK: CBPeripheralDelegate Methods
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
for service in peripheral.services! {
print("Service found with UUID: " + service.uuid.uuidString)
//device information service
if (service.uuid.uuidString == "180A") {
peripheral.discoverCharacteristics(nil, for: service)
}
//GAP (Generic Access Profile) for Device Name
// This replaces the deprecated CBUUIDGenericAccessProfileString
if (service.uuid.uuidString == "1800") {
peripheral.discoverCharacteristics(nil, for: service)
}
//Bluno Service
if (service.uuid.uuidString == BLEService) {
peripheral.discoverCharacteristics(nil, for: service)
}
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
//get device name
if (service.uuid.uuidString == "1800") {
for characteristic in service.characteristics! {
if (characteristic.uuid.uuidString == "2A00") {
peripheral.readValue(for: characteristic)
print("Found Device Name Characteristic")
}
}
}
if (service.uuid.uuidString == "180A") {
for characteristic in service.characteristics! {
if (characteristic.uuid.uuidString == "2A29") {
peripheral.readValue(for: characteristic)
print("Found a Device Manufacturer Name Characteristic")
} else if (characteristic.uuid.uuidString == "2A23") {
peripheral.readValue(for: characteristic)
print("Found System ID")
}
}
}
if (service.uuid.uuidString == BLEService) {
for characteristic in service.characteristics! {
if (characteristic.uuid.uuidString == BLECharacteristic) {
//we'll save the reference, we need it to write data
mainCharacteristic = characteristic
//Set Notify is useful to read incoming data async
peripheral.setNotifyValue(true, for: characteristic)
print("Found Bluno Data Characteristic")
}
}
}
}
使用我发布的当前代码,我得到一个返回的错误
API MISUSE: Forcing disconnection of unused peripheral <CBPeripheral: 0x280ab4000,
identifier = B5835D05-CE72-D9EC-1526-2967566810F1, name = HEAD,
state = connected>. Did you forget to cancel the connection?
有人可以就此问题提供一些指导吗?非常感谢。
最佳答案
我想我能看出问题所在。连接到第一个外围设备后,从导航 Controller 中弹出当前 View Controller 。通过这样做,您的外围设备阵列将被释放,因为 Controller 不再可用。因此,没有引用当前发现的外围设备。当没有对外围设备的引用时,它将断开连接。 (想想关闭连接到外围设备的应用程序将强制断开连接)。
不要将 parentView 设置为外围委托(delegate),而是将自身 (ScanTableViewController
) 设置为委托(delegate)并且不要弹出 Controller。换句话说,做一些可以保留引用的事情。
关于ios - 使用一个 CBManager 连接和写入多个外设,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58021941/
我是 Xcode 新手,我编写了一些代码来发现并连接到 BLE 模块 (Adafruit Bluefruit LE)。然而扫描时却什么也没发现。有什么帮助吗? 这是我的代码 import UIKit
我正在开发一个 Android 应用程序,该应用程序充当具有服务的蓝牙外围设备角色。 当我开始转换广告时,搜索提供此服务的设备的其他 Android 设备可以看到我的设备(并且无需 PIN 即可与之配
解释: 我正在使用核心蓝牙框架连接到 BTLE 外围设备,将外围设备标识符保存在数据库中。 同时,我将外围设备保存在一个数组中以处理与外围设备的重新连接。 但是,当用户手动退出应用程序时,我的数组变为
我对蓝牙通信还很陌生。我的第一个项目打算将数据从 iOS 设备传输到 BLEshield(小芯片)。 为了测试我的中央代码,我决定将 iPhone 设置为外围设备(一旦我拿到芯片,芯片将扮演的角色)并
我正在尝试创建一个充当低功耗蓝牙 (BLE) 外围设备的 Android 5 (lollipop) 应用程序。该应用程序在支持 BLE 外设模式的 Nexus 9 上运行。到目前为止,我已经设法为服务
我想在我的电脑上通过 USB 控制遥控模型。我不需要真正的 radio 控制,所以直接通过电缆控制 Helm 机就足够了(是的,模型和控制单元之间会有电缆)。我想我会构建一些基于微 Controlle
在升级到 iOS6 之前,一切正常,但现在当我运行时 [centralManager cancelPeripheralConnection:peripheral]; 回调被触发 -(void) cen
我需要使用 LPC1769 通过 SPI 将某些数据发送到 FPGA。但是我不太明白应该如何处理这个问题。我目前有这段代码,最终出现在硬故障处理程序中。我不被允许使用 CMSIS,所以我必须自己定义寄
我正在尝试使在不同设备上运行的两个程序通过 CoreBluetooth 进行蓝牙通信。我可以从管理器找到并连接外围设备,并且可以浏览连接的外围设备中的服务,但是当我尝试发现特征时,出现错误此操作不允许
我正在开发一个系统,该系统将 BLE 设备 (TI CC2540) 作为中央设备,将 iPhone4S 上的 iOS 应用程序作为外围设备。除了我需要的 1 个功能外,一切都很好:从中央端列入白名单(
BLE 在 7 Plus (iOS 14.4.2) 和 6 (iOS 12) 上运行良好。但是在 XR (14.4.2) 上,11 连接卡在 centralManager.connect(periph
我开发了一个 BLE 设备作为主设备,并连接到作为从设备的传感器。现在我想开发一个实时显示来 self 的设备(主设备)的数据的应用程序,因此该应用程序必须充当从设备(广告)。 Windows 8 A
我对这个问题失去了理智。 事实是,一个 android 设备正在通告一个字符串值:“78d89537-4309-4728-87f6-3ab2bbe231d8”(36 字节)。我使用的特征定义为 an
我正在运行以下代码: [peripheral maximumWriteValueLengthForType:CBCharacteristicWriteWithResponse]; 在我的例子中(iPh
有人可以建议在 BLE 通信中是否可以在建立连接之前从外围设备验证中央设备? 例子:1)周边广告不断2)Central发现Peripherial并发送连接请求3)连接建立,广告停止3) Periphe
所以我有一个简单的外设应用程序,我在我的三星 S8 手机上运行的 Android Studio 中编写代码。我可以很好地设置我所有的 BLE 广告和特性,但是当我添加我的服务时。从另一个中央设备(即具
如果我想将外围设备 (Arduino) 连接到中央设备 (Iphone),是否必须将 arduino 中的数据结构化为服务和特征?我见过一些项目,其中 iphone 连接到 arduino,但 iph
我正在 iOS 上开发一个 BLE 应用程序,在与 BLE 配件建立连接后,我将 CBPeripheral UUID 保存到 NSUSerDefaults。这样,如果稍后重新启动应用程序,并且 BLE
我想知道是否可以通过 coreBluetooth 将设备作为外围设备和 iBeacons 的范围来宣传服务,同时在后台完成这两项工作?本质上,目的是确定信标的范围,然后如果信标在.Near 或.Imm
是否可以在连接到中央设备时对广告进行外围扫描?看起来它要么正在扫描广告要么已连接您的外围设备,但我需要两者都做。外围设备应该能够报告它接收到的广告中的 RSSI 测量值。 我在 linux box (
我是一名优秀的程序员,十分优秀!