0

newbie to android ! basically i trying to make a proprietary app for my product which runs on BT SPP Protocol ! so i decided use https://github.com/akexorcist/BluetoothSPPLibrary a class which handles bt service for outgoing / incoming data from bt device

Sending data is ok ! but when i trying receiving data simply cannot call / receive data inside activity class

here is my snippets of my code !

BTSpp.java

 public interface OnDataReceivedListener {
        public void onDataReceived(byte[] data, String message);  /// for sending back data to the activity 
    }

...

//this is not executing cause log is not running when receives data

public void setOnDataReceivedListener (OnDataReceivedListener listener) {
        Log.d("TAG", "setOnDataReceivedListener() returned: " );
        if(null == mDataReceivedListener)
            mDataReceivedListener = listener;
    }
...
//Here is the snippet for reading data from bt service and 
//it is showing log whenever i receives data from spp device 

case BluetoothState.MESSAGE_READ:

                    byte[] readBuf = (byte[]) msg.obj;
                    String readMessage = new String(readBuf);

                    if(readBuf != null && readBuf.length > 0) {
                        if(mDataReceivedListener != null){
                            Log.d("TAG", "handleMessage() returned: "+mDataReceivedListener ); //executes
                            mDataReceivedListener.onDataReceived(readBuf, readMessage);}
                    }
                    break;

and in my activity window onCreate method not at all executing this method MainActivity.java

BluetoothSPP bt=new BluetoothSPP(this); //initialized

{...}
bt.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() {
            @Override
            public void onDataReceived(byte[] data, String message) {
                Log.d(TAG, "onDataReceived() returned: " );
                Log.i(TAG, "onDataReceived: "+message);
                Log.i(TAG, "onDataReceived: "+data);
                processIncomingdata(message);
            }
        });

maybe this function doesn't call from service class . i literally can't find any clue ! can anyone give me some suggestion about this !

Thank you in advance !

  • What's the problem here? Your code looks fine to me. ***this is not executing cause log is not running when receives data***? Of course, it is running when call the method in `MainActivity.java`. – Darkman May 20 '21 at 07:55
  • Thank u fr replying ! yes every other methods that is uses this type of call backs method those are working fine except this ! bt.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() {} as far i know this by default get called from service class ! but not getting executed ! – Pijush Manna May 20 '21 at 08:19
  • ***... as far i know this by default get called from service class ...*** It is completely false. Why would it be called by default by a service. You'll need to show more of your code especially your ***service*** class for us to understand what's really going on. – Darkman May 20 '21 at 09:10
  • here is original source code of the service library ! [BTService](https://github.com/akexorcist/BluetoothSPPLibrary/blob/master/library/src/main/java/app/akexorcist/bluetotohspp/library/BluetoothSPP.java) and here is my source code -- [dashboard](https://pastebin.com/5umvJfM0) – Pijush Manna May 20 '21 at 09:21
  • Does `onDataReceived()` get called after all? If not there must be something wrong in `BluetoothState.MESSAGE_READ`. – Darkman May 20 '21 at 09:46
  • Solved ! the issue is elsewhere ! i wrongly handled strings ! now it's working ! but Thank you for helping :) ! – Pijush Manna May 20 '21 at 11:47

0 Answers0