Reputation: 5
I want the data displayed on the device to be displayed on my android application using Bluetooth
.
I have gone through this. But I am not getting how to receive the data and display it in my app. Could anyone help?
Upvotes: 0
Views: 303
Reputation: 248
First you have to find the device using OnCreate method // open bluetooth connection
openButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pDialog = new ProgressDialog(Activity.this);
new AsyncTask<Void, Void, Void>()
{
@Override
protected Void doInBackground(Void... params)
{
try {
findBT();
openBT();
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
}
}.execute();
}
});
Then you can pass data by using following method
public void findBT() {
try {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null) {
Toast.makeText(getApplicationContext(),"Bluetooth Not Found...!",Toast.LENGTH_LONG).show();
}
if(!mBluetoothAdapter.isEnabled()) {
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
Log.d("Devices","============>"+device.getName());
// RPP300 is the name of the bluetooth device
// we got this name from the list of paired devices
//if (device.getName()=="NP100S28C9") {
mmDevice = device;
break;
}
}
}catch(Exception e){
e.printStackTrace();
}
}
// tries to open a connection to the bluetooth device
public void openBT() throws IOException {
try {
// Standard SerialPortService ID
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
if(mmDevice != null) {
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
beginListenForData();
btnPrint.setEnabled(true);
}
else{
Toast.makeText(getApplicationContext(),"Paired Bluthooth not Found..!",Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
pDialog.dismiss();
e.printStackTrace();
}
}
public void beginListenForData() {
try {
final Handler handler = new Handler();
// this is the ASCII code for a newline character
final byte delimiter = 10;
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable() {
public void run() {
while (!Thread.currentThread().isInterrupted() && !stopWorker) {
try {
int bytesAvailable = mmInputStream.available();
if (bytesAvailable > 0) {
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for (int i = 0; i < bytesAvailable; i++) {
byte b = packetBytes[i];
if (b == delimiter) {
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(
readBuffer, 0,
encodedBytes, 0,
encodedBytes.length
);
// specify US-ASCII encoding
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
// tell the user data were sent to bluetooth printer device
handler.post(new Runnable() {
public void run() {
myLabel.setText(data);
}
});
} else {
readBuffer[readBufferPosition++] = b;
}
}
}
} catch (IOException ex) {
stopWorker = true;
}
}
}
});
workerThread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
//Sending data to blutooth
public void sendData() throws IOException {
try {
// the text typed by the user
String data = "your data";
String msg = data;
msg += "\n";
mmOutputStream.write(msg.getBytes());
// tell the user data were sent
Toast.makeText(getApplicationContext(),"Data send Successfully...!",Toast.LENGTH_SHORT).show();
closeButton.setEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 3