Android Mobile/Device as a BLE GATT Peripheral & Central Simultaneously - java

I want to make two Android apps using BLE & GATT Protocol. Implementation like that role of the first app is GATT Client and the role of the second app is GATT Server ( or how to make the mobile phone a GATT Peripheral?) using Kotlin/Java. Both the apps will execute on a same mobile device and when I send an advertisement from one app the second app will receive this broadcast.
Is there any tutorial or article link other than the android official document where it is clearly explained? Or how should i implement this?

You can implement GATT client and GATT server concurrently. In most cases you don't need to know that the same app or another app runs the other role as well, so just develop like you usually would have done if you only implemented one role. Just keep in mind that a GATT client cannot communicate with a GATT server on the same device; it must be another physical device.

Related

Bluetooth LE send string data between Android devices via application

I'm completely clueless on how to use BLE. I need to create an application for Android devices that sends a small string from one Android device to the other. I am NOT using any sensor type devices. I only need to send a string from one phone to another phone using a BLE connection. From what I understand, I need to create a custom service and implement a GATT server on a central device and have a peripheral device access it as a client but I'm not even sure if that's what I need for my situation. I have scoured the internet and nothing I have found deals with this topic specifically. I have no idea how to create a GATT server for this purpose and what I need to include in order to create a service. Can someone please explain to me what I need to do or point me towards some resources that are useful for A COMPLETE NOVICE? Thanks.
You are right so far. You need to create two applications on two different Android devices as follows:-
Android App A on Device #1: An app to act as a GATT Server to host your data that you want to send. For that, you will need GATT Server API.
Android App B on Device #2: An app to act as a GATT Client to connect to Device #1 and read the string data from the GATT Server using GATT Client API.
BLE is a relatively complex technology but luckily there are so many resources to help you get started. I recommend that you first understand how BLE technology works and for that I the following links are useful:-
Introduction to BLE Video Series
The Basics of Bluetooth Low Energy
Introduction to BLE GATT
Getting Started with Bluetooth Low Energy
Getting Started with BLE GATT
Once you have a better understanding of the protocol, the following links are useful for understanding Android BLE development (in addition to the video posted in the comment):-
The Ultimate Guide to Android Bluetooth Low Energy
Getting Started with Bluetooth Low Energy in Android
How to use Android BLE to Communicate between Devices (With Examples)
However, before going through any of these, it's always a good idea to get a feel and practical understanding of how the technology works. For that I recommend using the nRF Connect Android app to connect and read data off devices. This way, you'll be able to relate to the new information and hopefully understand the new concepts easier.

Is it possible to connect and communicate with multiple Bluetooth Low Energy sensors with an android box simultaneously?

I want to connect and stream the multiple Bluetooth low energy sensors by matching their gatt profiles. All the sensors devices are of the same type but different mac address. So I want to connect those sensors to my mobile application and stream those data to our other application through the serial port. I know the Bluetooth can connect to 7 devices at a time.
But for connectivity with Android devices, it is not stated properly and still in a dilemma. Or else is it possible to connect external Bluetooth module to the android box to connect it to multiple devices? If yes then to do this what are the changes I need to perform on my system? How many devices can be used to communicate with the Android device at the same time? Can the android box act as a master that collects all the data from the peripherals and stream via the serial port. We have completed our process for a single sensor. Or is there any other possibility to stream multiple ble sensor data to another device through serial port write. please guide me through this process.
Execute connectGatt one time for every device, which will then give you one BluetoothGatt object per device. The callback object can be the same, or a separate instance per device, it depends on the use-case but most probably the devices serve different purposes so you'll have separate specific instances per device.
As long as you have a BluetoothDevice object, you can make a GATT connection to it. You can get it either using Bluetooth device address (which can sometimes be problematic since the API misses the random / public address type bit) or by a scan. You can have one outstanding GATT operation at a time, per BluetoothGatt object, so yes you can read / write characteristics to both devices simultaneously.
Feel free to post some source code for more analysis.

Frequent BLE disconnection - Android marshmallow+

The Problem:
The bluetooth connection between the android app(GATT client) and two TI boards(CC2640R2F, GATT server) is unstable. When the android app connects to the two boards, the bluetooth connection state of the two boards toggle rapidly between connected and disconnected states. As a result the service discovery process is affected and sometimes there are no GATT characteristics discovered(The GATT service is just empty). The problem occurs with only android devices running marshmallow(6.0+) and above.
This issue also occurs on other third-party apps available in the play store such as BLE scanner, sensorTag and nrf connect running on those android devices.
Fixes attempted on Android side:
The following fixes were attempted on the Android side but these were unsuccessful.
a time delay(tried upto 2000ms) was set between connection successful event and gatt.discoverServices() request.
Have separate device.gattConnect(...) methods for devices below and above marshmallow(6.0). Devices above marshmallow require a TRANSPORT parameter.
Switch connection between the GATT servers to ensure that only one server is connected at a given time.
Created separate Gatt callbacks for the two servers and separate threads for all the operations that took place involving the two servers.
The only fix that was successful is connecting only one server to the client. To connect another server, the app needs to be reopened. If another device is connected at the same time, the issue still occurs.
Data Collected:
The bluetooth sniffer logs were collected from the android side and are attached.
The 'btsnoop_hci_success.log' file deals with the successful BLE connection between an android device(Samsung galaxy s7 edge) running marshmallow(6.0.1) and just one server(a purifier).
The 'btsnoop_hci_fail.log' file deals with the problematic BLE connection between the same android device and two servers(a sensor and a purifier).

How to send a notification from an android app to another android device using phone number?

I am developing an android application and i need to send a notification from the app to a phone number (another device).
Can anyone tell me how can i implement this? any methods or API i can use?
Thank you in advance
I don't think you can do it without developing server-side software that Android devices will connect to. In your server-side software you can use FCM(Firebase Cloud Messaging) or any other framework/library to send notifications to one or group of devices.
Problem is that you need to have connection to the other device and you can't know if that device is connected to internet and even if it is you don't know the address of that device. 99.99% that you need server-side software.
the easiest way (but the most expensive i guess) is to have the first device send a request to a server (whose backend you have to build, eg. php) that will send a push notification to the second device via Google Cloud messaging service.
that's not so easy because the second device to receive the notification must register at GCM and the first device must identificate the id of this device (the common way is to have a common id like username)
There is on possibility without using server side code, Send SMS to the second Phone and read the SMS in second phone and display your notification in second phone. It will cost the SMS charges in both phones.
Create an API that communicates with the users of the application this will be able to send updates to the registered users at real-time.
http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

Ble Scan service of a device without connecting to it

I want to scan some device with BLE.
I only want to show my device, so for know I get the name of the device and if it is the good one I put it in my list.
if (device.getName().contains(DEVICE_NAME)) {
mDevices.put(device.hashCode(), device);
invalidateOptionsMenu();
}
My problem is that if I change the name of my device this check will be false.
So I look if it was possible to get some uuid of some services that I add to do the check with something that would not change.
And the only way is to connect to the device doing a
device.connectGatt(this, false, mGattCallback); and after with the gatt I discover service but, is it possible to discover some services from a device without connectiong to it ?
I am not with the android background, but I have understanding of how BLE works.
Take a look at all standard GATT characteristics here and all the standard GATT services here.
The characteristic Device Name (UUID is 0x2A00) is bundled into Generic Access service (UUID is 0x1800).
The GAP is always broadcasted within the advertisement packet from the GATT Server.
A GATT Client should run a discovery scan to receive the advertisement and parse the advertisement packet to receive the GAP descriptor. While you are doing this, technically speaking the connection is yet to be made.
Addressing to:
If I change the name of my device this check will be false, So I look if it was possible to get some uuid of some services that I add to do the check with something that would not change.
Assuming that you want your app to connect only to a particular set of device that has some unique identification. Say for example, a BLE-enabled digital pen configured by you should only connect to your app and no other device gadget should be able to connect with your app. If that is the use case and if you have the liberty to configure the GATT server on the BLE device then you can add some custom characteristics in GAP service on GATT server which will be unique to those devices. Needless to say, you will have to generate a 128-bit custom UUID for that characteristics and the UUID will be known to your application as well. This is more like a work-around solution.
Take a look at scan filter in android.
It allows you to scan by service UUID, which needs to be sent through advertisement by BLE peripheral. I haven't worked on android, but I am certain that the ScanFilter would allow you to to filter by either a 16-bit or a 128-but UUID.
Kindly refer Bluetooth Development Portal for more details.

Categories

Resources