Core Bluetooth

RSS for tag

Communicate with Bluetooth 4.0 low energy devices using Core Bluetooth.

Core Bluetooth Documentation

Posts under Core Bluetooth tag

180 Posts
Sort by:
Post not yet marked as solved
1 Replies
28 Views
I'm trying an iAP2 connection (SPP open) with Bluetooth module and Apple product. But I don't know the iAP2 UUID for sure. Can someone tell me? And I'd like to know if MFi Program is required for iAP2 connection. I'd appreciate it if you could let me know the detailed procedure for iAP2 connection.
Posted
by dlehdyd.
Last updated
.
Post not yet marked as solved
0 Replies
44 Views
A device running with the following lines of code can receive a message from a peripheral. In this manner, though, I can only receive messages from one peripheral since the service and characteristic IDs are hardcoded in CentralViewModel.swift. So my question is how I can observe messages from multiple peripherals. Thanks. import SwiftUI struct ContentView: View { var body: some View { NavigationStack { VStack { NavigationLink(destination: CentralView()) { Text("Central") } .buttonStyle(.borderedProminent) .padding() } } } } // CentralView.swift // import SwiftUI struct CentralView: View { @StateObject var central: CentralViewModel = CentralViewModel() var body: some View { Text(central.message) .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) .padding(20) .onDisappear { central.stopAction() } } } // CentralViewModel.swift // import Foundation import CoreBluetooth class CentralViewModel: NSObject, ObservableObject { @Published var message: String = "" var serviceUUID: CBUUID! var characteristicUUID: CBUUID! var centralManager: CBCentralManager! var discoveredPeripheral: CBPeripheral? var transferCharacteristic: CBCharacteristic? var writeIterationsComplete = 0 //var connectionIterationsComplete = 0 let defaultIterations = 5 var data: Data = Data() override init() { super.init() self.serviceUUID = CBUUID(string: "994F8A12-FE8E-4CCB-BD7B-1AE989A32853") self.characteristicUUID = CBUUID(string: "F4BD0CA2-7581-40E2-A517-1CE275A3A749") centralManager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionShowPowerAlertKey: true]) } func stopAction() { centralManager.stopScan() } private func cleanup() { guard let discoveredPeripheral = discoveredPeripheral, case .connected = discoveredPeripheral.state else { return } for service in (discoveredPeripheral.services ?? [] as [CBService]) { for characteristic in (service.characteristics ?? [] as [CBCharacteristic]) { if characteristic.uuid == characteristicUUID && characteristic.isNotifying { self.discoveredPeripheral?.setNotifyValue(false, for: characteristic) } } } centralManager.cancelPeripheralConnection(discoveredPeripheral) } private func writeData() { guard let discoveredPeripheral = discoveredPeripheral, let transferCharacteristic = transferCharacteristic else { return } while writeIterationsComplete < defaultIterations && discoveredPeripheral.canSendWriteWithoutResponse { writeIterationsComplete += 1 } if writeIterationsComplete == defaultIterations { discoveredPeripheral.setNotifyValue(false, for: transferCharacteristic) } } } extension CentralViewModel: CBCentralManagerDelegate { func centralManagerDidUpdateState(_ central: CBCentralManager) { switch central.state { case .poweredOn: print("Power on") startScanningForPeripherals() return case .poweredOff : print("Power off") return case .resetting: print("Resetting") return case .unauthorized: print("Unauthorized") return case .unknown: print("Unknown") return case .unsupported: print("Unsupported") return @unknown default: print("An unknown central manager state has occurred") return } } func startScanningForPeripherals() { self.centralManager.scanForPeripherals(withServices: [self.serviceUUID], options: nil) } func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { guard RSSI.intValue >= -50 else { return } if discoveredPeripheral != peripheral { print("Peripheral discovered") discoveredPeripheral = peripheral centralManager.connect(peripheral, options: nil) } } func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { peripheral.delegate = self peripheral.discoverServices([serviceUUID]) print("Service discovered") } } extension CentralViewModel: CBPeripheralDelegate { func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { if error != nil { cleanup() return } guard let peripheralServices = peripheral.services else { return } for service in peripheralServices { peripheral.discoverCharacteristics([characteristicUUID], for: service) } } func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { if let error = error { print("Error discovering characteristics: \(error.localizedDescription)") cleanup() return } guard let serviceCharacteristics = service.characteristics else { return } for characteristic in serviceCharacteristics where characteristic.uuid == characteristicUUID { transferCharacteristic = characteristic peripheral.setNotifyValue(true, for: characteristic) } } func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) { if let error = error { print("Error changing notification state: \(error.localizedDescription)") return } guard characteristic.uuid == characteristicUUID else { return } if characteristic.isNotifying { print("Notification began on \(characteristic)") } else { print("Notification stopped on \(characteristic). Disconnecting") cleanup() } } func peripheralIsReady(toSendWriteWithoutResponse peripheral: CBPeripheral) { print("Peripheral is ready to send data to YOU!") } func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { if let error = error { print("Error discovering characteristics: \(error.localizedDescription)") cleanup() return } guard let characteristicData = characteristic.value, let stringFromData = String(data: characteristicData, encoding: .utf8) else { return } print("Received \(characteristicData.count) bytes: \(stringFromData)") if stringFromData == "EOM" { message = String(data: data, encoding: .utf8) ?? "" writeData() } else { data.append(characteristicData) } } }
Posted
by Tomato.
Last updated
.
Post not yet marked as solved
1 Replies
86 Views
Hi All, Please excuse my relatively basic question but I am new to swift programming and I am battling with a project. I currently have an app that receives data from an Arduino using ble and displays the data as an integer. I used this medium article From Arduino programming to iOS App development as a guide for most of the functionality but changed the sensor data being sent to better suit my project requirements. Based on the link above, I have all of the bluetooth handling in PeripheralUseCase.swift file and then I have the ConnectView file for the display: @ObservedObject var viewModel: ConnectViewModel @Environment(\.dismiss) var dismiss @State var isToggleOn: Bool = false @State var isPeripheralReady: Bool = false @State var lastPressure: Int = 0 var body: some View { VStack { Text(viewModel.connectedPeripheral.name ?? "Unknown") .font(.title) ZStack { CardView() VStack { Text("Surface") HStack { Button("Flats") { viewModel.flats() } .disabled(!isPeripheralReady) .buttonStyle(.borderedProminent) Button("FlatPoint") { viewModel.flatPoint() } .disabled(!isPeripheralReady) .buttonStyle(.borderedProminent) Button("Points") { viewModel.points() } .disabled(!isPeripheralReady) .buttonStyle(.borderedProminent) } } } ZStack { CardView() VStack { Text("\(lastPressure) kPa") .font(.largeTitle) HStack { Spacer() .frame(alignment: .trailing) Toggle("Notify", isOn: $isToggleOn) .disabled(!isPeripheralReady) Spacer() .frame(alignment: .trailing) } } } Spacer() .frame(maxHeight:.infinity) Button { dismiss() } label: { Text("Disconnect") .frame(maxWidth: .infinity) } .buttonStyle(.borderedProminent) .padding(.horizontal) } .onChange(of: isToggleOn) { newValue in if newValue == true { viewModel.startNotifyPressure() } else { viewModel.stopNotifyPressure() } let startTime = Date().timeIntervalSince1970 } .onReceive(viewModel.$state) { state in switch state { case .ready: isPeripheralReady = true case let .Pressure(temp): lastPressure = temp default: print("Not handled") } } } } struct PeripheralView_Previews: PreviewProvider { final class FakeUseCase: PeripheralUseCaseProtocol { var peripheral: Peripheral? var onWriteLedState: ((Bool) -> Void)? var onReadPressure: ((Int) -> Void)? var onPeripheralReady: (() -> Void)? var onError: ((Error) -> Void)? func writeLedState(isOn: String) {} func readPressure() { onReadPressure?(25) } func notifyPressure(_ isOn: Bool) {} } static var viewModel = { ConnectViewModel(useCase: FakeUseCase(), connectedPeripheral: .init(name: "iOSArduinoBoard")) }() static var previews: some View { ConnectView(viewModel: viewModel, isPeripheralReady: true) } } struct CardView: View { var body: some View { RoundedRectangle(cornerRadius: 16, style: .continuous) .shadow(color: Color(white: 0.5, opacity: 0.2), radius: 6) .foregroundColor(.init(uiColor: .secondarySystemBackground)) } } With the associated View Model: @Published var state = State.idle var useCase: PeripheralUseCaseProtocol let connectedPeripheral: Peripheral init(useCase: PeripheralUseCaseProtocol, connectedPeripheral: Peripheral) { self.useCase = useCase self.useCase.peripheral = connectedPeripheral self.connectedPeripheral = connectedPeripheral self.setCallbacks() } private func setCallbacks() { useCase.onPeripheralReady = { [weak self] in self?.state = .ready } useCase.onReadPressure = { [weak self] value in self?.state = .Pressure(value) } useCase.onWriteLedState = { [weak self] value in self?.state = .ledState(value) } useCase.onError = { error in print("Error \(error)") } } func startNotifyPressure() { useCase.notifyPressure(true) } func stopNotifyPressure() { useCase.notifyPressure(false) } func readPressure() { useCase.readPressure() } func flats() { useCase.writeLedState(isOn: "1") } func flatPoint() { useCase.writeLedState(isOn: "2") } func points() { useCase.writeLedState(isOn: "3") } } extension ConnectViewModel { enum State { case idle case ready case Pressure(Int) case ledState(Bool) } } What I am now trying to do is plot the data that is received from the Arduino in a line graph as it is received. Preferably the graph will scroll with time as well.
Posted
by tbuys.
Last updated
.
Post not yet marked as solved
0 Replies
146 Views
As per iOS SMS sending API, there is no option to send SMS programmatically without user consent. Developer needs to use the MessageUI framework to get iPhone user consent for sending SMS. In that case, if any third party SmartWatch connected through BLE with iPhone received SMS notification through ANCS and want to reply to that SMS, After typing and sending from Watch, user needs to perform this additional step in iPhone - give consent. But if we use Apple watch, this consent in iPhone is not required if Apple Watch is already paired with iPhone. After typing text in Apple Watch, can send SMS to receiver through utilizing iPhone's SMS service without any user interaction. What is the reason of this difference? For sending SMS, iPhone and Apple Watch needs to be paired. Similarly, even third party SmartWatch also performs BLE connection and pair together before sending SMS text from Watch to iPhone to forward to receiver. But in that case why another additional user consent is required in iPhone? If we consider iPhone and Apple Watch case, pairing with each other is considered as user consent for sending any SMS later from Watch utilizing iPhone. Then, why BLE pairing between iPhone and other third party Watch not considered as user consent and additional user consent is required for each time SMS sending?
Posted
by Unique_23.
Last updated
.
Post not yet marked as solved
2 Replies
161 Views
When calling CBCentralManager's connectPeripheral:options: with some Bluetooth devices I'm getting the "Bluetooth Pairing Request" alert on iOS and a similar "Connection Request from:" alert on macOS. Is there a way to determine upfront if the alert is going to be presented or not? Alternatively is there a way to prohibit presenting this alert (in which case the connect request could fail, which is totally fine)? I tried specifying these options: var manager: CBCentralManager ... manager.connect( peripheral, options: [ CBConnectPeripheralOptionNotifyOnConnectionKey: false, CBConnectPeripheralOptionNotifyOnDisconnectionKey: false, CBConnectPeripheralOptionNotifyOnNotificationKey: false ] ) but those didn't help (and by the doc they shouldn't help as they relate to the use case of app running in background, which is not applicable in my case – my app runs and calls connect when it is in foreground, the unwanted alert is displayed immediately).
Posted Last updated
.
Post not yet marked as solved
0 Replies
751 Views
Dear Apple support, We are working on one of the projects related to BLE, in which our BLE device is acting as a GATT client and my iPhone device (iPhone 13, 17.3) is acting as a GATT server. Issue being faced: Whenever my BLE device subscribes to the ANCS notifications, and the moment the IOS device starts bombarding with the notifications available in notification center, the BLE device controller's heap memory becomes UNAVAILABLE in runtime for this notifications' queue and eventually making the BLE controller to reset. Thereafter, whenever user tries making a new connection session of BLE, again same thing repeats. Notifications allowed : W-SMS, W-CALL, SMS, CALL, none other than this. Issue observed with : 150 notifications and above available in notification center, otherwise our product works fine with iPhone. We would like to know how we can curb the number of PRE-EXISTING type notifications available in the notification center flowing towards the BLE device? What best solution can be done at the IOS side, we also have IOS APP team for this project. What kind of APIs does Core Bluetooth framework can provide to the IOS developer in order to overcome this issue. Please suggest possible ways from the IPHONE device only to overcome this. BLE's software CANNOT BE CHANGED under any circumstances as the product is already in production and complaints are coming from end user (from the field). OUR GOAL : ONLY notifications which will be available in run time, i.e. AFTER SUCCESSFUL BLE connection established to be displayed in the BLE's display. We DON'T WANT older notifications to be displayed and flow towards BLE over ANCS. WE MUST make 'notification popup' option as ALLOW to be shared with our BLE device, otherwise SMS,CALL, W-SMS,W-CALL during live connection will not come on the BLE 's display.
Posted
by MM_GGN.
Last updated
.
Post not yet marked as solved
1 Replies
433 Views
Scenario: We have a museum where each exhibit has a UWB BLE accessory. A user moves around the museum with their phone application in the background. Once a user enters an exhibit area (Determined by UWB distances) a background task is initiated (audio/notification/widget). The Question: In our current understanding of the protocol, the user must pair to each exhibit anchor beforehand, significantly impairing the user experience and bloating their Bluetooth-paired device list. Is it possible now or potentially in the future to not require pairing for pre-approved devices? Alternatively, pairing to a hub that acts as an intermediary between the phone and accessories also seems like an appropriate compromise.
Posted Last updated
.
Post not yet marked as solved
0 Replies
92 Views
Hello, whenever I want to add a CBMutableDescriptor, either with CBMutableDescriptor(type: CBUUID(string: "2901"), value: nil) or using the defined string CBUUIDCharacteristicUserDescriptionString: I get the following error message: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Expecting NSData value type for Format descriptor' What am I doing wrong here? Thanks
Posted
by clemhousa.
Last updated
.
Post not yet marked as solved
1 Replies
109 Views
If I'm talking to a Bluetooth (BLE) device using the Core Bluetooth functions, and I need to send or receive multiple packets (such as if I need to communicate a large message) is it guaranteed that the packets will arrive in the same order they were sent?
Posted
by flarosa.
Last updated
.
Post not yet marked as solved
1 Replies
618 Views
Hello, Our team is working on a mobile app that uses Nearby Interaction framework (UWB technology) to get real-time high-accuracy ranging information with our third-party UWB-enabled device. So far everything works fine, background sessions included as explained in wwdc2022/10008 video. We are using Bluetooth LE to exchange the UWB parameters as recommended by Apple. Our next goal is to go for a full hands-free configuration of the UWB session. Let's think of an access control use case where UWB is used to measure the distance between the user and the door (credentials exchanged via Bluetooth). What we want to achieve is to start the UWB session without requiring the user to take the phone out of his wallet and open the access control app. What it works for us today is if the user starts the app, then switches off the screen and puts the phone into his pocket. The app is still running in background, so the Bluetooth Scan keeps working, the Bluetooth session starts, the UWB parameters exchanged and the UWB session started in the background, all good. But what if the user killed the app or never started it after reboot? How can we force the app to start from killed state when the BLE / UWB third-party accessory comes into proximity? iBeacon seems like a promising approach, but according to the forums, in iOS 16 the app will not be restarted from killed state. Is this correct? Any idea / suggestion about how to let the OS start our app when the user approaches the BLE / UWB accessory and the app is in killed state? Thanks in advance for your time. Regards.
Posted
by gorka-mk.
Last updated
.
Post not yet marked as solved
1 Replies
242 Views
Hello, We are working with Nearby Interaction (UWB) technology to provide high-accurate location use cases to our customers. According to Apple's documentation, in order to enable background sessions starting iOS16, the two devices need to be paired. The accessory needs to implement Nearby service and mandated encrypted characteristic. https://developer.apple.com/documentation/nearbyinteraction/ninearbyaccessoryconfiguration So far we tested this with "PIN-based" secure pairing and everything works fine. Can you confirm if "Just works" PIN-less pairing is expected to work as well here? The documentation is not clear on this respect. We would basically indicate IoNone local capabitilities for the accessory gap parameters if this is expected to work. Thanks in advance! Regards.
Posted
by gorka-mk.
Last updated
.
Post not yet marked as solved
0 Replies
108 Views
Dear Developer Community, I recently implemented privacy manifest changes in accordance with Apple guidelines. However, have encountered unexpected issues with BLE communication while our app was running in the background when there are multiple reader. During local testing in both debug and release modes within Xcode, have not experienced any problems with BLE communication, even with multiple readers. However, upon uploading the build to TestFlight for testing, i found that communication was being blocked when multiple readers are there. This behavior was quite perplexing. Upon further investigation, I decided to revert the privacy manifest changes and retested via TestFlight. Surprisingly, we did not encounter any issues with BLE communication. I am reaching out to this forum to inquire whether anyone else has encountered similar issues with BLE communication. Additionally, I have submitted a report via Feedback Assistant to seek assistance from Apple. I am particularly interested in understanding if any core logic related to BLE is affected by the privacy manifest changes. As Apple has mandated the inclusion of the privacy manifest for App Store submissions starting from Spring 2024, any insights or assistance on this matter would be greatly appreciated.
Posted
by MahaSVS.
Last updated
.
Post not yet marked as solved
4 Replies
245 Views
Using CoreBluetooth I am getting these values from CBCentralManagerDelegate's didDiscover peripheral delegate method: kCBAdvDataTxPowerLevel: 12 (could be other number like 7, 0 or a small negative number) This one is taken from advertisementData parameter. This key might be absent. rssi: -68 (or -60, -100, etc) this is taken from the "rssi" parameter (always present). I am looking for a formula to calculate approximate distance based on these two numbers. Is that possible? I know that ideally I need to know rssi0 (rssi at 1 meter), but I don't see how I can get that via CoreBluetooth API or other means (without actually measuring rssi at one meter distance which is not good for me). How could I approximate rssi0 value with "kCBAdvDataTxPowerLevel"?
Posted Last updated
.
Post not yet marked as solved
1 Replies
150 Views
Hi! I have previously developed an app that established a Bluetooth connection between two devices (two iPads). Now, I want to port the client-side app to VisionPro. Similar to the iPad app, I have supplemented my info.plist file with the following line: NSBluetoothAlwaysUsageDescription Uses BLE to communicate with devices. Despite this, when I launch the app on VisionPro, the prompt to use Bluetooth does not appear. What could be the issue? What additional settings do I need to configure to enable Bluetooth usage?
Posted
by ateszra.
Last updated
.
Post not yet marked as solved
0 Replies
189 Views
I managed to get the entry/exit for beacon region on >iOS 17.0 to <=iOS 17.1.2, but it doesn't work at all on latest iOS version 17.3.1. I have tried the new addition in iOS 17.2, assuming unmonitored as well but still no result. I'm using the sample code provided by apple, https://developer.apple.com/documentation/corelocation/monitoring_location_changes_with_core_location. Please guide.
Posted Last updated
.
Post not yet marked as solved
0 Replies
104 Views
Hi, we're having trouble with a device that changes its services to do firmware updates and does not implement the Service Changed characteristic. I understand that the iPhone caches the services, is it possible to clear this cache, or is it known how long does it last? We unfortunately cannot reset the iPhone as I've seen suggested, as this is the users' phone I'm talking about. Thanks
Posted
by disoteo.
Last updated
.
Post not yet marked as solved
0 Replies
131 Views
Hi, My app allows to connect to a bluetooth device. If the user has multiple devices he can connect all of them simultaneously, the app UI is presenting a list of all paired device. If the user open the phone bluetooth settings and forget one of the paired device, I would like to also remove it from the list of paired devices in displayed in my application. I was expecting CBCentralManager.retrievePeripherals(withIdentifiers:) to return nil if the device has been removed in the bluetooth setting but this function still return a device. Is there another solution to do that ? Thanks
Posted Last updated
.
Post marked as solved
3 Replies
204 Views
Where's CBAdvDataManufacturerData format documented? It looks like there's the company code stored in little endian order in the first two bytes (referencing the companies found in company_identifiers.yaml on bluetooth site). And the rest is arbitrary? I'd like to see the official documentation about this. Thank you.
Posted Last updated
.