2

I am trying to pass apps notifications from one android device to another android device using android BLE.So device which receive will receive notifications will be a GATT server (so can receive notification from many clients but only one at a time) and device will send notification will be a GATT client? I am bit confused about passing apps notifications don't know how to do. Also suggest is there any other good options which I can use other than BLE.

Dhina17
  • 123
  • 1
  • 8

2 Answers2

2

BLE notifications can only be sent from a GATT server to a GATT client. In other words, you will have one Android phone that is a GATT client, listening for notifications from multiple GATT servers, one of which can be another Android phone acting as a GATT server:-

  • 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. You can then send this data via BLE notifications.
  • 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.

I recommend going through some of the Bluetooth basics as it can help you in developing your app:-

Once you have read the above, you can see the links below to accomplish your original objective of having communication between two Android devices:-

Youssif Saeed
  • 11,789
  • 4
  • 44
  • 72
  • I think the question was about Android App Notifications, not BLE notifications. But I don't know a standardized way on Android so your links might still be helpful. – Michael Kotzjan Jul 29 '21 at 09:33
  • 1
    Ah good point, I completely missed this part. I think the answer still holds though and that the links above can be used to achieve the goal of the question. – Youssif Saeed Jul 30 '21 at 06:17
  • It is still a great answer indeed – Michael Kotzjan Jul 30 '21 at 07:01
  • @YoussifSaeed are you sure about the (server: sends; client: reads) being feasible? I am currently working on an application where it is the opposite, but by new analysis, it turns out that we need the server too to send data to the client. – Cliff Burton Sep 28 '21 at 08:24
0

After doing some research, I found that I have to use Alert Notification Service with New Alert characteristic in Bluetooth Low Energy to achieve this. This way most of the fitness bands showing notification from connected device.

You have to create two Android apps.

App 1 - BLE GATT Client - This should be on the device which sends notification.

App 2 - BLE GATT server - This should be on the device which receive notification.

Follow these steps to transfer your notification:

Step 1 : Use NotificationListernerService in Client app to get your device notification data.

Step 2 : Create a byte array to write to the New Alert Characteristic by the following manner

first index - Category id - defines the type of alert. Check here

second index - Alert count - Number of the alerts

Remaining indices - Alert data

Then write the value to the New Alert characteristic

Step 3 : On the Server side, get the data in the onCharacteristicWriteRequest() method of BluetoothGattServerCallback and traverse the data in the manner how you sent.

Based on the category id, alert count, Create Notification with the received data.

For example, if you want to send missed calls notifications through BLE

Step 1 : Implement the NotificationListenerService class, Get the phone/dialer app notification based on package name and notification title on the onNotificationPosted() overridden method and get the notification text (data which we want to pass)

Step 2: Create a byte array in the manner mentioned above.

first index - category id = 4 (Which is reserved for Missed call alert. Refer this for more info)

second index - Integer 1 value in bytes(We are sending only single alert).

remaining index - data obtained in the previous step.(Make sure convert the string to byte array)

val MISSED_CALl_CATEGORY_ID = 4
private fun createNewAlertCharacteristicData(text: String): ByteArray {
    val categoryId = MISSED_CALl_CATEGORY_ID.toByte()
    val count = 1.toByte()
    val info = ByteArray(2)
    info[0] = categoryId
    info[1] = count
    val data = text.toByteArray()
    return info + data
}

Step 3: On the server side, Based on the category id, create notification for the received data.

By this way, You can pass any notifications easily. This is how smart devices like fitness bands show notifications of connected device.

Please refer the previous answer for Android BLE basics and to know how to develop BLE applications which I haven't covered here.

Dhina17
  • 123
  • 1
  • 8