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.
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 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.
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
I am currently developing Android BLE, and encounters a lot of problems with the Android BLE stacks..
My development tool is Samsung Galaxy J with Android 4.3.
I want to know how can I read a characteristics from the BLE and the write the characteristics (is like i verify what data I have received, and then I send another data using the BLE)
and I have serious problem understanding how the Android BLE callbacks works,
I dont understand these 5 functions...and the manual is not clear, can anyone good soul explain in simple form???
onCharacteristicWrite
onCharacteristicRead
onCharacteristicChanged
onDescriptorRead
onDescriptorWrite
My current situation is, I managed to read the data in onCharacteristicChanged() callback and then I verified the received the data I try to send the data by using
characteristics.setValue(data)
gatt.writeCharacteristic(characteristics)
But, the Android BLE stack is not calling onCharacteristicsWrite() and in fact, Android just hangs there..
I try to google about Android BLE, there is not much information and only bunch of complains on how unstable the BLE stacks is......
Each of the callback from the Android BLE has its functions;
onDescriptorRead and onDescriptorWrite
This is used to write/read the configuration settings for the BLE device, some manufactures might require to send some data to the BLE device and acknowledge it by reading, before you can connect to the BLE device
onCharacteristicWrite
This is used to send data to the BLE device, usually in data mode for the BLE device.
This callback is called when you type
gatt.writeCharacteristic(characteristics);
onCharacteristicRead
This is used to read data from the BLE device
The callback is called when you write this code
gatt.readCharacteristic(characteristics);
onCharacteristicChanged
This callback is called when you are trying to send data using writeCharacteristic(characteristics) and the BLE device responds with some value.
Usually a BLE device has few characteristics, to make it simple, I name a few characteristics
WRITE - write Characteristics
READ - read Characteristics
To make it clear, when you send data, you will need to use WRITE characteristics and then when the BLE device responds Android app will call READ characteristics
A very important point to note is Android BLE stack allows you to write characteristics one at a time only!!
Example: IF you try to call write characteristics twice at a same time
gatt.writeCharacteristic(characteristics);
gatt.writeCharacteristic(characteristics);
The Android BLE stack will not issue the 2nd write characteristics!
Before setValue:characteristics.setValue(data) you should use gatt.setCharacteristicNotification(Char,true) to setNotification.
can anyone in simple words explain me the need of UUID in android bluetooth example. I have read some articles about it but it is still not getting clear the exact need of UUID.
And now let me explain you the scenario of what I want to develop:
I want to develop an android application to transfer data for example a "FILE with .xyz extension"
from my phone to the other phone over bluetooth. IT IS NOT AT ALL NECESSARY THAT THE RECEIVING PHONE SHOULD ALSO HAVE THE APPLICATION THAT I AM USING. I just want to transfer data from my application to other phone and thats it. I don't care what the receiver does with the data.
I just want to connect to a device within range and transfer that file using my application
Now how should I do this? Where does the the role of UUID come here? I have read that UUID is for my application , and both the server and the receiver should be aware of this UUID to form a connection . But what if the receiver does not have the my application? It will surely not know my Applications UUID ? then how the data transfer will be possible? I simply want to use Bluetooth without concerning a specific application.
In here, what my application should be doing? Should it be creating a server socket / a client socket or what? and Why.
An explanation in simple words is appreciated(some articles if possible). I dont want regular answers having BluetoothChat suggestions. If you don't understand the question please let me know, I will try to be more specific and elaborate it for you.
The basic goal of this question is to clarify the use of UUID and transfer data between two DEVICES (and not an application) using bluetooth from an application running on one android phone.
Even with bluetooth you can create a client-server application.. there is a BluetoothSocket
read here http://developer.android.com/reference/android/bluetooth/BluetoothSocket.html
Now, lets say you have two devices:
device A
device B
and assume that device A sending data to device B, You didn't say if device B is also sending data to device A, so I'll just describe the first scenario when A send to B.
So in that case, since all the data is stored in device A and you want to send it to device B, it will be more reasonable to create device A as a BluetoothServer and device B as BluetoothClient which listening to the server.
But.. If you want both of devices will exchange data.. you can make one of them as a server
and for each one of them create 2 threads:
Thread that sends data
thread that listening to data
so both of them can exchange data..
Another thing.. if you ever programmed a regular client server you noticed the accept() method which is blocking until there is a client which connected to server.. the same is with Bluetooth client-server application.
Summarize:
One device will act as a server - so you'll need to write a server project and install
it on the first device
Second device will act as a client - so you'll need to write a client project and
install it on the second device
Don't forget to add the bluetooth permission in the manifest file for both
projects.
Both of the projects need the same UUID as you mentioned in your question.
in simple words both of the sides need the UUID so they each know with who they're
communicate
I think it's more like a port in a regular client-server..
I read somewhere that is used for RFC communication.. as you probably know there are
some protocols for Bluetooth like RFC,SDP and etc..
EDIT:
In most of the phones there is a pairing process when you want to send data through
bluethooth. so if you don't want to use the client-server version, I think you can
do this:
Your application will search for devices to connect to. (pairing process)
After pairing you are connected to the other device and just send the data
EDIT 2:
You want to send data from A to B right?
I'll explain more clearly..
You're right when you said the client should know who is the server and need insert the port and
the IP of the server this is correct and works in this way.
Now, look..
The server listen to connection from clients, when a connection established the communication
begins.
Client ask for data
The server processing the client request and send him the related data
So any data like: Files, Databases should be stored on the server side..
Now in your case, the files you want to send are located in device A and not in device B,
So if device A is the server he will listen for connections.. when device B connects to the server
(device A) the communication begins.. Device B can request for files from Device A..
Also, since device A is the server, he can even broadcast a message.. means send the same message
for all clients which are connected to him.
But what you're want to do is to send file even if device b didn't ask for it, right?
I don't understand if you want that device B will also send file to device A, so lets divide it
into scenarios:
just device A send to B:
In this case, since the files are located in device A, means device A have the data,
device A is the server and device B is the client.
So when connection established you can send from A to B.
Both devices exchange data:
In this case, both devices should listen to each other, but just one of the should act
as a server and the other as a client. means that you need to install the serverApp on
one of them and the clientApp on the other.
But each of them can send and listen to other. so for each of the you need to create
thread that handle with the sending data and another thread that handles the receiving data
UUID is the Universal Unique Identifier. When you want to connect to any of the service that bluetooth is exposing then you should have the UUID to tell to the bluetooth software module that it has to inititae connection to this particular service. In your case in order to send file from DevA to DevB it has to use File Transfer Profile and there is specific uuid which is associated with this and this is defined by Bluetooth SIG which is the authority which qualifies bluetooth products and works on the technology. This is known by all the devices which uses bluetooth.
Tu cut the story in short when DevB receives the incoming connection request with the uuid which is unique it comes to know to which particular service of DevB the device is trying to connect to and it connects to that service. Hence if you want to send file from DevA to DevB then you need not have the same application at DevB. But you need to make sure that you use the UUID which is specified for File Transfer profile by Bluetooth SIG.
Regards,
Shripathi