Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- import android.bluetooth.BluetoothSocket;
- import android.content.Intent;
- import android.os.Handler;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Toast;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Set;
- import java.util.UUID;
- public class BluetoothUstawienia extends ActionBarActivity {
- private BluetoothAdapter mBluetoothAdapter; // BT
- private BluetoothSocket mmSocket; // BT
- BluetoothDevice mmDevice; // BT
- OutputStream mmOutputStream; // BT
- InputStream mmInputStream; // BT
- private static final UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //uuid dla wymiany danych
- Thread workerThread;
- byte[] readBuffer;
- int readBufferPosition;
- volatile boolean stopWorker;
- private static final String TAG = "BTustawienia";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_bluetooth_ustawienia);
- }
- public void findBT(View view){ // View view bo bierze wartość z activity_main.xml
- mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- if(mBluetoothAdapter==null){ // BT info
- Toast.makeText(getApplicationContext(), "Nie wykryto BT", Toast.LENGTH_LONG).show();
- }
- if(!mBluetoothAdapter.isEnabled()){ // Pyta czy bluetooth jest włączony
- Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(enableBluetooth,0);
- }
- Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
- if(pairedDevices.size() > 0)
- {
- for(BluetoothDevice device : pairedDevices)
- {
- if(device.getName().equals("PATRYK-KOMPUTER")) // WPISZ TU SWOJA NAZWE URZADZENIA
- {
- mmDevice = device;
- break;
- }
- }
- }
- Toast.makeText(getApplicationContext(),"Znaleziono BT", Toast.LENGTH_LONG).show();
- }
- public void openBT(View view) throws IOException
- {
- if (mmDevice != null) {
- try {
- Toast.makeText(getApplicationContext(),"Próba łączenia", Toast.LENGTH_SHORT).show();
- mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
- mmSocket.connect();
- mmOutputStream = mmSocket.getOutputStream();
- mmInputStream = mmSocket.getInputStream();
- beginListenForData();
- Toast.makeText(getApplicationContext(),"Połączono!", Toast.LENGTH_LONG).show();
- } catch (Exception e2) {
- Toast.makeText(getApplicationContext(),"Nie połączono :(", Toast.LENGTH_LONG).show();
- }
- mmOutputStream = mmSocket.getOutputStream();
- mmInputStream = mmSocket.getInputStream();
- //beginListenForData(); // Do nasluchiwania bluetooth
- //sendData(); // Do odbierania
- }
- else if (mmDevice == null){
- Toast.makeText(getApplicationContext(), "Nie ma sparowanych urządzeń", Toast.LENGTH_LONG).show();
- }
- }
- void beginListenForData()
- {
- final Handler handler = new Handler();
- final byte delimiter = 10; //This is the ASCII code for a newline character
- 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);
- final String data = new String(encodedBytes, "US-ASCII");
- readBufferPosition = 0;
- handler.post(new Runnable()
- {
- public void run()
- {
- Log.v(TAG, "data = " + data);
- }
- });
- }
- else
- {
- readBuffer[readBufferPosition++] = b;
- }
- }
- }
- }
- catch (IOException ex)
- {
- stopWorker = true;
- }
- }
- }
- });
- workerThread.start();
- }
- public void sendData() throws IOException
- {
- String msg = "wiadomosc";
- msg += "\r";
- mmOutputStream.write(msg.getBytes());
- Toast.makeText(getApplicationContext(),"Dane zostały przesłane", Toast.LENGTH_SHORT).show();
- Log.v(TAG, "wysylanie wiadomosci");
- //TextView textViewBT = (TextView) findViewById(R.id.BTzobacz);
- //textViewBT.setText();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_bluetooth_ustawienia, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment