Search code examples
androidbluetoothbluetooth-lowenergyandroid-bluetooth

How to pass apps notification from one android phone to another android phone using BLE


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.


Solution

  • 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.