Receiving data from raspberry pi in android via bluetooth -


i creating android app using going connect raspberry pi on bluetooth. issue able send data raspberry pi , visible on terminal (i using outputstream in android send data), whatever raspberry pi sending not able in inputstream.

i have read using listenrfcomm data sent device, while using createrfcomm also, have input output streams. confused use , how use.

note: using createrfcomm able send data raspberry pi successfully. data reception rasperry pi part that's remaining.

please advise accordingly.

it easier answer code, found api guide example helpful although disjointed @ first:

have thread connect:

private class connectthread extends thread {    private final bluetoothsocket mmsocket;    private final bluetoothdevice mmdevice;     public connectthread(bluetoothdevice device) {        // use temporary object later assigned mmsocket,        // because mmsocket final        bluetoothsocket tmp = null;        mmdevice = device;         // bluetoothsocket connect given bluetoothdevice        try {            // my_uuid app's uuid string, used server code            tmp = device.createrfcommsockettoservicerecord(my_uuid);        } catch (ioexception e) { }        mmsocket = tmp;    }     public void run() {        // cancel discovery because slow down connection        mbluetoothadapter.canceldiscovery();         try {            // connect device through socket. block            // until succeeds or throws exception            mmsocket.connect();        } catch (ioexception connectexception) {            // unable connect; close socket , out            try {                mmsocket.close();            } catch (ioexception closeexception) { }            return;        }         // work manage connection (in separate thread)        manageconnectedsocket(mmsocket);    }     /** cancel in-progress connection, , close socket */    public void cancel() {        try {            mmsocket.close();        } catch (ioexception e) { }    } 

}

and thread listen , work:

private class connectedthread extends thread {    private final bluetoothsocket mmsocket;    private final inputstream mminstream;    private final outputstream mmoutstream;     public connectedthread(bluetoothsocket socket) {        mmsocket = socket;        inputstream tmpin = null;        outputstream tmpout = null;         // input , output streams, using temp objects because        // member streams final        try {            tmpin = socket.getinputstream();            tmpout = socket.getoutputstream();        } catch (ioexception e) { }         mminstream = tmpin;        mmoutstream = tmpout;    }     public void run() {        byte[] buffer = new byte[1024];  // buffer store stream        int bytes; // bytes returned read()         // keep listening inputstream until exception occurs        while (true) {            try {                // read inputstream                bytes = mminstream.read(buffer);                // send obtained bytes ui activity                mhandler.obtainmessage(message_read, bytes, -1, buffer)                        .sendtotarget();            } catch (ioexception e) {                break;            }        }    }     /* call main activity send data remote device */    public void write(byte[] bytes) {        try {            mmoutstream.write(bytes);        } catch (ioexception e) { }    }     /* call main activity shutdown connection */    public void cancel() {        try {            mmsocket.close();        } catch (ioexception e) { }    } 

}

i assume if can sent have bluetoothdevice, , bluetoothadapter already, , can create , run connect thread

mconnectthread = new connectthread(bluetoothadapter.getremotedevice(deviceaddress)); mconnectthread.start(); 

in example bytes data read, sent ui thread mhandler.obtainmessage. line can edited suit whatever want received data.

example comes http://developer.android.com/guide/topics/connectivity/bluetooth.html


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -