在 ios 中未从 BLE 设备获取数据/服务
not getting data / services from BLE device in ios
我需要连接一个 BLE 设备,然后根据其中不同按钮发送的数据处理数据。
为此,我编写了以下代码。
import CoreBluetooth
class HomeViewController: UIViewController,CBPeripheralDelegate,CBCentralManagerDelegate
{
var centralManager : CBCentralManager!
var peri : CBPeripheral!
override func viewDidLoad()
{
super.viewDidLoad()
centralManager = CBCentralManager(delegate: self, queue: nil)
}
func centralManagerDidUpdateState(central: CBCentralManager) {
if central.state == .Unknown
{
print("Unknown")
}
else if central.state == .Unsupported
{
print("Unsupported")
}
else if central.state == .Unauthorized
{
print("Unauthorized")
}
else if central.state == .Resetting
{
print("Resetting")
}
else if central.state == .PoweredOn
{
print("Powered On")
startScan()
}
else if central.state == .PoweredOff
{
print("Powered Off")
}
}
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
print("Discovered: \(peripheral.name) at \(RSSI)")
print("AdvertisementData:\(advertisementData)")
if peri != peripheral
{
peri = peripheral
centralManager.connectPeripheral(peripheral, options: nil)
}
}
func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
print("Failed to connect \(peripheral) cause of \(error)")
}
func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
print("connected to \(peripheral)")
// centralManager.stopScan()
print("Available services:\(peripheral.services)")
}
func peripheral(peripheral: CBPeripheral, didDiscoverIncludedServicesForService service: CBService, error: NSError?) {
print("Services\(service) and error\(error)")
}
func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
print("Services and error\(error)")
}
func startScan(){
print("Scanning...")
centralManager.scanForPeripheralsWithServices(nil, options: nil)
}
}
这是我的代码日志。
Powered On
Scanning...
Discovered: Optional("**** BLE") at 127
AdvertisementData:["kCBAdvDataIsConnectable": 1, "kCBAdvDataServiceUUIDs": (
1802
)]
connected to <CBPeripheral: 0x12756d910, identifier = 6197****-EB0A-F1E8-BEF4-1AFAC629C5BC, name = **** BLE, state = connected>
Available services:nil
这是从 BLE 设备单击一个按钮时生成的输出。但是当我点击另一个按钮时,我无法接收或读取数据。
Android 同一应用的开发者已经集成了这两个按钮。
所以设备没有任何问题。
任何人都可以帮助我指导我在这段代码中出错的地方吗??
连接到外围设备后,您必须使用要发现的服务的 UUID 在外围设备上调用 discoverServices
,然后您必须发现服务的特征。如果您希望在单击按钮时进行更新,则必须为与该按钮对应的特征打开通知
如果您仍然需要帮助,我强烈推荐 link 来自 apple 的这篇文章作为后续阅读。它以比我在这里描述的更好的方式描述了您需要逐步完成的操作。
您还必须在连接到设备后发现服务和特征。
例如,在您的 "didConnectPeripheral" 方法中,您必须执行如下操作:
func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
print("connected to \(peripheral)")
peripheral.delegate = self
peripheral.discoverServices(nil)
print("Discovering services!")
}
然后:
func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
print("Discovered services: \(peripheral.services), Error\(error)")
if let services = peripheral.services {
for service in services {
peripheral.discoverCharacteristics(nil, forService: service)
}
}
}
然后你必须处理每个特征:
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError)
您必须记住手动存储每个特征,否则它们将被释放。
为了接收流数据(通知),您必须为每个特征启用通知。
peripheral.setNotifyValue(true, forCharacteristic: characteristic)
您还必须实施:
func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError)
处理传入值的顺序。
如您所见,开始需要大量样板代码。
Pandafox
的回答很完美,只是缺少一件事。
这是外设的设置委托。
这是发现外围设备、连接到它并发现其服务和特征的完整代码。
1.Connect外设
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
print("Discovered: \(peripheral.name) at \(RSSI)")
print("AdvertisementData:\(advertisementData)")
if peri != peripheral
{
peri = peripheral
peri.delegate = self
centralManager.connectPeripheral(peri, options: nil)
}
}
连接失败或成功
func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
print("Failed to connect \(peripheral) cause of \(error)")
}
func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
print("connected to \(peripheral)")
// centralManager.stopScan()
peripheral.discoverServices(nil)
}
3.DiscoverServices
func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
print("Services:\(peripheral.services) and error\(error)")
if let services = peripheral.services {
for service in services {
peripheral.discoverCharacteristics(nil, forService: service)
}
}
}
发现特征并设置通知
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?)
{
print("peripheral:\(peripheral) and service:\(service)")
for characteristic in service.characteristics!
{
peripheral.setNotifyValue(true, forCharacteristic: characteristic)
}
}
处理更新特征值的通知
func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?)
{
print("characteristic changed:\(characteristic)")
}
我需要连接一个 BLE 设备,然后根据其中不同按钮发送的数据处理数据。
为此,我编写了以下代码。
import CoreBluetooth
class HomeViewController: UIViewController,CBPeripheralDelegate,CBCentralManagerDelegate
{
var centralManager : CBCentralManager!
var peri : CBPeripheral!
override func viewDidLoad()
{
super.viewDidLoad()
centralManager = CBCentralManager(delegate: self, queue: nil)
}
func centralManagerDidUpdateState(central: CBCentralManager) {
if central.state == .Unknown
{
print("Unknown")
}
else if central.state == .Unsupported
{
print("Unsupported")
}
else if central.state == .Unauthorized
{
print("Unauthorized")
}
else if central.state == .Resetting
{
print("Resetting")
}
else if central.state == .PoweredOn
{
print("Powered On")
startScan()
}
else if central.state == .PoweredOff
{
print("Powered Off")
}
}
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
print("Discovered: \(peripheral.name) at \(RSSI)")
print("AdvertisementData:\(advertisementData)")
if peri != peripheral
{
peri = peripheral
centralManager.connectPeripheral(peripheral, options: nil)
}
}
func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
print("Failed to connect \(peripheral) cause of \(error)")
}
func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
print("connected to \(peripheral)")
// centralManager.stopScan()
print("Available services:\(peripheral.services)")
}
func peripheral(peripheral: CBPeripheral, didDiscoverIncludedServicesForService service: CBService, error: NSError?) {
print("Services\(service) and error\(error)")
}
func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
print("Services and error\(error)")
}
func startScan(){
print("Scanning...")
centralManager.scanForPeripheralsWithServices(nil, options: nil)
}
}
这是我的代码日志。
Powered On
Scanning...
Discovered: Optional("**** BLE") at 127
AdvertisementData:["kCBAdvDataIsConnectable": 1, "kCBAdvDataServiceUUIDs": (
1802
)]
connected to <CBPeripheral: 0x12756d910, identifier = 6197****-EB0A-F1E8-BEF4-1AFAC629C5BC, name = **** BLE, state = connected>
Available services:nil
这是从 BLE 设备单击一个按钮时生成的输出。但是当我点击另一个按钮时,我无法接收或读取数据。
Android 同一应用的开发者已经集成了这两个按钮。
所以设备没有任何问题。
任何人都可以帮助我指导我在这段代码中出错的地方吗??
连接到外围设备后,您必须使用要发现的服务的 UUID 在外围设备上调用 discoverServices
,然后您必须发现服务的特征。如果您希望在单击按钮时进行更新,则必须为与该按钮对应的特征打开通知
如果您仍然需要帮助,我强烈推荐 link 来自 apple 的这篇文章作为后续阅读。它以比我在这里描述的更好的方式描述了您需要逐步完成的操作。
您还必须在连接到设备后发现服务和特征。
例如,在您的 "didConnectPeripheral" 方法中,您必须执行如下操作:
func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
print("connected to \(peripheral)")
peripheral.delegate = self
peripheral.discoverServices(nil)
print("Discovering services!")
}
然后:
func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
print("Discovered services: \(peripheral.services), Error\(error)")
if let services = peripheral.services {
for service in services {
peripheral.discoverCharacteristics(nil, forService: service)
}
}
}
然后你必须处理每个特征:
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError)
您必须记住手动存储每个特征,否则它们将被释放。
为了接收流数据(通知),您必须为每个特征启用通知。
peripheral.setNotifyValue(true, forCharacteristic: characteristic)
您还必须实施:
func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError)
处理传入值的顺序。
如您所见,开始需要大量样板代码。
Pandafox
的回答很完美,只是缺少一件事。
这是外设的设置委托。
这是发现外围设备、连接到它并发现其服务和特征的完整代码。
1.Connect外设
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
print("Discovered: \(peripheral.name) at \(RSSI)")
print("AdvertisementData:\(advertisementData)")
if peri != peripheral
{
peri = peripheral
peri.delegate = self
centralManager.connectPeripheral(peri, options: nil)
}
}
连接失败或成功
func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) { print("Failed to connect \(peripheral) cause of \(error)") } func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) { print("connected to \(peripheral)") // centralManager.stopScan() peripheral.discoverServices(nil) }
3.DiscoverServices
func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
print("Services:\(peripheral.services) and error\(error)")
if let services = peripheral.services {
for service in services {
peripheral.discoverCharacteristics(nil, forService: service)
}
}
}
发现特征并设置通知
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) { print("peripheral:\(peripheral) and service:\(service)") for characteristic in service.characteristics! { peripheral.setNotifyValue(true, forCharacteristic: characteristic) } }
处理更新特征值的通知
func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) { print("characteristic changed:\(characteristic)") }