0

I am developing a code to communicate to custom bluetooth circuit from Micromax Tab. Till socket creation it works fine. But when i try to connect it fails saying either Service discovery failed or host is down. I tried all the possible posts referring to this topic mentioning same problem. But end up getting same errors. I tried to change UUID but nothing works. Any clues? Below is my code:

final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
              Toast.makeText(this, "Bluetooth not found",Toast.LENGTH_LONG).show();
        }

        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }

        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
     // If there are paired devices
     if (pairedDevices.size() > 0) {
         // Loop through paired devices
         for (BluetoothDevice device : pairedDevices) {
             // Add the name and address to an array adapter to show in a ListView
             if(device.getName().equals("linvor"))
             {
                 mmDevice = device;
                 Toast.makeText(this, "Name: " + device.getName() + " And Address: " + device.getAddress(),Toast.LENGTH_LONG).show();
             }

         }
     }
     mBluetoothAdapter.cancelDiscovery();
     BluetoothSocket tmp = null;
    // UUID uuid = UUID.fromString("00001105-0000-1000-8000-00805F9B34FB");

     // MY_UUID is the app's UUID string, also used by the server code
      //  tmp = mmDevice.createRfcommSocketToServiceRecord(uuid);
     Method m = null;
    try {
        m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
        try {
            tmp = (BluetoothSocket) m.invoke(mmDevice, 1);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_LONG).show();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_LONG).show();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_LONG).show();
        }
    } catch (NoSuchMethodException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
     try {
        tmp = (BluetoothSocket) m.invoke(mmDevice, 1);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_LONG).show();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_LONG).show();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_LONG).show();
    }


     Toast.makeText(getBaseContext(), "Created socket",Toast.LENGTH_LONG).show();
     mmSocket = tmp;
     mBluetoothAdapter.cancelDiscovery();
     Toast.makeText(getBaseContext(), "Discovery cancelled",Toast.LENGTH_LONG).show();
     try {
         // Connect the device through the socket. This will block
         // until it succeeds or throws an exception
         mmSocket.connect();
         Toast.makeText(getBaseContext(), "Connected",Toast.LENGTH_LONG).show();
     } catch (IOException e1) {
         // Unable to connect; close the socket and get out
         Toast.makeText(getBaseContext(), e1.getMessage(),Toast.LENGTH_LONG).show();
         try {
             mmSocket.close();
         } catch (IOException e) { 
            Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_LONG).show();
         }
     }
Prasad Dixit
  • 31
  • 1
  • 9
  • what you are asking.Are u want to pair another remote device or send data to that device... – Satheesh Aug 01 '13 at 09:18
  • Send the data to the device. I have already paired manually. And trying to connect with the socket to send the data. The other device is a circuit having bluetooth modem – Prasad Dixit Aug 01 '13 at 09:32
  • wait i will send sample code to connect and send data to that device – Satheesh Aug 01 '13 at 09:34

1 Answers1

0

You are using this one to send image...

@Override
                        protected void onPostExecute(Void result) {
                            if(selectedImageURI!=null)
                            {
                            ContentValues values = new ContentValues();
                            values.put(BluetoothShare.URI, selectedImageURI.toString());
                            Toast.makeText(getBaseContext(), "URi : " + selectedImageURI,
                                    Toast.LENGTH_LONG).show();
                            values.put(BluetoothShare.DESTINATION, addressPairedDevice);
                            values.put(BluetoothShare.DIRECTION,
                                    BluetoothShare.DIRECTION_OUTBOUND);
                            Long ts = System.currentTimeMillis();
                            values.put(BluetoothShare.TIMESTAMP, ts);
                            getContentResolver().insert(BluetoothShare.CONTENT_URI,
                                    values);
                            }

BluetoothShare.java is in the link

How to send file using bluetooth on android programatically?

How to send file from Android device to other device through Bluetooth by code

Community
  • 1
  • 1
Satheesh
  • 1,722
  • 25
  • 35
  • Thanks Satheesh. But I would like to send a simple string as a message from tablet to my custom circuit port. Do you have any such code? – Prasad Dixit Aug 01 '13 at 09:46
  • see the below above second link...if my answer is useful for you vote me – Satheesh Aug 01 '13 at 10:12
  • I discovered the problem. It was completely my fault. The receiving device was down due to low battery level. Anyway thanks for the help and will surely vote to you. – Prasad Dixit Aug 02 '13 at 05:03