I want to print the text file from my application in android 8 through Bluetooth or WiFi. Please suggest me the solution.
this is a sample
package com.example.untitled2;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
public class MyActivity extends Activity {
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(MyActivity.this, "no device", Toast.LENGTH_LONG).show();
}
if (!mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.enable();
}
Set<BluetoothDevice> bluetoothDevices = mBluetoothAdapter.getBondedDevices();
if (bluetoothDevices.size() == 0)
return;
OutputStream mmOutStream;
BluetoothDevice device = bluetoothDevices.iterator().next();
try {
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(MY_UUID);
socket.connect();
mmOutStream = socket.getOutputStream();
/*String textPrint = ""+(char) 27 + (char)116 + (char) 27;*/
String textPrint = "this is example text"+(char)10;
mmOutStream.flush();
mmOutStream.write(textPrint.getBytes());
mmOutStream.flush();
for (int i = 0; i < 10; i++) {
mmOutStream.write(textPrint.getBytes());
}
mmOutStream.flush();
//mmOutStream.wait();
mmOutStream.close();
socket.close();
} catch (IOException e) {
Toast.makeText(MyActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
}