toomy
toomy

Reputation: 21

send and receive plain/text string via bluetooth convets to html format

I am a junior in the world of Android ..I want to send string from application in android device to application in another device via Bluetooth .. I wrote the code below .. but the phone received the string in the HTML file format, while I want to receive it in the application Can anyone help me please???

sending code..

public class MainActivity extends AppCompatActivity {
EditText txt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt=(EditText) findViewById(R.id.txt);
}

public void sendMessage(View view) {
    String message ;
    message= txt.getText().toString();
    Intent i = new Intent();
    i.setAction(Intent.ACTION_SEND);
    i.putExtra(Intent.EXTRA_TEXT, message);
    i.setType("text/plain");
    startActivity(i);
}

}

receiving code

public class MainActivity extends AppCompatActivity {
TextView txt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt= (TextView)findViewById(R.id.txt);
    Intent intent= getIntent();
    String action= intent.getAction();
    String type= intent.getType();
    if (Intent.ACTION_SEND.equals(action) && type != null) {
        // When tyoe is 'text/plain'
        if ("text/plain".equals(type)) {
            handleSendText(intent, txt); // Handle text being sent
        }
    }
}
private void handleSendText(Intent intent, TextView txt) {
    // Get the text from intent
    String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
    // When Text is not null
    if (sharedText != null) {
        // Show the text as Toast message
        txt.setText(sharedText);
    }
}

}

Upvotes: 0

Views: 777

Answers (1)

mohammadreza khalifeh
mohammadreza khalifeh

Reputation: 1618

It's possible to implement an exception for the "Bluetooth Share" app by using EXTRA_REPLACEMENT_EXTRAS

you can see this code of sample app or see this Question and its answes...

Upvotes: 0

Related Questions