0

Unable to receive data from a device even when it is paired , Getting IO Exception error . I have built a android app to receive data from a device, for the first time I am getting all the values but if I try for second time the data is not receiving, I have to remove it from paired list and have to pair it again, in android studio I am getting IOException error and "getBluetoothService called with no BluetoothManagerCallback".

In some devices its working fine.

logcat screenshot

enter image description here

 @Override
    protected void onResume() {
        super.onResume();

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter != null) {
            if (!mBluetoothAdapter.isEnabled())
            {

                Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
            } else {

                scanAndPairDevice();
            }

        } else {
            // TODO Android Device doesnt Support Bluetooth
        }
    }

private void scanAndPairDevice() {
        if (isDevicePaired()) {
            receiveDataFromDevice();

        } else {

            if(flag!=1) {

                showAlertDialog(mContext, "SPLTECH Device is not paired!", "Please pair the device to connect", 1);
            }
            else {

                showAlertDialog(mContext, "SPLTECH Device is not registered!", "Please conect with a registered device", 3);
            }
            //getDeviceListFromServer();
        }
    }

private boolean isDevicePaired() {

        Set<BluetoothDevice> bluetoothDeviceSet = mBluetoothAdapter.getBondedDevices();
        if (bluetoothDeviceSet.size() > 0) {

            Set<String> set=mSesssion.getDeviceList();
            Iterator<BluetoothDevice> iT=bluetoothDeviceSet.iterator();
            while(iT.hasNext()){

                BluetoothDevice device=iT.next();
                for(String device1: set)
                {
                    if(device.getName().equals("SPLTECH"))
                    {
                        flag=1;
                    }

                    if(device.getAddress().equals(device1))
                    {

                        mDevice = device;
                        mSesssion.setDeviceMacId(device.getAddress());
                        return true;
                    }

                }

            }
        }
        return false;
    }


private void receiveDataFromDevice() {

        mProgressBar.setVisibility(View.GONE);
        mHint.setVisibility(View.VISIBLE);
        Toast.makeText(mContext, "Now,Bluetooth is Ready to Take Value From Device..!", Toast.LENGTH_SHORT).show();

        myThreadConnectBTdevice = new ThreadConnectBTdevice();
        myThreadConnectBTdevice.start();
    }


 private class ThreadConnectBTdevice extends Thread {

        private BluetoothSocket bluetoothSocket = null;

        private ThreadConnectBTdevice() {

            try {
                Log.i("hii","hello");
                //bluetoothSocket = mDevice.createRfcommSocketToServiceRecord(myUUID);
                Method m = mDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
                bluetoothSocket = (BluetoothSocket) m.invoke(mDevice, 1);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.i("hii","hello");
                e.printStackTrace();

            }
        }

        @Override
        public void run() {
            boolean success = false;
            try {
                bluetoothSocket.connect();
                success = true;
            } catch (IOException e)
            {
                e.printStackTrace();

                final String eMessage = e.getMessage();
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                        // textStatus.setText("something wrong bluetoothSocket.connect(): \n" + eMessage);
                    }
                });


               try{
                    bluetoothSocket.close();

                }
                catch (IOException ex)
                {

                }

            }

            if (success) {
                //connect successful
                startThreadConnected(bluetoothSocket);

            } else {
                //fail
                Log.i("hii","in false");


            }
        }

        public void cancel() {

            try {
                bluetoothSocket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }


private void startThreadConnected(BluetoothSocket socket) {

        myThreadConnected = new ThreadConnected(socket);
        myThreadConnected.start();
    }

    /*
    ThreadConnected:
    Background Thread to handle Bluetooth data communication
    after connected
     */
    private class ThreadConnected extends Thread {
        private final BluetoothSocket connectedBluetoothSocket;
        private final InputStream connectedInputStream;
        private final OutputStream connectedOutputStream;


        public ThreadConnected(BluetoothSocket socket) {

            connectedBluetoothSocket = socket;
            InputStream in = null;
            OutputStream out = null;

            try {
                in = socket.getInputStream();
                out = socket.getOutputStream();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            connectedInputStream = in;
            connectedOutputStream = out;
        }

        @Override
        public void run() {
            byte[] buffer = new byte[1024];
            int bytes;


            while (true) {
                try {

                    bytes = connectedInputStream.read(buffer);
                    final String strReceived = new String(buffer, 0, bytes);

                    Log.d("abc", strReceived);

                    Message readMsg = mHandler.obtainMessage(
                            DATA_FROM_DEVICE, strReceived);
                    readMsg.sendToTarget();

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();


                    final String msgConnectionLost = "Connection lost:\n"
                            + e.getMessage();

                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            mHint.setText(msgConnectionLost);
                        }
                    });

                    break;
                }
            }

        }

        public void write(byte[] buffer) {
            try {
                connectedOutputStream.write(buffer);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        public void cancel() {
            try {
                connectedBluetoothSocket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

0 Answers0