akexorcist / BluetoothSPPLibrary

[UNMAINTAINED][Android] Bluetooth Serial Port Profile which comfortable to developer application to communication with microcontroller via bluetooth
Apache License 2.0
1.7k stars 572 forks source link

Not able to receive #8

Open naevtamarkus opened 9 years ago

naevtamarkus commented 9 years ago

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 commented 9 years ago

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!

yanjingzhaisun commented 9 years ago

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?

naevtamarkus commented 9 years ago

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

yanjingzhaisun commented 9 years ago

Thanks!

msuzer commented 8 years ago

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

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

siddhpuraamitr commented 8 years ago

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
msuzer commented 8 years ago

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.

vidyajejurkar commented 7 years ago

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.

HardikPatelIVision commented 6 years ago

This worked for me In BluetoothService.java At line 358

Change this

ArrayList arr_byte = new ArrayList(); 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(); } 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();
            }
        }