I currently have a bluetooth low energy peripheral device (written in swift, an OSX machine) with a single service. The service has two characteristics, one that is to be written to by a central device (Android) with a string representing a command, and another that the peripheral writes to with a response corresponding to the command it has received.
The question I have is this: is that a normal way to go about this? Or is there a better way to respond to a write from the central device?
I am also having some trouble writing the Android side of this, I have been using BLE Scanner to test and it has worked perfectly but I cannot find the source of an example that works similarly.
you can do this but it is not the normal way.It is also basically how you see the data exchange. BLE is designed in a way where the Slave (peripheral) provides different characteristics where you write and read data on the Slave(like a register) or the slave sends notifications to inform the master about changes.
Your response will be written in your second characteristic and you will read it out with the Ble Scanner or? So this is not a "real response sending" you just write the data and read from the Master.
The Master have always to send a request to the Slave.(except notifications)
I hope this helps you.
br
Related
Currently, I am developing an app which is communicating with one BLE Hardware which is receiving commands and responding back the command response. To Simulate hardware, We have developed one iOS Simulator app, which is working in Peripheral and responding back on requested command. But Somehow, As and when I try to write to the write characteristics, I got the status = BluetoothGatt .GATT_REQUEST_NOT_SUPPORTED in the onCharacteristicWrite callback. But somehow, I came to know that we need to implement the Central and Peripheral roles into Android app.
But I am still not sure, Do we need to implement Peripheral role as well to send and receive data in multiple packets.
I am developing the app using following nice blog post:
- https://medium.com/#avigezerit/bluetooth-low-energy-on-android-22bc7310387a
- https://android.jlelse.eu/android-bluetooth-low-energy-communication-simplified-d4fc67d3d26e
- https://www.bignerdranch.com/blog/bluetooth-low-energy-on-android-part-2/
And using following repo as learning point of view:
- https://github.com/bignerdranch/android-bluetooth-testbed/tree/a/android-ble-part-3
Thanks in advance!
Typically your phone app is the central and it communicates with a peripheral device over Bluetooth. If you want to send data to the peripheral, you can write a characteristic in the phone app, given writing is enabled for that characteristic. Your central can get data from the peripheral in 2 ways: it can either read a characteristic (if it's enabled) from the peripheral or receive notifications from the peripheral (if it's enabled). So if all you have is a central (phone app) and a peripheral (some kind of Bluetooth device) and you want to send data back and forth, you don't need to have both central and peripheral roles in the phone app. If you have some special stuff going on, it might be different for you, I don't know. I'm talking about a typical setup.
If you want to send commands to the peripheral, you could write those commands to a characteristic. The peripheral could in turn respond with notifications. This is basically the way I develop a Bluetooth solution, but it can be different depending what you want to achieve.
It takes two devices to communicate with each other.
Device A:
It will be Peripheral device which will be advertising the data. i.e.: Beacons, BLE Hardware
Device B:
It will be Central device which will send request for read,write. i.e.: Mobile
Setup for Device A:
If you don't have Peripheral, there is a way to make your android mobile to act like a Peripheral if your device is supporting that advance feature.
So before beginning, you may simply check that by using following app:
https://play.google.com/store/apps/details?id=com.kyriakosalexandrou.bluetoothsupportcheck
To make your device act like Peripheral, you may install following app which simply simulates the GATT and advertising:
https://play.google.com/store/apps/details?id=com.ble.peripheral.sim
Important: Setup service and characteristics based on your requirements, make sure characteristic is write enabled if you want write data on it.
I am a newbie to Android app coding and I am currently writing an Android app to scan BLE beacons nearby and get the data inside the beacon. Currently I am using LightBlue Explorer which is an iOS app as a BLE beacon for broadcasting data.
I want my app to receive data broadcasted by the beacon. First of all, should I use 'descriptor' as a place for putting the data? (Since I can set the characteristic user description and descriptor value inside LightBlue Explorer) If so, how can I get the value of a descriptor in my app? For now, I can get the service and the characteristics of that beacon. Thanks a lot!
I am not familiar with beacons, but i have worked on BLE devices. SO i might say you a thing or two to start conversation thread.
BLE devices around you gives out broadcast information regarding the device itself necessary for establishing the connection with device itself.
So you cant directly access the data within the device without prior connection.
This is called Generic Access Profile (GAP)
Once after establishing the connection with the device and the app(Mobile) you have to use Generic Attribute Profile (GATT) to get data necessary for the device.
You can receive data after connection by two ways.
1) Write & Notify
2) Write & Read
1) In this you have to write necessary command on the specified write characteristic for specific service on the device and device will push the necessary data on the specific Notify characteristic. Make sure the notify characteristic is enabled
2) In this you have to write necessary command on the specified write characteristic for specific service on the device and then you have to request read to the device from the app, then from the read port you can get the data given by the device.
I hope you get kinda of idea about BLE working. I have worked on BLE devices on Android side.
If any doubts just reply to thread.
To others if any correction you feel is needed, please reply.
Thank You.
I want to build a simple challenge response protocol in order to authenticate a user to an Arduino via the smartphone (Android and iOS) using BLE. I'm having problems seeing how this would be implemented with BLE. What I want to do:
Arduino sends/broadcasts a challenge
Smartphone receives challenge and computes response
Smarpthone sends response to Arduino
The way I thought about implementing this is to have the Arduino (peripheral and server) start advertising when a button is pressed. Ideally an event would be triggered on the smartphone (central and client) when a certain service shows up. Then the smartphone connects to Arduino, reads the challenge characteristic and writes the response to it. Is this a good approach?
It appears though that it is not possible to trigger an event when a BLE service appears and I would continuously need to scan with the smartphone which is very battery intensive. Not sure how to get around this? Geofencing is not really an option since it can be inside. Maybe detecting if the user is walking, biking, standing still? Any suggestions are greatly appreciated =).
On iOS BLE connectivity looks like this:
initialize CBluetoothManager,
start discovering devices through bluetooth.
when found particular peripheral that u're intrested in, connect to it via core manager and store this peripheral variable in ur Class,
then.. u should STOP discovering cuz it's terrible for batteries ;) (as u said),
after device's found, you can search through services, characteristic and at the end through descriptors <-- IN THIS ORDER otherwise u won't get any of these,
ofc, from now on you're able to store each of these vars in your Class and work with them as you wish to. BLE on iOS's based on delegates and it's own lifecycle, each time when there will be something to read the event will trigger. When you will want to send some msg to Arduino back it shouldn't be a problem. In the partial answer for your question about
'when a certain service shows up. THEN the smarphone connects to
Arduino.'
you have to make steps ABOVE in order to read services. Hope that helps.
In the context of BLE (Bluetooth Low Energy), Write Commands can be used to write from a Client to the Server, and Notifications to write from the Server to the Client. In my setup, the Client is a Central device (Android phone), and the Server is a Peripheral (dev board).
After performing several data throughput tests with multiple phones, I noticed that the throughput varies greatly with the phone, which is expected because a great deal of the BLE lower layers implementation is up to the manufacturer to figure out. But what caught my attention was that Write Command always achieve a much lower throughput that Notifications, independently from the phone. Why is that?
They should have the same throughput. Multiple write commands and notifications can be sent during one connection event. They are treated the same.
You could use an air sniffer to see if you find any problems.
How long the connection event should be open can be suggested when the connection is created and with connection parameter updates. Sadly, Android's BLE stack hard codes this to the default value, which means no recommendation. That will in practice mean you are limited to 3 or 4 packets per connection event.
I´m currently developing an BLE application, based on the Gatt sample project provided by Google.
What I want to realize is to send a notification from my Android device(smartphone) to another BLE device(e.g. TI CC2540). There are many discussion about how to receive a notification on the Internet.
However, I can't find out any discussions about sending a notification. So is there any method to sending a notification through Android device? Thank you in advance.
Technically, there is a difference between the Central/Peripheral classification, which belongs to the GAP, and the Client/Server one, which belongs to the GATT. A Central (one that scans and connects) is usually a Client, and the Peripheral (one that advertises) is usually a Server, but not necessarily.
The smartphone is the Central (it can be Peripheral as of Android 5.0 but I doubt that's the case for you), and usually it's the Client because it connects to GATT Servers located on Peripherals such as your sensor (or whatever you are building).
So, if you want to send a Notification from your device you need 2 things:
Create a GATT Server on your smartphone.
Design your Peripheral to execute GATT Client procedures (Discover Characteristics, Read/Write etc).
This may not be necessary, as #istirbu pointed out. If your application is already up-and-running, your smartphone Central is a Client. The equivalent of a Notification at Client-side is the Write Command (without response). So use that.
Notifications are used by the peripheral device to send back information to the client (eg Android device). There is no need to send a notification to the device from GATT perspective, you can perform a write operation on a characteristic. As a result of that write, or any periodic operation, the peripheral device will send back (notify) some info to the client on the same or other characteristic that you have subscribed for.