Data passing problem from a class to a activity

87 Views Asked by At

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 !

0

There are 0 best solutions below