2

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

dda
  • 6,030
  • 2
  • 25
  • 34
parliament
  • 371
  • 1
  • 5
  • 18

2 Answers2

3

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.

Vegar Westerlund
  • 1,604
  • 3
  • 16
  • 24
  • Thanx for your help, but I noticed that in android you can not connect another android device as peripheral. My aim was make a Gatt connection between two android smartphones but it is not possible yet. This is an official video to show that two android phones cannot be connected with Gatt https://developers.google.com/events/io/sessions/326240948. and also an screenshot http://imgur.com/AqIRJ7q – parliament Jan 02 '14 at 23:30
  • @user3127838: That is right, the Android API does not allow the phone to operate as a peripheral. Since iOS does support this I suspect that Android will have to do so as well before long. But the answer to your question is still the same. You cannot change the structure of a remote database (adding services and characteristics) using the GATT client. You can only read of write the values already present. The local database you can change... – Vegar Westerlund Jan 03 '14 at 08:13
2

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.

Kerem
  • 2,867
  • 24
  • 35
J Sanchez
  • 140
  • 8