0

I have bluetooth device with MAC address 18xxxxxxxx18. I write the following code to get the data from this device, but got the error message of invalid UUID.

public class MainActivity extends AppCompatActivity {

private static final boolean DEBUG = true;
public static final int MESSAGE_READ = 1;
private BluetoothAdapter mBTAdapter;
private AcceptThread mAcceptThread;
TextView textview1;
private static final int REQUEST_ENABLE_BT = 3;
BluetoothAdapter btAdapter;

private static final UUID MY_UUID = UUID.fromString("18xxxxxxxx18");

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textview1 = (TextView) findViewById(R.id.textView);

    // Getting the Bluetooth adapter
    btAdapter = BluetoothAdapter.getDefaultAdapter();
    textview1.append("\nAdapter: " + btAdapter);

   // CheckBluetoothState();
    mBTAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBTAdapter == null) {
        textview1.append( "device does not support Bluetooth");
    }
}

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

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

@Override
protected void onPause() {
    mAcceptThread.cancel();

    super.onPause();
}

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

    mAcceptThread = new AcceptThread();
    mAcceptThread.start();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_ENABLE_BT) {
        if (resultCode == AppCompatActivity.RESULT_OK) {
            textview1.append("Enabled BT\n");

            setDiscoveralbe();
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

private class AcceptThread extends Thread {
    private final BluetoothServerSocket mmServerSocket;

    public AcceptThread() {
        BluetoothServerSocket tmp = null;
        try {
            tmp = mBTAdapter.listenUsingRfcommWithServiceRecord("BluetoothServer", MY_UUID);
        }
        catch (IOException e) { }
        mmServerSocket = tmp;
    }

    @Override
    public void run() {
        BluetoothSocket socket = null;
        while (true) {
            try {
                socket = mmServerSocket.accept();
            }
            catch (IOException e) {
                break;
            }

            if (socket != null) {
                manageConnectedSocket(socket);
                try {
                    mmServerSocket.close();
                } catch (IOException e) {
                }
                break;
            }
        }
    }

    public void cancel() {
        try {
            mmServerSocket.close();
        }
        catch (IOException e) { }
    }
}

public void manageConnectedSocket(BluetoothSocket socket) {
    if (DEBUG) {textview1.append("Connected");

    ConnectedThread server = new ConnectedThread(mHandler, socket);
    server.start();
    }
    else
    {
    textview1.append("Not Connected");
    }
}

private final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case MESSAGE_READ:
                byte[] readBuf = (byte[])msg.obj;
                String readMessage = new String(readBuf, 0, msg.arg1);
                textview1.append(readMessage);
        }
    }
};

private void setDiscoveralbe() {
    textview1.append("set BT as being discoverable during 2 minutes\n");
    Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 120); // 2 minutes
    startActivity(discoverableIntent);
}

}

I need this device to connect to my mobile app to get the data from the device. If I am going wrong please suggest me a way to do it?

Pratyush
  • 68
  • 8
  • Refer http://www.theappguruz.com/blog/to-allow-two-way-text-chat-over-bluetooth-in-android and http://stackoverflow.com/questions/19960663/how-to-connect-to-phone-and-send-files-chat-messages-via-bluetooth and http://stackoverflow.com/questions/12562875/android-bluetooth-example – Sathish Kumar J Jun 25 '16 at 05:24
  • Thanks @Sathish Kumar J, but my question is different. I just need the UUID from MAC address. – Pratyush Jun 25 '16 at 05:46
  • what do u try ? android to android bluetooth connection or what else? can u explain bit more? – Sathish Kumar J Jun 25 '16 at 05:56
  • No, it is not android to android bluetooth connection, it is hardware device(a digital weight machine) to android. – Pratyush Jun 25 '16 at 08:29

0 Answers0