BluetoothSPPLibrary icon indicating copy to clipboard operation
BluetoothSPPLibrary copied to clipboard

Not able to receive

Open naevtamarkus opened this issue 10 years ago • 9 comments

Hi,

I am sure I am making something wrong... so this is more a question than an "issue". Anyway, here it goes.

I have an activity with (some code stripped):

public class ViewDeviceActivity extends Activity {
    BluetoothSPP bt;

    protected void onCreate(Bundle savedInstanceState) {
        bt = new BluetoothSPP(this);
    }

    public void onStart() {
        bt.setBluetoothConnectionListener(new BluetoothSPP.BluetoothConnectionListener() {
            public void onDeviceConnected(String name, String address) {
                Log.i("Sensorino", "Connected, sending data...");
                bt.send("blah",false);
            }
        });
        bt.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() {
            public void onDataReceived(byte[] data, String message) {
                // Do something when data incoming
                Log.i("Sensorino", "Received bytes: "+data.length);
            }
        });
        bt.setupService();
        bt.startService(BluetoothState.DEVICE_OTHER);

        Log.i("Sensorino", "Connecting to " + device.getRemote_address());
        bt.connect(device.getRemote_address());
    }
}

The problem is that I am able to send (on the other end I see "blah") but not able to receive. I have tried with both the sample BluetoothChat from googlecode and from my HC-05 device: same with both.

The thing that drives me mad is that I ACTUALLY see the data getting into the device (I enabled Bluetooth Debugging and see the packet with the data in the /sdcard) but the data does not reach my activity.

Is there anything I am doing wrong?

The only strange thing I see in the logcat is: W/BluetoothAdapter﹕ getBluetoothService() called with no BluetoothManagerCallback

It would actually help if you tell me what's the minimum methods and the right order to call them in order to have I/O working. If I understand correctly (I am not an Android expert) I am doing the following:

bt = new BluetoothSPP(this);
bt.setBluetoothConnectionListener(new xxx);
bt.setOnDataReceivedListener(new xxx);
bt.setupService();
bt.startService(BluetoothState.DEVICE_OTHER);
bt.connect(device.getRemote_address());
bt.send("blah",false);

I tried shuffling the setupService and startService up and down with the same result.

Thanks!

naevtamarkus avatar Feb 23 '15 17:02 naevtamarkus

I have just found the cause of my problem: the sender was not finishing the transfer with a CR (newline)... so somehow the local (android's) bluetooth had the info, but did not delivered it to the upper layers.

Do you know where this limitation come from? Is it Android's limitation or is it the BluetoothSPP library? If it's the second, can this be parametrized/tuned? For example, on the Arduino side I look for a 0x00 character, but if it does not arrive it returns whatever is in the buffer after 0.1s without receiving data.

I guess the biggest problem is quite the opposite: what happens if I want to send a \n (CR) in the middle of the message, does it force a split in two different messages?

The second does not probably have a nice solution, but first one does.

Thanks!

naevtamarkus avatar Feb 26 '15 10:02 naevtamarkus

Hello naevtamarkus. Have you fixed the problem? Because I happened to meet the same problem with you.... Should I just send CR to the other device?

yanjingzhaisun avatar May 14 '15 08:05 yanjingzhaisun

No, the problem is not fixed AFAIK... but this has been clearly understood on Issue #13

naevtamarkus avatar May 19 '15 07:05 naevtamarkus

Thanks!

yanjingzhaisun avatar Jun 03 '15 08:06 yanjingzhaisun

I had a fix for this problem. Please read my comment on:

https://github.com/akexorcist/Android-BluetoothSPPLibrary/issues/30

msuzer avatar Oct 14 '15 19:10 msuzer

hi lazy21r, I have done what you have suggest like below

public void setOnDataReceivedListener (OnDataReceivedListener listener) {
            mDataReceivedListener = listener;
}
modify it:

public void setOnDataReceivedListener (OnDataReceivedListener listener) {
        if (mDataReceivedListener == null)
            mDataReceivedListener = listener;
}

but I am still not getting data, will you please help me what should be problem

I am getting below logs in LOGCAT

05-11 15:53:23.656 1968-1968/app.akexorcist.bluetoothspp D/Bluetooth Service: setState() 1 -> 2
05-11 15:53:23.659 1968-2611/app.akexorcist.bluetoothspp W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
05-11 15:53:23.745 1968-2149/app.akexorcist.bluetoothspp D/OpenGLRenderer: endAllStagingAnimators on 0xb36a9a00 (ListView) with handle 0xaec0fa50
05-11 15:53:24.694 1968-2611/app.akexorcist.bluetoothspp D/Bluetooth Service: setState() 2 -> 3
05-11 15:53:24.801 1968-2149/app.akexorcist.bluetoothspp V/RenderScript: 0xa09fe000 Launching thread(s), CPUs 4

siddhpuraamitr avatar May 11 '16 10:05 siddhpuraamitr

hi @siddhpuraamitr, it's been a while so I don't really remember all the project. please refer to my working source files first. If still no success, you could forward me your source files so i can give you a clue.

msuzer avatar May 11 '16 19:05 msuzer

lazy21tr: Hi I am not able to send and recieve using setup(). I read ur comment to modify public void setOnDataReceivedListener (OnDataReceivedListener listener) { mDataReceivedListener = listener; } modify it:

public void setOnDataReceivedListener (OnDataReceivedListener listener) { if (mDataReceivedListener == null) mDataReceivedListener = listener; } but I m not able to update coz I used library in build.gradle..How should I update it in my code.

vidyajejurkar avatar May 15 '17 09:05 vidyajejurkar

This worked for me In BluetoothService.java At line 358

Change this

ArrayList<Integer> arr_byte = new ArrayList<Integer>(); while (true) { try { int data = mmInStream.read(); if(data == 0x0A) { } else if(data == 0x0D) { buffer = new byte[arr_byte.size()]; for(int i = 0 ; i < arr_byte.size() ; i++) { buffer[i] = arr_byte.get(i).byteValue(); } mHandler.obtainMessage(BluetoothState.MESSAGE_READ, buffer.length, -1, buffer).sendToTarget(); arr_byte = new ArrayList<Integer>(); } else { arr_byte.add(data); } } catch (IOException e) { connectionLost(); BluetoothService.this.start(BluetoothService.this.isAndroid); break; } }

To

Log.e(BluetoothService.TAG, "BEGIN mConnectedThread"); byte[] buffer = new byte[1024];

        while (true) {
            int bytes = 0;
            try {
                bytes = this.mmInStream.read(buffer);
                if (bytes > 0) {
                    byte[] buf = new byte[bytes];
                    int i = 0;
                    while (i < bytes) {
                        buf[i] = buffer[i];
                        i++;
                    }
                    BluetoothService.this.mHandler.obtainMessage(2, bytes, -1, buf).sendToTarget();
                }
            } catch (IOException e) {
                Log.e(BluetoothService.TAG, "Error: "+e.getMessage());
                BluetoothService.this.start(BluetoothService.this.isAndroid);
                e.printStackTrace();
            }
        }

HardikPatelIVision avatar Mar 05 '18 17:03 HardikPatelIVision