Sending data to a WICED SENSE BLE device through WICED SENSE app

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hello,

I am trying to send data to a WICED SENSE BLE device through WICED SENSE app. I've  added the following code into  the a  WICED SENSE app source code, especially in the GattRequestManager. hust for testing I've used ota service:

@public boolean send(byte[] data) {

    BluetoothGattService Bgs=  findService(UUID_WS_SECURE_UPGRADE_SERVICE);
    if (mGatt == null || Bgs== null) {
        Log.w(TAG, "BluetoothGatt not initialized");
        return false;
    }

    BluetoothGattCharacteristic characteristic =
            Bgs.getCharacteristic(UUID_WS_SECURE_UPGRADE_CHARACTERISTIC_APP_INFO );

    if (characteristic == null) {
        Log.w(TAG, "Send characteristic not found");
        return false;
    }

    characteristic.setValue(data);
    characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
    return mGatt.writeCharacteristic(characteristic);
}


And the findService() is as follows:

public BluetoothGattService findService(UUID uuid) {

   if (uuid == null) {

   return null;

  }

    List<BluetoothGattService> services = mGatt.getServices();

   if (services == null) {

   return null;

  }

  Iterator<BluetoothGattService> i = services.iterator();

   while (i.hasNext()) {

  BluetoothGattService s = i.next();

   if (uuid.equals(s.getUuid())) {

   return s;

  }

  }

   return null;

}

I call the send(byte[] data) as follows from the mainActivity:

Button one= (Button) findViewById(R.id.send);

one.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {

String mode = "3";

GattRequestManager GM=new GattRequestManager(this, bluetoothDevice);

GM.send(mode.getBytes())

  }

});

I after clicking of the button I get an error message in the GattRequestManger class  in the method findService()  at List<BluetoothGattService> services = mGatt.getServices();

saying Attempt to invoke virtual method 'java.util.List android.bluetooth.BluetoothGatt.getServices()' on a null object reference I am lost here. I did run the app in debug mode and found out

it runs perfectly when the reading are received from the sensors. Any kind of help is appreciated.  jaeyoung j.t

Regards,

Adarsh









0 Likes
4 Replies
VikramR_26
Employee
Employee
25 sign-ins 10 sign-ins 10 comments on KBA

you gatt services list is null because you are performing button click action before connect, pair and discover of services.

The BluetoothGattService list object gets populated with services on discovery of services.

0 Likes
Anonymous
Not applicable

Ok, I understand that those actions are to be performed. But, I want to know do I need to perform those actions again if I will be writing into the same service or is there a way of saving an instance of the service.

0 Likes

If you do it once it should be good, android ble framework stores that for you in appropriate objects.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

vik86

I am trying to send a data to WICED SENSE device through my modified WICED SENSE app. I have defined a new characteristic to send a data  to the WICED SENSE device (as in the screenshot_2). I also sent data to the device through Smart explorer through from newly created characteristic and it works. So, how do I implement the the added screen shorin the WICED SENSE app?

0 Likes