Communicating Between the Home Automation Android Application and the Qualcomm® QCA4020/4024 Platform

Connecting an Android app to the QCA402x board

The following are prerequisites for an Android application to connect to and communicate with the QCA402x board over BLE:

  • BLE support requires Android version KitKat or later
  • Location permission should be turned on in the app.
  • Bluetooth must be turned on with the Android device.

Scanning

In this code snippet, the Home Automation Android Application initiates a scan for BLE-supported devices on the QCA402x board:

  private ScanCallback mScanCallback = new ScanCallback() {
     @Override
     public void onScanResult(int callbackType, ScanResult result) {
         BluetoothDevice btDevice = result.getDevice();
                        Logger.d(TAG, "Device: " + btDevice);
         }
     }
  
  ...
  ...
  ...
  };

Connecting

The Home Automation Android Application uses the GATT profile for establishing a connection with the QCA4020 Development Board Application and interacting with it. After the user selects the QCA402x board from the list displayed, the Home Automation Android Application will connect with it and is ready for data exchange. Following is the relevant code snippet:

  private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
  
     @Override
     public void onConnectionStateChange(BluetoothGatt gatt, int status, int  newState) {
         String intentAction;
  //connection 
   if (newState == BluetoothProfile.STATE_CONNECTED) {
             intentAction = ACTION_GATT_CONNECTED;
             mConnectionState = STATE_CONNECTED;
             Logger.i(TAG, "Connected to GATT server.");
  //disconnection 
         } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
             intentAction = ACTION_GATT_DISCONNECTED;
             mConnectionState = STATE_DISCONNECTED;
             Logger.i(TAG, "Disconnected from GATT server.");
  
         } else if (newState == BluetoothProfile.STATE_CONNECTING) {
             Logger.i(TAG, "connecting from GATT server.");
         }
     }

Discovering Services

After connecting with the QCA4020 Development Board Application, the Home Automation Android Application uses the GATT profile to discover the available GATT services from the QCA4020 Development Board Application. Those services, which are provided with UUIDs, will have one or more characteristics with READ, WRITE and NOTIFY properties. Following is the relevant code snippet:

  @Override
  public void onServicesDiscovered(BluetoothGatt gatt, int status) {
     if (status == BluetoothGatt.GATT_SUCCESS) {
         broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
     } else {
         Logger.i(TAG, "onServicesDiscovered received: " + status);
     }
  }

READ

This property applies if the data is to be received on demand. The Home Automation Android Application makes this request on a specific attribute, and the QCA4020 Development Board Application responds with the requested value.

  @Override
     public void onCharacteristicRead(BluetoothGatt gatt,
                                          BluetoothGattCharacteristic characteristic,
      int status) {
  
     }
  
  
  @Override
     public void onCharacteristicChanged(BluetoothGatt gatt,
                                         BluetoothGattCharacteristic characteristic) {
         String str = null;
         try {
             str = new String(characteristic.getValue(), "UTF-8");
         } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
         }
     };

WRITE

This property applies if data needs to be sent. The Home Automation Android Application makes this request on a specific attribute with a new value to be written.

  @Override
      public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
      }
  
  @Override
    public void onCharacteristicChanged(BluetoothGatt gatt,
                                         BluetoothGattCharacteristic characteristic) {
         String str = null;
         try {
             str = new String(characteristic.getValue(), "UTF-8");
  
         } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
         }
     };

NOTIFY

This property is used for notifying the Home Automation Android Application whenever the characteristic has a new value. This operation is initiated by the QCA4020 Development Board Application when a new value is written to a notify characteristic. If the Home Automation Android Application has subscribed to notification on that characteristic, then the new value is pushed to the client when it is written.

  mBLEService.setCharacteristicNotification(characteristic, true);
  
  @Override
     public void onCharacteristicChanged(BluetoothGatt gatt,
                                         BluetoothGattCharacteristic characteristic) {
         String str = null;
         try {
             str = new String(characteristic.getValue(), "UTF-8");
         } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
         }
    };