在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:troystribling/BlueCap开源软件地址:https://github.com/troystribling/BlueCap开源编程语言:Swift 99.7%开源软件介绍:Features
Requirements
InstallationCocoaPodsCocoaPods is an Xcode dependency manager. It is installed with the following command, gem install cocoapods
Add platform :ios, '10.0'
use_frameworks!
target 'Your Target Name' do
pod 'BlueCapKit', '~> 0.7'
end To enable CarthageCarthage is a decentralized dependency manager for Xcode projects. It can be installed using Homebrew, brew update
brew install carthage To add
To download and build carthage update then add If desired use the carthage update --no-build This will only download Manual
Getting StartedWith BlueCap it is possible to easily implement CentralManagerA simple CentralManager implementation that scans for Peripherals advertising a TiSensorTag Accelerometer Service, connects on peripheral discovery, discovers service and characteristics and subscribes to accelerometer data updates will be described. All applications begin by calling let manager = CentralManager(options: [CBCentralManagerOptionRestoreIdentifierKey : "us.gnos.BlueCap.central-manager-documentation" as NSString])
let stateChangeFuture = manager.whenStateChanges() To start scanning for public enum AppError : Error {
case invalidState
case resetting
case poweredOff
case unknown
case unlikely
}
let serviceUUID = CBUUID(string: TISensorTag.AccelerometerService.uuid)
let scanFuture = stateChangeFuture.flatMap { [weak manager] state -> FutureStream<Peripheral> in
guard let manager = manager else {
throw AppError.unlikely
}
switch state {
case .poweredOn:
return manager.startScanning(forServiceUUIDs: [serviceUUID])
case .poweredOff:
throw AppError.poweredOff
case .unauthorized, .unsupported:
throw AppError.invalidState
case .resetting:
throw AppError.resetting
case .unknown:
throw AppError.unknown
}
}
scanFuture.onFailure { [weak manager] error in
guard let appError = error as? AppError else {
return
}
switch appError {
case .invalidState:
break
case .resetting:
manager?.reset()
case .poweredOff:
break
case .unknown:
break
}
}
Here when To connect discovered peripherals the scan is followed by var peripheral: Peripheral?
let connectionFuture = scanFuture.flatMap { [weak manager] discoveredPeripheral -> FutureStream<Void> in
manager?.stopScanning()
peripheral = discoveredPeripheral
return peripheral.connect(connectionTimeout: 10.0)
} Here the scan is also stopped after a peripheral with the desired service UUID is discovered. The public enum AppError : Error {
case dataCharactertisticNotFound
case enabledCharactertisticNotFound
case updateCharactertisticNotFound
case serviceNotFound
case invalidState
case resetting
case poweredOff
case unknown
case unlikely
}
let discoveryFuture = connectionFuture.flatMap { [weak peripheral] () -> Future<Void> in
guard let peripheral = peripheral else {
throw AppError.unlikely
}
return peripheral.discoverServices([serviceUUID])
}.flatMap { [weak peripheral] () -> Future<Void> in
guard let peripheral = peripheral, let service = peripheral.services(withUUID: serviceUUID)?.first else {
throw AppError.serviceNotFound
}
return service.discoverCharacteristics([dataUUID, enabledUUID, updatePeriodUUID])
}
discoveryFuture.onFailure { [weak peripheral] error in
switch error {
case PeripheralError.disconnected:
peripheral?.reconnect()
case AppError.serviceNotFound:
break
default:
break
}
} Here a reconnect attempt is made if the public enum AppError : Error {
case dataCharactertisticNotFound
case enabledCharactertisticNotFound
case updateCharactertisticNotFound
case serviceNotFound
case invalidState
case resetting
case poweredOff
case unknown
}
var accelerometerDataCharacteristic: Characteristic?
let subscriptionFuture = discoveryFuture.flatMap { [weak peripheral] () -> Future<Void> in
guard let peripheral = peripheral, let service = peripheral.services(withUUID: serviceUUID)?.first else {
throw AppError.serviceNotFound
}
guard let dataCharacteristic = service.service.characteristics(withUUID: dataUUID)?.first else {
throw AppError.dataCharactertisticNotFound
}
accelerometerDataCharacteristic = dataCharacteristic
return dataCharacteristic.read(timeout: 10.0)
}.flatMap { [weak accelerometerDataCharacteristic] () -> Future<Void> in
guard let accelerometerDataCharacteristic = accelerometerDataCharacteristic else {
throw AppError.dataCharactertisticNotFound
}
return accelerometerDataCharacteristic.startNotifying()
}.flatMap { [weak accelerometerDataCharacteristic] () -> FutureStream<Data?> in
guard let accelerometerDataCharacteristic = accelerometerDataCharacteristic else {
throw AppError.dataCharactertisticNotFound
}
return accelerometerDataCharacteristic.receiveNotificationUpdates(capacity: 10)
}
dataUpdateFuture.onFailure { [weak peripheral] error in
switch error {
case PeripheralError.disconnected:
peripheral?.reconnect()
case AppError.serviceNotFound:
break
case AppError.dataCharactertisticNotFound:
break
default:
break
}
} These examples can be written as a single PeripheralManagerA simple First the // create accelerometer service
let accelerometerService = MutableService(uuid: TISensorTag.AccelerometerService.uuid)
// create accelerometer data characteristic
let accelerometerDataCharacteristic = MutableCharacteristic(profile: RawArrayCharacteristicProfile<TISensorTag.AccelerometerService.Data>())
// create accelerometer enabled characteristic
let accelerometerEnabledCharacteristic = MutableCharacteristic(profile: RawCharacteristicProfile<TISensorTag.AccelerometerService.Enabled>())
// create accelerometer update period characteristic
let accelerometerUpdatePeriodCharacteristic = MutableCharacteristic(profile: RawCharacteristicProfile<TISensorTag.AccelerometerService.UpdatePeriod>())
// add characteristics to service
accelerometerService.characteristics = [accelerometerDataCharacteristic, accelerometerEnabledCharacteristic, accelerometerUpdatePeriodCharacteristic] Next create the enum AppError: Error {
case invalidState
case resetting
case poweredOff
case unsupported
case unlikely
}
let manager = PeripheralManager(options: [CBPeripheralManagerOptionRestoreIdentifierKey : "us.gnos.BlueCap.peripheral-manager-documentation" as NSString])
let startAdvertiseFuture = manager.whenStateChanges().flatMap { [weak manager] state -> Future<Void> in
guard let manager = manager else {
throw AppError.unlikely
}
switch state {
case .poweredOn:
manager.removeAllServices()
return manager.add(self.accelerometerService)
case .poweredOff:
throw AppError.poweredOff
case .unauthorized, .unknown:
throw AppError.invalidState
case .unsupported:
throw AppError.unsupported
case .resetting:
throw AppError.resetting
}
}.flatMap { [weak manager] _ -> Future<Void> in
guard let manager = manager else {
throw AppError.unlikely
}
manager.startAdvertising(TISensorTag.AccelerometerService.name, uuids:[CBUUID(string: TISensorTag.AccelerometerService.uuid)])
}
startAdvertiseFuture.onFailure { [weak manager] error in
switch error {
case AppError.poweredOff:
manager?.reset()
case AppError.resetting:
manager?.reset()
default:
break
}
manager?.stopAdvertising()
} Now respond to write events on // respond to Update Period write requests
let accelerometerUpdatePeriodFuture = startAdvertiseFuture.flatMap {
accelerometerUpdatePeriodCharacteristic.startRespondingToWriteRequests()
}
accelerometerUpdatePeriodFuture.onSuccess { [weak accelerometerUpdatePeriodCharacteristic] (request, _) in
guard let accelerometerUpdatePeriodCharacteristic = accelerometerUpdatePeriodCharacteristic else {
throw AppError.unlikely
}
guard let value = request.value, value.count > 0 && value.count <= 8 else {
return
}
accelerometerUpdatePeriodCharacteristic.value = value
accelerometerUpdatePeriodCharacteristic.respondToRequest(request, withResult:CBATTError.success)
}
// respond to Enabled write requests
let accelerometerEnabledFuture = startAdvertiseFuture.flatMap {
accelerometerEnabledCharacteristic.startRespondingToWriteRequests(capacity: 2)
}
accelerometerEnabledFuture.onSuccess { [weak accelerometerUpdatePeriodCharacteristic] (request, _) in
guard let accelerometerEnabledCharacteristic = accelerometerEnabledCharacteristic else {
throw AppError.unlikely
}
guard let value = request.value, value.count == 1 else {
return
}
accelerometerEnabledCharacteristic.value = request.value
accelerometerEnabledCharacteristic.respondToRequest(request, withResult:CBATTError.success)
} See PeripheralManager Example for details. Test CasesTest Cases are available. To run type, pod install and run from test tab in generated ExamplesExamples are available that implement both CentralManager and PeripheralManager. The BluCap app is also available. The example projects are constructed using either CocoaPods or Carthage. The CocaPods projects require installing the Pod before building, pod install and Carthage projects require, carthage update
DocumentationBlueCap supports many features that simplify writing Bluetooth LE applications. Use cases with example implementations are described in each of the following sections.
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论