I'm developing an application that tries to connect my smartphone to a BLE device with Gatt (it can be either a smartphone or a BLE device) and read just a piece of data. This data will dynamically change and I want to keep this data as the value of a characteristic of a service. My main purpose is to read this data from the client side. Since I am connecting with Gatt, I think the only way is to store that data as the value of characteristic.
How can I add a service and a characteristic to the BLE device from the app which I created for the BLE device? I did a research and found some useful information from here. But this helps you create a service for a device you have connected to. Please help me or give me an idea. Thank you very much
Seems to me that what you are trying to do is to connect to a device and add a service on that remote device. That is not have it works and you will not be able to create an app that does this.
In BLE, both sides have a Gatt server. The addService API call in BluetoothGattService is only for adding services to your local Gatt server, not the remote server.
This is how it needs to work:
Your phone is called the central (or master), the device you are connecting to are the peripheral (or slave).
Both sides can define their local Gatt server (typically before they connect). The local Gatt server is mandatory for both sides.
If you can write the firmware running on the peripheral you can certainly create the services and characteristics you need to make your application work (again this is usually as the devices is starting, not after a connection has been made)
Your phone will connect to the peripheral and can read from or write to the Gatt server running on the remote device using the Android Gatt client. The structure however remains unchanged.
To be able to answer more detailed a lot more information on the setup is necessary.
Android (prior to 5.0) doesn't support acting as a peripheral mode you can write applications in order to act as central(master) or Observer mode.
Since Android 5.0, you can use android.bluetooth.le API along with BluetoothGattServer to adopt peripheral role on your Android 5.0+ devices.
Related
I want to use the BLE Current Time Service (0x1805) in conjunction with a BLE enabled device. My question is: Do I "need" to create an App to manage BLE connectivity and return results for the Current Time Service to my device, or does Android somehow implement a generic GATT server?
If so, how do I enable that?
So far, my BLE pairing attempts have simply errored out on the device side.
I'm not aware of a generic GATT Server on Android, but there are apps that can do the job for you. I would suggest starting with the nRF Connect app which is able to start a GATT server with a pre defined current time service.
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 developed two applications that implements both GATT roles: server and client.
In my case, the GATT server is also the GAP peripheral and the GATT client is the GAP central.
I can connect/disconnect the client to/from the server, but I found some problems when I tried to stop advertising in the moment when the client is connected to the server. In this case, the connection is broken.
I can't understand. If two devices are connected, the server shoud not need to transmit advertising packets to keep alive the connection... That's strange.
I am using the SDK version 23
After receiving STATE_CONNECTED in your GattServerCallback, before stopping advertise, run mGattServer.connect(device, false); to tell the fool Android that "Hey, I want to keep this connection alive!"!
According to the BLE specification following is allowed: A device which is in connection state can have both master and slave roles simultaneously. Master and slave devices may have multiple connections. The only restriction is the following: Two BLE devices cannot be both master and slave for each other at the same time since
two BLE devices can have only one connection at a time.
Although BLE specification states these rules, it is the BLE chip in your device that limits the number of connections and number of other BLE events that are happening simultaneously. If you see unexpected behaviors on BLE, I would suggest to try the same thing with different devices that have different BLE chips.
After a connection is established, it is lower layers' task to keep the connection alive. You don't need to do anything using the API. If the device is in connection, the connection may not be stable in case it advertises at the same time depending on the capability of the chip.
I am writing an android app to connect with BLE heart rate monitor devices.
All works well until another application tries to connect to the same service on the BLE device.
Only one application seems to be able to connect at a time.
Is it possible to connect the same BLE service to multple apps at once?
I set up my Bluetooth service in accordance with Link :
You can use BluetoothManager.getConnectedDevices() to get a list of Bluetooth devices, even ones not connected by your app. Unfortunately, you don't also get the service information for those devices.
What you can do then, is connect to the ones not already connected by your app and perform service discovery on them (BluetoothDevice.connectGatt() / BluetoothGatt.discoverServices()), then disconnect those that you aren't interested in. It's a bit cumbersome, but it seems to work for us.
No you can not do that, this is something is not supported in BLE.
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.