How do I specify a lower BLE connection interval in iOS?
I believe in iOS the default is 30 ms but can be lowered to 15 ms (as mentioned in 2017 WWDC Whats New In Core Bluetooth).
Apple mentioned the connection interval can be lowered but this does not seem to be documented anywhere.
I need this to be done by the centralManager rather than the peripheral.
Current code:
self.centralManager.connect(self.peripheral, options: nil)
I would expect the options parameter could be the right place for this, but it does not seem to be so https://developer.apple.com/documentation/corebluetooth/cbcentralmanager/peripheral_connection_options.
This https://developer.apple.com/documentation/corebluetooth/cbperipheralmanager/1393277-setdesiredconnectionlatency looked promising but was for an acting peripheral rather than a central.
Is it even possible to do this using CoreBluetooth in iOS?
This question is not really about the Swift language, but about the CoreBluetooth framework. What they talked about in WWDC 2017 was that iOS acting as central now accepts a larger range of connection parameters requested by a peripheral. From what I know, there is no API to change the connection parameters from an app acting as BLE central.
Related
I know the default connection interval for CoreBluetooth is 30 ms. I've read couple of articles that claim they can reduce it 30 ms > by changing the min and max of the interval. I didn't see any explanation of how they were changing the parameters of it? I am assuming this is all in the iOS end.
Currently I am working on a project where the iOS device is sending packets to the bluetooth le device. When I was writing without response, there were a lot of packets being dropped so I added a handshake so once the bluetooth device receives a packet the iOS sends the next packet. This is currently taking a long time to upload a file since the connection interval is 30 ms which I am trying to reduce.
Any suggestions would be helpful
td;lr How do I change the connection interval on iOS
Solution So after doing research there is no public API that allows iOS devices to request for a connection interval change request. For Android this is possible.
There is no API on iOS for a app as master (using CBCentralManager) to modify the initial Connection Parameters when connecting to a peripheral.
However, the slave can suggest new connection parameters using a L2CAP Connection Parameter Update Request (see Bluetooth 4.0 specification, Volume 3, Part A, Section 4.20), which iOS will accept if they are reasonable (see Bluetooth Accessory Design Guidelines for Apple Products section 3.6 “Connection Parameters”). Peripherals should do this because different operating systems have different default connection parameters that might not be optimal for a particular peripheral. For example, if you rare implementing your peripheral in iOS or OSX, call -[CBPeripheralManager setDesiredConnectionLatency:forCentral:. Or, if you are using the TI BLE stack to program a CC2540 or the like, call the function L2CAP_ConnParamUpdateReq.
I have an app that acts as a Bluetooth LE peripheral. I have a single service with four characteristics. 2 out of the 4 are read and write only, the other two are configured as notify.
If I subscribe to one of the "notify" characteristics then the app will not disconnect until I do so manually, works well.
My issue is, If I read or write to the other characteristics, and the then am inactive for around 30 seconds, the BTLE connection disconnects from the peripheral. This may be a limitation set by apple, not sure.
Anyone know of a solution to keep the peripheral active even when there aren't any subscribers and no read or write command has been received in 30 seconds??
This is a by-product of the BLE 4.0 specs. Bluetooth Low Energy is explicitly designed to not maintain a connection for long periods which is what you are describing.
The only way to bypass this (beyond subscribing to a characteristic as you have found) would be to modify the implementation of the BLE stack on the peripheral you are connecting to and removing or elongating the interval of connection to a point that you find satisfactory.
Although this may not help you either as both sides of the BLE communication negotiate these values and iOS may impose a maximum below your requested threshold.
In my case reason was in a mismatch between characteristic properties. I wrote data to a characteristic with "waiting for response" option, but characteristic was in 'without response' state.
The symptom: write callback in delegate does not work when BLE peripheral did not write a response.
I'm Working on iOS Application(Objective-C) for Bluetooth watch which using BLE(CoreBluetooth), and my watch is having GATT Bluetooth Profile, iOS application minimum support is from iOS7.
I wants to know How can we do data transfer between iOS device and external device using Core Bluetooth framework.
Actually I'm working on the Firmware upgrade section of my Bluetooth watch,
My iOS application will get Firmware code (binary data) from web service whenever any update received and then it will send data to Bluetooth watch.
I have searched and got one Apple Sample code:
BTLE_Transfer: https://developer.apple.com/Library/ios/samplecode/BTLE_Transfer/Introduction/Intro.html
I guess sample code was not useful in my case as its having Central and Peripheral both code and transferring data between two iOS device.
is there any other way apart from this sample code for BLE data transfer? or it's possible with this sample code only?(if yes how?)
UPDATED:
My device have 1 service which have 2 characteristic one for read and one for write.
According to my workflow using write charateristic:
Using WRITECHARACTERISTIC I'm sending data of firmware code in chunks
[MYDEVICEINSTANCE writeValue:NSDATACHUNK
forCharacteristic:WRITECHARACTERISTIC
type:CBCharacteristicWriteWithResponse];
and in delegate method "didWriteValueForCharacteristic" method I'm notifying read characteristic as below
[MYDEVICEINSTANCE setNotifyValue:TRUE
forCharacteristic:READCHARACTERISTIC];
which calls "didUpdateNotificationStateForCharacteristic" delegate method inside that I'm checking if READCHARACTERISTIC isNotifying or not then I call
[MYDEVICEINSTANCE readValueForCharacteristic:READCHARACTERISTIC];
which call delegate method "didUpdateValueForCharacteristic" and I'm reading response using READCHARACTERISTIC.value
My query:
I wants to confirm MTU maximum limit allowed by Apple for External device communication from iOS application, which I'm starting in step-1 by sending NSDATACHUNK to BLE Watch from iOS app using writeValue
I have tested that I can send NSDATACHUNK of MTU=255 size and BLE watch is receiving same successfully.
I have found in "Apple Sample code: BTLE_Transfer" they are using MTU=20 but, I guess that sample code is for iOS device to iOS device communication (Please, correct me if I'm wrong)
So, If I use MTU=250 in my iOS application for BLE communication is there any chance that apple will reject my Application?
Or is there any one that can say what is the maximum limit allowed by Apple for MTU?
Every suggestion are appreciated,
Thanks in advance
You can use whatever MTU size you want (up to 512).
The value that the connection will use is always the minimum between yours and theirs.
So, if for example they want to use MTU equal to, let's say, 50, then if your MTU is lower than 50, that one will be used; otherwise, whichever value you select above 50 is meaningless, as 50 gets picked.
After connect your device yo your app you should have to write a "characteristic" with:
[YOURDEVICEINSTANCE writeValue:NSDATAVALUE forCharacteristic:YOURCHARACTESITIC type:CBCharacteristicWriteWithResponse];
We spent a lot of time working with my custom BLE device and my conclusion was:
The connection is asymmetric. (you will spent 5ms to transmit from your BLE device to your app and 20ms from your app to your BLE device)
On an iOS device, the available mtu is 20. It means that you can send 20 bytes of data every time when you set it up as BLE peripheral. If you want to communicate for more than 20 bytes, you have to handle that yourself, referencing the APPLE Central Peripheral sample code
In your case, the problem is not the mtu of iOS device but your external BLE device, because your BLE device is the peripheral device. Since your BLE device is capable of transmitting a large amount of data - 255 bytes, it will be fine to have that mtu.
The maximum MTU is negotiated between the participating devices on a low level. CoreBluetooth makes this value available via CBPeripheral's maximumWriteValueLength(for: …).
That said, unless you exactly know the behavior of your BLE peripheral, I'm afraid you can't rely on this value. There are devices out there which claim to support the BLE 5.0 specification with QUEUED WRITES – which would lead to pretty high MTUs, such as 512. They do NOT implement it though, thus packages larger than their native MTU will get silently truncated and as of iOS 14, iOS will not detect this!
Is there any way to modify CBAdvertisementDataManufacturerDataKey from CBPeripheralManager? I understand I can get the CBAdvertisementDataManufacturerDataKey from CBCentralManager.
The reason I want to do this is because I want the peripheral to advertise a 10-byte value without resorting to having the central connect to it, discover services, and then reading a characteristic which would contain the 10-byte value.
No, this is currently not possible as of iOS 7. The Manufacturer Data is reserved by Apple and is unmodifiable (at least on non-jailbroken devices). If you were writing the firmware for your own peripheral, this would not be a problem. But since the CoreBluetooth api is a layer above Apple's actual implementation of the iOS ble firmware, we face the limitations of what they actually expose. You may consider advertising you own custom service and displaying the same value you would have put in the manufacturer data, however.
I've looked everywhere and tried everything, but nothing seems to work :(
On iOS, I'm making an app (for iOS 6 and above) in which iOS devices need to exchange data. Therefore, both devices need to be peripheral and central at the same time. I've done exactly as specified in the WWDC video, but the devices can't connect successfully with each other.
When I make one device only central and the other only peripheral, the central connects seamlessly to the peripheral.
However, when both devices are peripheral and central at the same time, I get random errors: at any stage (discovering services/characteristics or setting notify value to YES) errors sometimes happen, and sometimes discoverServices doesn't even call didDiscoverServices
Is there something different I should be doing? I simply merged the peripheral and central code into one view controller. I've noticed that if device "a" connects to device "b", and then device "b" connects to device "a", it works more often than not. I manage this by using NSThread sleepForTimeInterval: manually for different amounts of time on each device, but how could I get one device to connect first (and then the other) in a reliable (and not manually pre-defined) way?
If I do get errors, usually they're simply Unknown error
Please let me know if you need any code or any other information :)
Yes, it can be in both roles at the same time. You just have to initialize a CBPeripheralManager and a CBCentralManager. As soon as the peripheral manager is initialized and you receive the POWER ON state the device starts acting as a peripheral. You can add your services at this point and receive connections from other devices. At the same time you can use the central manager to scan and initiate connections to other peripherals.
Note that you cannot connect to your own device even if it acts as a peripheral.
For your errors, I suggest:
Turn off scanning before initiating a connection. That is, scan, find peripheral, stop scan, connect. Connection and scanning do not like each other.
Use a dedicated queue for handling bluetooth events, not the main queue. [[CBCentralManager alloc] initWithDelegate:self queue:my_dedicated_bluetooth_q]
Unfortunately, the stack sometimes become unstable. Even restarts are possible. But this usually happens only under heavy loads or several simultaneous connections. Hopefully, this will be improved in iOS7.
The unfamous Unknown error started to appear for several developers recently. Judging from your description there are probably a number of reasons why your setup may fail and it would require much more info that what fits well into a SO question.
For more info I suggest you search the bluetooth-dev mailing list archives https://lists.apple.com/archives/Bluetooth-dev or send a mail Bluetooth-dev#lists.apple.com. The community provides great help if you approach with reasonable questions like this.
As per my understanding one device can work with one mode at a time . That is if the device is working in the peripheral mode then it you cant work it as a central mode .If you see some standard examples like BTLE transfer or lilke Light Blue those are working in one mode at a time .
Firstly, what do you mean "the same time"?
If you mean the device advertising to other devices while it scanning for other devices, it can not.
But you can create two threads which share same lock to advertising and scanning.
Before scanning, stop advertising, before advertising, stop scanning.
I tested on my iPhone 4s and iPad air, worked well.