4

I'm developing an Android app than can transmit data to a 4.0 Bluetooth serial device. I'm guiding by LeGatt android sample project (http://developer.android.com/samples/BluetoothLeGatt/index.html). In this project, they connect to the device, but nothing about transmission data.

For 2.0 bluetooth I can create a Socket, InputStream and OutputStream to transmit the data, something like this:

protected BluetoothSocket mySocket = null;
private InputStream MyInStream;
private OutputStream MyOutStream;
try {
                        Method m = mBluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
                        tmp = (BluetoothSocket) m.invoke(mBluetoothDevice, Integer.valueOf(1));
                    } catch (Exception e) {
                        textViewLog.append("\n"+"CONNECTION IN THREAD DIDNT WORK");
                    }
                    mySocket = tmp;

                    try {
                        mySocket.connect();
                    } catch (IOException e) {
                        textViewLog.append("\n"+e.getMessage());
                        textViewLog.append("\n"+"CONNECTION IN THREAD DIDNT WORK 2");
                    }  

                    try {
                        MyInStream = mySocket.getInputStream();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

try {
                    MyOutStream = mySocket.getOutputStream();
                } catch (IOException e) {
                    textViewLog.append("\nERROR: "+e.getMessage()); 
                }     

                try {
                    MyOutStream.write((letra+"\r").getBytes()); 
                } catch (IOException e) {
                    textViewLog.append("\nERROR: "+e.getMessage());
                }

But in 4.0 Bluetooth I can't create the Socket, because this method doesn't works

try {
                            Method m = mBluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
                            tmp = (BluetoothSocket) m.invoke(mBluetoothDevice, Integer.valueOf(1));
                        } catch (Exception e) {
                            textViewLog.append("\n"+"CONNECTION IN THREAD DIDNT WORK");
                        }

Can someone help me to reach the data transmission using my 4.0 bluetooth device.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Orlando
  • 467
  • 1
  • 7
  • 11

1 Answers1

2

Android BLE works entirely different from the Bluetooth stack, read about BLE in Wikipedia.

To send a data using BLE, you need to place your data in characteristics and send it using the gatt!

1st, you need to check your BLE device, which characteristic is used for sending data and used that characteristics for sending data!

byte[] data; //Place your data into this array of byte
characteristic.setValue(data); 
gatt.writeCharacteristic(characteristic);

Please take note that Android BLE stack is buggy, you can only writeCharacteristics once at a time, as mention in the link below!!

You can check this post about Android BLE, it will give you a clear understanding of the how the Android BLE callbacks work!

Android BLE, read and write characteristics

Community
  • 1
  • 1
Tim
  • 3,755
  • 3
  • 36
  • 57
  • Hi Tim.Thaks for your useful answer. – Orlando Jul 10 '14 at 17:23
  • Hi @Tim, how are you I've waste a lot of time trying to send data through characteristics, but I can't do it!!! (http://stackoverflow.com/questions/24720029/null-pointer-exception-error-sending-a-characteristic-ble-android?noredirect=1#comment38341920_24720029) (http://stackoverflow.com/questions/24686163/write-a-characteristic-in-ble-android) Please, if you have good documentation about the matter or a full project that do this, I will be grateful. – Orlando Jul 14 '14 at 21:44
  • just use gatt.writeCharacteristic(characteristic); to send data, make sure you use the right characteristics, and which BLE module are you using??? check with the BLE device manufacture(user manual) to see if they need certain protocol to send data! – Tim Jul 15 '14 at 00:35