How to send data through central mode to peripheral? - ios

I'm developing an application where Client App starts BLE scanning in "Central" mode and there is a scanner App which Acts as "Peripheral",client will keep scanning for Scanners (Beacons) with specific id (where the Scanners are advertising the same id) when that both id match data is sent over BLE to the scanner app I have an example code which works perfectly from peripheral to central but i don't have idea about my case how it work.

The Demo which you are following is the correct one for a beginner. In this Demo, app is sending text data from TextView. The text data is passed in octet form, in iOS it is considered as Data (.utf8).
Now, question is, which type of data you want to send from Central to Peripheral.
The General flow is:
1. Central will proximate peripheral so that other Bluetooth manager can scan it.
2. Once anyone try to connect with that peripheral, then on successful connection, it will return available Services and Characteristics inside of those each services.
3. Based on characteristic, you can write your own logic to send text, images, audio, video or anyother data from Central to Peripheral.
Just follow the Demo link which you are following. Thanks.

Related

BLE different peripheral name on different devices

I am trying to connect BLE device using his hex number written on his peripheral name. but the peripheral name is different for some reason on different devices.
What could be the reason for that?
also i tried to find it inside advertisementData but without success.
thanks
Your device might have the shortened name in the advertisement data but have the longer one in the scan response.
If you're scanning while the app is in the foreground, you'll get the full scan response data, so it might not matter in your app. You just might have to ignore the first callback that doesn't have scan response data.

Sending data from computer to iOS using Core Bluetooth

Is it possible to send data from any computer the supports BLE (Windows or Mac) to an iOS app's CBCentralManager?
The data I want to send is purely text based. I'm searching for it but I am not being able to find if it is possible or a tutorial of how to do it.
Any help would be greatly appreciated.
Yes, normally you can do that. What you have to do is:
From the peripheral device (transmitter), advertise an CBService
(iOS) with a CBCharacteristic that support write value.
From the receiver, you create a CBCentralManager to search for the service created by the transmitter, then discover the right CBCharacteristic. Once the CBCharacteristic discovered, you can try to write value to that characteristic.
The processus look simple, but you have to do step by step on the receiver's side:
first, look for device
if device found, try to connect
then once connected to that device, try to discover the service
then once the service discovered, try to discover the characteristic
then once the characteristic is discovered, try to send
then you will receive the result of sending (ok or failed)
Take a look at my project in github, it's not complete but it show you how to exchange data between 2 BLE devices. The application is for iOS, but I'm pretty sure that you have the same code in Mac OS. I don't know how it works on PC.

If I send data by bluetooth low energy, can I know the status of transferring?

I'm trying to learn more about by this sample provided by Apple.
This sample can send text from an iOS device to another one.
There is a method called:
-(BOOL)updateValue:(NSData *)value forCharacteristic:(CBMutableCharacteristic *)characteristic onSubscribedCentrals:(NSArray *)centrals;
When this sample send text by this method, it will return a BOOL to let you know succes or not.
It's easy and clear.
But I have doubt about :
Is this provide by BLE?
Or it's because of CoreBluetooth API?
In other words , If I receive data from other BLE device(not iOS device).
Could that device know that I've received data?
In BLE, when your central device (here iOS device, assumed to be master, and also the client) wants to send data to a peripheral device (assumed to be slave and the server), it has several options to do it:
(1) write to a characteristic value
(2) write command to a characteristic value
The difference is (1) has a response from the peripheral device. (2) doesn't have that. The advantage of (2) over (1) is that (2) can send multiple data blindly while (1) has to wait until there is a response to previous write before it could send next data.
Similarly, if your peripheral device (as server) wants to send some data to your iOS device, you could either indicate (with acknowledgement) or notify (without ack).
Hope this helps.

bluetooth low energy advertising packet and topology

I want to advertise a single id lets say "stackoverflow1" on a ble device. So people close to the sensor can get this message (welcome to wwdc2012) as popups on the iPhone. That's it! meaning that there is no update on the value or anything else in other words I just want to know which room I came in. In another room there's another sensor adversing "stackoverflow2".
Now the question is, should I put the rooms' sensor (advertiser) as Peripherals and visitors' iPhones as Centrals?
If your answer is yes, can I send this Id in advertising packet? i.e. can I skip connection to the room's peripheral? Please guide me a little but on this
Thanks
Yes, the iPhones should be centrals and the in-room device should be a peripheral. It's perfectly fine to put enough data in the advertisement that the iOS app can do something useful after simply seeing the peripheral advertised without actually connecting to it. That way, multiple centrals/phones can detect proximity at once without a single phone tying up an exclusive connection to the device. Instead of specifying the room in the service name, you should be able to put it in the advertisement data for the device, giving them all the same service name. That way the iOS app doesn't need to know the completely list of rooms (i.e. services) in advance and they can be added to without changing the app.
In short, the gist of what you described should work fine, and seems like a sound approach to me.
You can include information in the advertisement from the peripheral as "Manufacturer Specific" data. Then the iOS application can get it from the advertisement data dictionary using the CBAdvertisementDataManufacturerDataKey.

Transmitting data with CoreBluetooth

I'm developing an iOS app with an accompanying Bluetooth LE peripheral. The one step I don't seem to be able to solve is how to actually transmit the data from my app to the peripheral or vice versa.
What I've built so far is a test app that can connect to my sample Bluetooth peripheral, and read all of its services/characteristics/descriptors. It can toggle notifications for a given characteristic, and write to given characteristics. It is just this last step of "transmit n bytes to the peripheral, and receive m bytes from the peripheral" that I can't seem to figure out.
Looking at the External Accessory Framework (what I would use if Apple would actually give me MFi approval for this project), they give you input and output streams on a given session to communicate with the accessory, but no such object exists for CoreBluetooth.
Is this simply an oversight on Apple's part on the functionality of CoreBluetooth? Or do I simply need to develop my own Bluetooth service profile to handle the inflow/outflow of data to and from the peripheral?
LE is fundamentally designed to work with these GATT based profiles, which are suited for monitoring sensors, not for data streams. While LE does allow for additional L2CAP streams to be opened for custom protocols, Apple's CoreBluetooth doesn't provide access to do so.
You can build a custom profile with private services and characteristics and have it work kind of like SSP; that's the way I'm using my BLE module to get data from some sensors to my app. The module I bought (Microchip's RN-4020) already has a custom profile made specifically for this known as MLDP (Microchip Low-energy Data Profile).
The way I get the data in my iOS app is by subscribing to the private characteristic, thus being notified when the values are updated. So far it has been working great, and the data rate can go up to 20 kbps according to Microchip (I haven't tested its limits, since I don't need much speed). Here's a link to Microchip's product page: http://www.microchip.com/wwwproducts/Devices.aspx?product=RN4020
Good luck!
You can use the bluetooth.org 'Immediate Alert Service' uuid=1802 with characteristic uuid=2A06 with property=write_no_response to send one byte values to your peripheral device from your iPhone. The peripheral device must be programmed to act on the data that is sent. For example, you might use a button on an iPhone app to send a hex address that causes one or more port pins to turn on or off on the peripheral. While this is not using the Alert Service as it was intended, it does provide an easy way to test out data transfer to a peripheral device. The same process could be used to send sequential data bytes similar to a serial data stream. I have not yet tried sending more complex data streams. The write_no_response does not provide any feedback to the app as to whether the data was received by the peripheral.
The IOS TemperatureSensor.xproj is an example of code for reading temperature data from a peripheral. The OSX HealthThermometerClient.xproj has the code needed to decode the somewhat complex thermometer data structure. The IOS TI-BLE-Demo.xproj TIBLECBKeyfob.m has code for reading and writing characteristic values, such as, reading temperature or battery levels from a peripheral device.

Resources