Skip to content Skip to sidebar Skip to footer

Connected Thread Return Null Object

I am using bluetoothconnection service on my fragment but ConnectedThread return empty. Although i am calling bluetoothconnection service but doesnt work. I dont find any solutions

Solution 1:

Your code is not something I can fix directly. But I here is aan example of how I made a connection and then send data to this connected device:

publicclassBluetoothConnectionextendsThread {

publicstatic  BluetoothSocket mSocket;

private InputStream mInStream;
private OutputStream mOutStream;
privatebyte[] buffer;
private BluetoothAdapter mAdapter;
private Handler mHandler;
private String output;
private String sendString;
privateStringtempTester="";

static UUID MY_UUID;

/**
 * Constructor initializes all necessary variables.
 * @param device the device that the constructor will connect to
 */publicBluetoothConnection(BluetoothDevice device){
    MY_UUID = device.getUuids()[0].getUuid();
    mAdapter = null;
    mSocket = createMSocket(device);
    mSocket = connectSocket(mSocket);

    InputStreamtmpIn=null;
    OutputStreamtmpOut=null;
    try{
        tmpIn = mSocket.getInputStream();
        tmpOut = mSocket.getOutputStream();
    }catch (IOException e){
        e.printStackTrace();
    }
    mInStream = tmpIn;
    mOutStream = tmpOut;
    buffer = newbyte[25];
}// end constructor/**
 * Creates the main socket that will be used in connection with device.
 * @param device a BluetoothDevice
 * @return a BluetoothSocket mSocket.
 */private BluetoothSocket createMSocket(BluetoothDevice device) {
    BluetoothSockettmp=null;
    try {
        tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return tmp;
}// end createMSocket/**
 * Socket makes connection to device then returns back the updated socket.
 * @param socket BluetoothSocket
 * @return an updated version of the parameter socket.
 */private BluetoothSocket connectSocket(BluetoothSocket socket){
    try {
        // This is a blocking call and will only return on a// successful connection or an exception
        socket.connect();
        System.out.println("$$$$$$$$$$$$$$$$****** socket connected ******$$$$$$$$$$$$$$$$");
    } catch (IOException e) {
        //connection to device failed so close the sockettry {
            socket.close();
            System.out.println("$$$$$$$$$$$$$$$$****** socket closed ******$$$$$$$$$$$$$$$$");
        } catch (IOException e2) {
            e2.printStackTrace();
        }
    }
    return socket;
}// end connectSocket/**
 * Sends message back to device in the form of a byte[].
 * @param buffer byte[]
 */publicvoidwrite(byte[] buffer){
    try{
        mOutStream.write(buffer);
    }catch(IOException e) {
        e.printStackTrace();
    }
}// end write/**
 * Closes the connection with the device
 */publicvoidcancel(){
    try{
        mSocket.close();
    }catch(IOException e){
        e.printStackTrace();
    }
}// end cancel

}

Then on your main thread in an activity, you can call the following code (as long as you know the device you are connecting to):

BluetoothConnectionconnection=newBluetoothConnection(connectedDevice);

publicvoidsendData(){

   Strings= editText.getText().toString();

   byte[] b = s.getBytes();

   connection.write(b);

   //System.out.println("Bytes Sent");
}// end sendData

Post a Comment for "Connected Thread Return Null Object"