dont work "input tap x y" with external app - android

Try to make my app tapping on right places on some apps. I use Runtime.getRuntime().exec() with "input tap x y" adb shell command, but it working only on my activity. What can i do to make it work?
Thread r;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button BtnStart = (Button) findViewById(R.id.btnStart);
try {
Runtime.getRuntime().exec("su");
} catch (IOException e) {
e.printStackTrace();
}
r = new Thread() {
#Override
public void run() {
try{
synchronized (this){
sleep(10000);
}
} catch (InterruptedException e){
}
try {
Runtime.getRuntime().exec("input tap 120 1050");
} catch (IOException e) {
e.printStackTrace();
}
}
};
View.OnClickListener StartClick = new View.OnClickListener(){
#Override
public void onClick(View v) {
StartApp("some.app.here");
r.start();
}
};
BtnStart.setOnClickListener(StartClick);
}
public void StartApp(String AppName){
Intent LaunchApp = getPackageManager().getLaunchIntentForPackage(AppName);
if (LaunchApp != null) {
startActivity(LaunchApp);
}
}

Find a way like:
try {
dataOutputStream.writeBytes(command + "\n");
dataOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
Where "command" some command with "su" preveosly

Related

Get Video Play Time android from videoplayer

I write this function to get video time . This functions works fine but when I backpress then it throws exception .
public void videoTimer ()
{
try
{
running = true;
final int duration = mVideoView.getDuration();
thread = new Thread(new Runnable()
{
public void run()
{
do
{
tvVideoTimer.post(new Runnable()
{
public void run()
{
int time = (duration - mVideoView.getCurrentPosition()) / 1000;
tvVideoTimer.setText(getTimeString(mVideoView.getCurrentPosition()));
}
});
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
if (!running) break;
}
while (mVideoView.getCurrentPosition() <= duration);
}
});
thread.start();
}
catch (Exception e)
{
e.printStackTrace();
}
}

How to display Hindi or Gujarati Font using Bluetooth Printer?

I am using Android-Bluetooth-Printer.it works fine if i type English text in edittext,but the issue is if i select hindi language from soft keyboard,and then i enter some hindi text and try to print,but it display nothing in paper.
public class BlueToothPrinterApp extends Activity
{
EditText message;
Button printbtn;
byte FONT_TYPE;
private static BluetoothSocket btsocket;
private static OutputStream btoutputstream;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
message = (EditText)findViewById(R.id.message);
printbtn = (Button)findViewById(R.id.printButton);
printbtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
connect();
}
});
}
protected void connect() {
if(btsocket == null){
Intent BTIntent = new Intent(getApplicationContext(), BTDeviceList.class);
this.startActivityForResult(BTIntent, BTDeviceList.REQUEST_CONNECT_BT);
}
else{
OutputStream opstream = null;
try {
opstream = btsocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
btoutputstream = opstream;
print_bt();
}
}
private void print_bt() {
try {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
btoutputstream = btsocket.getOutputStream();
byte[] printformat = { 0x1B, 0x21, FONT_TYPE };
btoutputstream.write(printformat);
String msg = message.getText().toString();
btoutputstream.write(msg.getBytes());
btoutputstream.write(0x0D);
btoutputstream.write(0x0D);
btoutputstream.write(0x0D);
btoutputstream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
try {
if(btsocket!= null){
btoutputstream.close();
btsocket.close();
btsocket = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
btsocket = BTDeviceList.getSocket();
if(btsocket != null){
print_bt();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Can any one help me with this?
Printers have predefined character sets built in. These character sets are known as code pages.
Your Bluetooth printer, like most POS printers doesn't seem to support Unicode.
To solve this you could try converting your output to an image and send it to the printer as bytes using an emulation the printer understands.(PCL, ESC/POS, ZPL etc.)

AudioRecorder App crashes when I record a second time

My app breaks down when I attempt to record a second a time when I hit the stop button. I went through this code several times. I can't find the problem. I'm not sure if its my MediaRecorder or my MediaPlayer. It works the first time around. But not the second time. can anyone find the problem.
public class PatientName extends Activity implements OnClickListener {
private Button instructionsBtn;
private ImageView record;
private ImageView stop;
private MediaPlayer instructions;
private MediaPlayer namePlayer;
private MediaRecorder nameRecorder;
private String OUTPUT_FILE;
private Button play;
private Button accept;
private LinearLayout review;
private Button delete;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.patient_name);
OUTPUT_FILE = Environment.getExternalStorageDirectory() + "/namerecording.3gpp";
initializeViews();
initializeListeners();
}
public void initializeViews() {
instructionsBtn = (Button)findViewById(R.id.patient_name_instructions);
record = (ImageView)findViewById(R.id.record_name);
stop = (ImageView)findViewById(R.id.stop_name);
stop.setVisibility(View.GONE);
play = (Button)findViewById(R.id.play_name);
play.setVisibility(View.GONE);
review = (LinearLayout)findViewById(R.id.review_name);
delete = (Button)findViewById(R.id.delete_name);
accept = (Button)findViewById(R.id.accept_name);
review.setVisibility(View.GONE);
}
public void initializeListeners() {
instructionsBtn.setOnClickListener(this);
record.setOnClickListener(this);
stop.setOnClickListener(this);
play.setOnClickListener(this);
delete.setOnClickListener(this);
accept.setOnClickListener(this);
}
#Override
public void onBackPressed() {
}
public void playInstructions() {
setMaxVolume();
instructions = MediaPlayer.create(PatientName.this, R.raw.intro_instructions);
instructions.start();
instructions.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer play) {
instructions.release();
instructionsBtn.setEnabled(true);
}
});
}
public void setMaxVolume() {
AudioManager audio = (AudioManager) getSystemService(this.AUDIO_SERVICE);
int maxVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
audio.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0);
}
public void onClick(View v) {
int id = v.getId();
if (id==R.id.patient_name_instructions) {
instructionsBtn.setEnabled(false);
playInstructions();
}
if (id==R.id.record_name) {
beginRecording();
}
if (id==R.id.stop_name){
play.setVisibility(View.VISIBLE);
nameRecorder.stop();
}
if (id==R.id.play_name) {
playbackRecording();
}
if (id==R.id.delete_name){
deleteRecording();
}
if (id==R.id.accept_name) {
saveAndContinue();
}
}
private void beginRecording(){
record.setVisibility(View.GONE);
stop.setVisibility(View.VISIBLE);
File outFile = new File(OUTPUT_FILE);
if (outFile.exists()) {
outFile.delete();
}else {
nameRecorder = new MediaRecorder();
nameRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
nameRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
nameRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
nameRecorder.setOutputFile(OUTPUT_FILE);
try {
nameRecorder.prepare();
} catch (IllegalStateException e) {
Toast toast = Toast.makeText(this,"Illegal State",Toast.LENGTH_LONG);
toast.show();
e.printStackTrace();
} catch (IOException e) {
Toast toast = Toast.makeText(this,"Error Recording",Toast.LENGTH_LONG);
toast.show();
e.printStackTrace();
}
nameRecorder.start();
}
}
private void playbackRecording() {
play.setVisibility(View.GONE);
namePlayer = new MediaPlayer();
try {
namePlayer.setDataSource(OUTPUT_FILE);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
namePlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
namePlayer.start();
namePlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer play) {
namePlayer.release();
nameRecorder.release();
review.setVisibility(View.VISIBLE);
}
});
}
private void deleteRecording() {
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
private void saveAndContinue() {
Intent intent = new Intent(PatientName.this, PatientLocation.class);
startActivity(intent);
}
}
Here is my logCat:
I figured out the issue. In the beginRecording method, I have an if and else statement that handles the output file. Unfortunately, the code that I had in the else statement was something that I wanted to always run no matter what.

Android SoundBoard Crash when Activity resumed

Hi there I have built a soundboard for a character of mine called Aussie Bloke - the soundboard contains 30 buttons and 30 mediaplayers. I originally used one mediaplayer for all buttons but eventually the sounds would stop playing after about 20 random presses. So I decided on using seperate mediaplayer objects for each sound as used in the original soundboard template I used. This worked fine you could press buttons all day and get sounds working properly ..but the problem I have is that when the activity is sent to the background and brought back again even after a few seconds the app crashes when you press a button..I tried creating an onResume method to prepare all the players but this just made the app crash right from the start..sorry I am very new to coding and app building so there may be an obvious solution I have overlooked or been completely unaware of.
package com.zammacat.aussiebloke1;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AUSSIEBLOKE1 extends Activity {
private static final String BASE64_PUBLIC_KEY = "key here";
MediaPlayer beer1;
MediaPlayer beer2;
MediaPlayer beer3;
MediaPlayer beauty;
MediaPlayer boner;
MediaPlayer death;
MediaPlayer dingo;
MediaPlayer dogupya;
MediaPlayer donotpress;
MediaPlayer enough;
MediaPlayer heaven;
MediaPlayer help;
MediaPlayer hungry;
MediaPlayer kebab;
MediaPlayer kids;
MediaPlayer knees;
MediaPlayer later;
MediaPlayer meatpie;
MediaPlayer medal;
MediaPlayer oath;
MediaPlayer poem;
MediaPlayer politics;
MediaPlayer pub;
MediaPlayer sex;
MediaPlayer sport;
MediaPlayer strewth;
MediaPlayer tango;
MediaPlayer v8;
MediaPlayer what;
MediaPlayer yoursister;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aussiebloke1);
beer1 = MediaPlayer.create(this, R.raw.beer1);
beer2 = MediaPlayer.create(this, R.raw.beer2);
beer3 = MediaPlayer.create(this, R.raw.beer3);
beauty = MediaPlayer.create(this, R.raw.beauty);
boner = MediaPlayer.create(this, R.raw.boner);
death = MediaPlayer.create(this, R.raw.death);
dingo = MediaPlayer.create(this, R.raw.dingo);
dogupya = MediaPlayer.create(this, R.raw.dogupya);
donotpress = MediaPlayer.create(this, R.raw.donotpress);
enough = MediaPlayer.create(this, R.raw.enough);
heaven = MediaPlayer.create(this, R.raw.heaven);
help = MediaPlayer.create(this, R.raw.help);
hungry = MediaPlayer.create(this, R.raw.hungry);
kebab = MediaPlayer.create(this, R.raw.kebab);
kids = MediaPlayer.create(this, R.raw.kids);
knees = MediaPlayer.create(this, R.raw.knees);
later = MediaPlayer.create(this, R.raw.later);
meatpie = MediaPlayer.create(this, R.raw.meatpie);
medal = MediaPlayer.create(this, R.raw.medal);
oath = MediaPlayer.create(this, R.raw.oath);
poem = MediaPlayer.create(this, R.raw.poem);
politics = MediaPlayer.create(this, R.raw.politics);
pub = MediaPlayer.create(this, R.raw.pub);
sex = MediaPlayer.create(this, R.raw.sex);
sport = MediaPlayer.create(this, R.raw.sport);
strewth = MediaPlayer.create(this, R.raw.strewth);
tango = MediaPlayer.create(this, R.raw.tango);
v8 = MediaPlayer.create(this, R.raw.v8);
what = MediaPlayer.create(this, R.raw.what);
yoursister = MediaPlayer.create(this, R.raw.yoursister);
Button b01 = (Button) findViewById(R.id.beer1);
b01.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
beer1.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
beer1.start();
}
});
Button b02 = (Button) findViewById(R.id.beer2);
b02.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
beer2.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
beer2.start();
}
});
Button b03 = (Button) findViewById(R.id.beer3);
b03.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
beer3.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
beer3.start();
}
});
Button b04 = (Button) findViewById(R.id.beauty);
b04.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
beauty.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
beauty.start();
}
});
Button b05 = (Button) findViewById(R.id.boner);
b05.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
boner.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
boner.start();
}
});
Button b06 = (Button) findViewById(R.id.death);
b06.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
death.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
death.start();
}
});
Button b07 = (Button) findViewById(R.id.dingo);
b07.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
dingo.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
dingo.start();
}
});
Button b08 = (Button) findViewById(R.id.dogupya);
b08.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
dogupya.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
dogupya.start();
}
});
Button b09 = (Button) findViewById(R.id.donotpress);
b09.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
donotpress.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
donotpress.start();
}
});
Button b10 = (Button) findViewById(R.id.enough);
b10.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
enough.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
enough.start();
}
});
Button b11 = (Button) findViewById(R.id.heaven);
b11.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
heaven.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
heaven.start();
}
});
Button b12 = (Button) findViewById(R.id.help);
b12.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
help.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
help.start();
}
});
Button b13 = (Button) findViewById(R.id.hungry);
b13.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
hungry.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
hungry.start();
}
});
Button b14 = (Button) findViewById(R.id.kebab);
b14.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
kebab.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
kebab.start();
}
});
Button b15 = (Button) findViewById(R.id.kids);
b15.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
kids.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
kids.start();
}
});
Button b16 = (Button) findViewById(R.id.knees);
b16.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
knees.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
knees.start();
}
});
Button b17 = (Button) findViewById(R.id.later);
b17.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
later.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
later.start();
}
});
Button b18 = (Button) findViewById(R.id.meatpie);
b18.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
meatpie.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
meatpie.start();
}
});
Button b19 = (Button) findViewById(R.id.medal);
b19.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
medal.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
medal.start();
}
});
Button b20 = (Button) findViewById(R.id.oath);
b20.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
oath.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
oath.start();
}
});
Button b21 = (Button) findViewById(R.id.poem);
b21.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
poem.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
poem.start();
}
});
Button b22 = (Button) findViewById(R.id.politics);
b22.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
politics.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
politics.start();
}
});
Button b23 = (Button) findViewById(R.id.pub);
b23.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
pub.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
pub.start();
}
});
Button b24 = (Button) findViewById(R.id.sex);
b24.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
sex.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
sex.start();
}
});
Button b25 = (Button) findViewById(R.id.sport);
b25.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
sport.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
sport.start();
}
});
Button b26 = (Button) findViewById(R.id.strewth);
b26.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
strewth.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
strewth.start();
}
});
Button b27 = (Button) findViewById(R.id.tango);
b27.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
tango.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
tango.start();
}
});
Button b28 = (Button) findViewById(R.id.v8);
b28.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
v8.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
v8.start();
}
});
Button b29 = (Button) findViewById(R.id.what);
b29.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
what.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
what.start();
}
});
Button b30 = (Button) findViewById(R.id.yoursister);
b30.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
yoursister.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
yoursister.start();
}
});
}
protected void onStop() {
super.onStop();
beer1.release();
beer2.release();
beer3.release();
beauty.release();
boner.release();
death.release();
dingo.release();
dogupya.release();
donotpress.release();
enough.release();
heaven.release();
help.release();
hungry.release();
kebab.release();
kids.release();
knees.release();
later.release();
meatpie.release();
medal.release();
oath.release();
poem.release();
politics.release();
pub.release();
sex.release();
sport.release();
strewth.release();
tango.release();
v8.release();
what.release();
yoursister.release();
}
}
You need to implement onResume() method in your activity and recreate there your mediaplayer objects:
public void onResume() {
super.onResume();
// ...
beer1 = MediaPlayer.create(this, R.raw.beer1);
beer2 = MediaPlayer.create(this, R.raw.beer2);
beer3 = MediaPlayer.create(this, R.raw.beer3);
// ...
}
You can read more about Activity lifecycle here: http://developer.android.com/training/basics/activity-lifecycle/index.html

How to return to the previous screen when Bluetooth connection is lost?

At first, my android device scans for bluetooth devices and then displays them in a listview. I select one of them and a new screen appears. How to return to the main screen when the connection is lost. Following is the code for selected device screen.
public class devicefound extends Activity implements OnClickListener {
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
Button b1;
private static final UUID MY_UUID =
UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
public static String address;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
findViewById(R.id.b1).setOnClickListener(this);
b1 = (Button) findViewById(R.id.b1);
}
#Override
public void onStart() {
super.onStart();
String address = getIntent().getStringExtra("address");
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
run();
}
public void run(){
try {
btSocket.connect();
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) { }
return;
}
}
public void onClick(View v){
String message1 = "1";
byte[] msgBuffer1 = message1.getBytes();
try{
outStream = btSocket.getOutputStream();
} catch (IOException e){ }
try {
outStream.write(msgBuffer1);
} catch (IOException e) {
}
}
}
#Override
public void onPause() {
super.onPause();
if (outStream != null) {
try {
outStream.flush();
} catch (IOException e) { }
}
}
#Override
public void onStop() {
super.onStop();
}
#Override
public void onDestroy() {
super.onDestroy();
}
}
As I know you should use BroadcastReceiver in a such situation.
Something like this http://android-er.blogspot.com/2011/05/start-bluetooth-discoverable-and.html
If you want to return to the previous screen, then you can call the finish method which your devicefound class inherits from Activity.

Categories

Resources