问题描述
我正在使用 SwiftUI 构建一个 iOS 应用程序。我是 iOS Swift 开发的初学者。我的应用程序需要连接到 BLE 设备并从中获取服务。另外,从中获取数据。但它不起作用。
import Foundation
import CoreBluetooth
class BleManager: NSObject,ObservableObject,CBCentralManagerDelegate,CBPeripheralDelegate {
@Published var peripherals = [ BleDevice ]()
@Published var services = [ BleService ]()
@Published var characteristies = [ CBCharacteristic ]()
@Published var isSwitchedOn = false
var myCentral: CBCentralManager!
var connectedPeripheral: CBPeripheral!
override init() {
super.init()
myCentral = CBCentralManager(delegate: self,queue: nil)
myCentral.delegate = self
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == .poweredOn {
isSwitchedOn = true
} else {
isSwitchedOn = false
}
}
func centralManager(_ central: CBCentralManager,diddiscover peripheral: CBPeripheral,advertisementData: [String : Any],RSSi RSSI: NSNumber) {
var peripheralName: String!
if let name = advertisementData[CBAdvertisementDataLocalNameKey] as? String {
peripheralName = name
} else {
peripheralName = "UnkNown"
}
let newPeripheral = BleDevice(id: peripherals.count,name: peripheralName,RSSi: RSSI.intValue,peripheral: peripheral)
peripherals.append(newPeripheral)
}
func peripheral(_ peripheral: CBPeripheral,didUpdateValueFor characteristic: CBCharacteristic,error: Error?) {
//Todo
}
func peripheral(_ peripheral: CBPeripheral,diddiscoverServices error: Error?) {
guard let localServices = peripheral.services else {
return
}
var newServices = [BleService]()
var id = 1
for service in localServices {
newServices.append(BleService(
id: id,uuid: service.uuid.uuidString,peripheral: peripheral,service: service
))
id = id + 1
}
services = newServices
}
func startScanning() {
myCentral.scanForperipherals(withServices: nil,options: nil)
}
func stopScanning() {
myCentral.stopScan()
}
func connect(peripheral: CBPeripheral) {
myCentral.connect(peripheral,options: nil)
connectedPeripheral = peripheral
connectedPeripheral.delegate = self
}
func disconnect(peripheral: CBPeripheral) {
myCentral.cancelPeripheralConnection(peripheral)
}
func discoverServices() {
connectedPeripheral.discoverServices(nil)
}
func discovercharacteristics(service: CBService) {
//Todo
}
func discovercharacteristics(peripheral: CBPeripheral,service: CBService) {
//Todo
}
}
我有一个 ContentView 可以扫描 BLE 设备并使用以下代码列出它们。
struct ContentView: View {
@Observedobject var bleManager = BleManager()
init() {
}
var body: some View {
NavigationView {
vstack() {
if bleManager.isSwitchedOn {
Text("Bluetooth is switched on")
.foregroundColor(.green)
}
else {
Text("Bluetooth is NOT switched on")
.foregroundColor(.red)
}
List(bleManager.peripherals){
peripheral in vstack {
Text(String(peripheral.id))
Text(peripheral.name)
NavigationLink(destination: ServicesListView(device: peripheral)) {
Text("Connect")
.padding()
.foregroundColor(/*@START_MENU_TOKEN@*/.blue/*@END_MENU_TOKEN@*/)
Spacer()
}
Spacer()
}.navigationTitle("Devices")
}
Spacer()
Button("Scan Devies") {
self.bleManager.startScanning()
}
Spacer()
Button("Stop Scanning") {
self.bleManager.stopScanning()
}
}
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
然后我有列出连接的设备/外围设备的服务的 ServicesListView。
struct ServicesListView: View {
@Observedobject var bleManager = BleManager()
var device: BleDevice
var body: some View {
vstack {
Button("Connect and get services") {
bleManager.connect(peripheral: device.peripheral)
bleManager.discoverServices()
}
Spacer()
Button("disconnect") {
bleManager.disconnect(peripheral: device.peripheral)
}
Spacer()
Text(device.name)
Spacer()
List(bleManager.services) {
service in vstack {
Text(String(service.id))
Text(service.uuid)
}
}
}
}
}
struct ServicesListView_Previews: PreviewProvider {
static var previews: some View {
ServicesListView(device: BleDevice(id: 123,name: "test",RSSi: 123,peripheral: nil))
}
}
当我单击“连接并获取服务”按钮时,它没有列出所连接外围设备的服务。我的代码有什么问题?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)