There is a grey label which is fixed to battery pack, on it. If you push it up, there are screws.They need soldering for serial connection.
10 Nisan 2019 Çarşamba
5 Nisan 2019 Cuma
Android Bluetooth Connection To HC-05
Sending Data to HC-05
package com.example.a6dofbt;
import android.app.Activity;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
public class dof extends Activity {
private OutputStream outStream = null;
private BluetoothDevice device;
private TextView title;
private SeekBar clamp ;
private SeekBar base;
private SeekBar leg;
private SeekBar waist;
private SeekBar head;
private SeekBar chest;
private EditText motorno;
private Button add;
private Button subtract;
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private static String address = "00:00:00:00:00:00";
private BluetoothAdapter btAdapter = null;
private BluetoothSocket btSocket = null;
private static final int REQUEST_ENABLE_BT = 1;
private boolean isBtConnected = false;
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dof);
MotorBar clamp = new MotorBar(0,200, "Clamp");
MotorBar head = new MotorBar(1,200, "Head");
MotorBar chest = new MotorBar(2,200, "Chest");
MotorBar waist = new MotorBar(3,200, "Waist");
MotorBar leg = new MotorBar(4,200, "Leg");
MotorBar base = new MotorBar(5,200, "Base");
clamp.bar = (SeekBar) findViewById(R.id.seekBarOne);
head.bar = (SeekBar) findViewById(R.id.seekBarTwo);
chest.bar = (SeekBar) findViewById(R.id.seekBarThree);
waist.bar = (SeekBar) findViewById(R.id.seekBarFour);
leg.bar = (SeekBar) findViewById(R.id.seekBarFive);
base.bar = (SeekBar) findViewById(R.id.seekBarSix);
clamp.text = (TextView) findViewById(R.id.textOne);
head.text = (TextView) findViewById(R.id.textTwo);
chest.text = (TextView) findViewById(R.id.textThree);
waist.text = (TextView) findViewById(R.id.textFour);
leg.text = (TextView) findViewById(R.id.textFive);
base.text = (TextView) findViewById(R.id.textSix);
title = (TextView) findViewById(R.id.title);
try {
Bundle b = this.getIntent().getExtras();
address = (String) b.get("address");
title.setText(address);
/*
btAdapter = BluetoothAdapter.getDefaultAdapter();
device = btAdapter.getRemoteDevice(address);
btSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
btSocket.connect();
outStream = btSocket.getOutputStream();
outStream.write("w".getBytes());
*/
new BTbaglan().execute();
} catch(Exception e) {
errorExit("Error", e.getLocalizedMessage());
}
//checkBTState();
add = (Button) findViewById(R.id.add);
subtract = (Button) findViewById(R.id.subtract);
motorno = (EditText) findViewById(R.id.motorno);
motorno.setText("0");
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String mno = String.valueOf(motorno.getText());
outStream.write(mno.getBytes());
outStream.flush();
Thread.sleep(30);
outStream.write("w".getBytes());
outStream.flush();
} catch(Exception e) {
errorExit("Final Error", "no write on stream "+e.getLocalizedMessage());
}
}
});
subtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String mno = String.valueOf(motorno.getText());
outStream.write(mno.getBytes());
outStream.flush();
Thread.sleep(30);
outStream.write("e".getBytes());
outStream.flush();
} catch(Exception e) {
errorExit("Final Error", "no write on stream for button"+e.getLocalizedMessage());
}
}
});
int maxV = 500;
MotorBar[] motors = {clamp, head, chest, waist, leg, base};
for(int i=0; i < motors.length; i++) {
final MotorBar m = motors[i];
m.bar.setMax(maxV);
m.bar.setProgress(motors[i].value);
m.bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int progressValue = 0;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progressValue = progress;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) { }
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
seekBar.setProgress(progressValue);
m.value = progressValue + 50;
m.text.setText(m.name +": "+String.valueOf(m.value));
try {
String mno = String.valueOf(m.no);
outStream.write(mno.getBytes());
outStream.flush();
Thread.sleep(500);
String mv = String.valueOf(m.value);
outStream.write(mv.getBytes());
outStream.flush();
} catch(Exception e) {
errorExit("Fatal Error", "no write on stream for bar "+e.getLocalizedMessage());
}
}
});
}
}
private void errorExit(String title, String message){
Toast msg = Toast.makeText(getBaseContext(),
title + " - " + message, Toast.LENGTH_SHORT);
msg.show();
finish();
}
/*
@Override
public void onPause() {
super.onPause();
if (outStream != null) {
try {
outStream.flush();
} catch (IOException e) {
errorExit("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + ".");
}
}
try {
btSocket.close();
} catch (IOException e2) {
errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
}
}
private void checkBTState() {
if(btAdapter==null) {
errorExit("Fatal Error", "Bluetooth Not supported. Aborting.");
} else {
if (!btAdapter.isEnabled()) {
//Prompt user to turn on Bluetooth
Intent enableBtIntent = new Intent(btAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
Disconnect();
}
private void Disconnect() {
if(btSocket != null) {
try{
btSocket.close();
} catch (Exception e) {
}
}
finish();
}
@Override
public void onResume() {
super.onResume();
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
}
btAdapter.cancelDiscovery();
try {
btSocket.connect();
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
}
}
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
}
}
private void setMotor(MotorBar motor) {
String no = String.valueOf(motor.no);
String v = String.valueOf(motor.value);
sendData(no);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
sendData(v);
}
private void sendData(String message) {
byte[] msgBuffer = message.getBytes();
try {
if(!btSocket.isConnected()) {
btSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
btSocket.connect();
}
outStream.write(msgBuffer);
} catch (IOException e) {
String msg = "Sending data failed" + e.getMessage();
errorExit("Fatal error", msg);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
*/
public class BTbaglan extends AsyncTask<Void, Void, Void> {
private boolean ConnectSuccess = true;
@Override
protected void onPreExecute() {
super.onPreExecute();
try {
dialog = ProgressDialog.show(dof.this, "Baglanıyor...", "Lütfen Bekleyin");
} catch(Exception e) {
errorExit2("Fatal error", e.getLocalizedMessage());
}
}
@Override
protected Void doInBackground(Void... devices) {
try {
if (btSocket == null || !isBtConnected) {
btAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = btAdapter.getRemoteDevice(address);
btSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
btSocket.connect();
outStream = btSocket.getOutputStream();
}
} catch (IOException e) {
ConnectSuccess = false;
} catch(Exception e) {
errorExit2("Fatal error", e.getLocalizedMessage());
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (!ConnectSuccess) {
// msg("Baglantı Hatası, Lütfen Tekrar Deneyin");
Toast.makeText(getApplicationContext(), "Bağlantı Hatası Tekrar Deneyin", Toast.LENGTH_SHORT).show();
finish();
} else {
// msg("Baglantı Basarılı");
Toast.makeText(getApplicationContext(), "Bağlantı Başarılı", Toast.LENGTH_SHORT).show();
isBtConnected = true;
}
dialog.dismiss();
}
private void errorExit2(String title, String message){
Toast msg = Toast.makeText(getBaseContext(),
title + " - " + message, Toast.LENGTH_SHORT);
msg.show();
}
}
}
Kaydol:
Kayıtlar (Atom)