Guest User

Untitled

a guest
May 1st, 2015
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.09 KB | None | 0 0
  1.  
  2. import android.bluetooth.BluetoothAdapter;
  3. import android.bluetooth.BluetoothDevice;
  4. import android.bluetooth.BluetoothSocket;
  5. import android.content.Intent;
  6. import android.os.Handler;
  7. import android.support.v7.app.ActionBarActivity;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.view.View;
  13. import android.widget.Toast;
  14.  
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. import java.io.OutputStream;
  18. import java.util.Set;
  19. import java.util.UUID;
  20.  
  21.  
  22. public class BluetoothUstawienia extends ActionBarActivity {
  23.  
  24.     private BluetoothAdapter mBluetoothAdapter;        // BT
  25.     private BluetoothSocket mmSocket;                  // BT
  26.     BluetoothDevice mmDevice;                  // BT
  27.     OutputStream mmOutputStream;               // BT
  28.     InputStream mmInputStream;                 // BT
  29.     private static final UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //uuid dla wymiany danych
  30.  
  31.     Thread workerThread;
  32.     byte[] readBuffer;
  33.     int readBufferPosition;
  34.     volatile boolean stopWorker;
  35.  
  36.     private static final String TAG = "BTustawienia";
  37.  
  38.  
  39.     @Override
  40.     protected void onCreate(Bundle savedInstanceState) {
  41.         super.onCreate(savedInstanceState);
  42.         setContentView(R.layout.activity_bluetooth_ustawienia);
  43.     }
  44.  
  45.     public void findBT(View view){          // View view bo bierze wartość z activity_main.xml
  46.  
  47.         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  48.  
  49.         if(mBluetoothAdapter==null){                            // BT info
  50.             Toast.makeText(getApplicationContext(), "Nie wykryto BT", Toast.LENGTH_LONG).show();
  51.         }
  52.  
  53.         if(!mBluetoothAdapter.isEnabled()){                         // Pyta czy bluetooth jest włączony
  54.             Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  55.             startActivityForResult(enableBluetooth,0);
  56.         }
  57.  
  58.         Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
  59.         if(pairedDevices.size() > 0)
  60.         {
  61.             for(BluetoothDevice device : pairedDevices)
  62.             {
  63.                 if(device.getName().equals("PATRYK-KOMPUTER"))          // WPISZ TU SWOJA NAZWE URZADZENIA
  64.                 {
  65.                     mmDevice = device;
  66.                     break;
  67.                 }
  68.             }
  69.         }
  70.  
  71.         Toast.makeText(getApplicationContext(),"Znaleziono BT", Toast.LENGTH_LONG).show();
  72.  
  73.     }
  74.  
  75.     public void openBT(View view) throws IOException
  76.     {
  77.         if (mmDevice != null) {
  78.  
  79.             try {
  80.                 Toast.makeText(getApplicationContext(),"Próba łączenia", Toast.LENGTH_SHORT).show();
  81.                 mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
  82.                 mmSocket.connect();
  83.  
  84.                 mmOutputStream = mmSocket.getOutputStream();
  85.                 mmInputStream = mmSocket.getInputStream();
  86.                 beginListenForData();
  87.  
  88.                 Toast.makeText(getApplicationContext(),"Połączono!", Toast.LENGTH_LONG).show();
  89.             } catch (Exception e2) {
  90.                 Toast.makeText(getApplicationContext(),"Nie połączono :(", Toast.LENGTH_LONG).show();
  91.             }
  92.  
  93.             mmOutputStream = mmSocket.getOutputStream();
  94.             mmInputStream = mmSocket.getInputStream();
  95.  
  96.             //beginListenForData();         // Do nasluchiwania bluetooth
  97.             //sendData();                     // Do odbierania
  98.         }
  99.         else if (mmDevice == null){
  100.             Toast.makeText(getApplicationContext(), "Nie ma sparowanych urządzeń", Toast.LENGTH_LONG).show();
  101.         }
  102.  
  103.  
  104.     }
  105.  
  106.     void beginListenForData()
  107.     {
  108.         final Handler handler = new Handler();
  109.         final byte delimiter = 10; //This is the ASCII code for a newline character
  110.  
  111.         stopWorker = false;
  112.         readBufferPosition = 0;
  113.         readBuffer = new byte[1024];
  114.         workerThread = new Thread(new Runnable()
  115.         {
  116.             public void run()
  117.             {
  118.                 while(!Thread.currentThread().isInterrupted() && !stopWorker)
  119.                 {
  120.                     try
  121.                     {
  122.                         int bytesAvailable = mmInputStream.available();
  123.                         if(bytesAvailable > 0)
  124.                         {
  125.                             byte[] packetBytes = new byte[bytesAvailable];
  126.                             mmInputStream.read(packetBytes);
  127.                             for(int i=0;i<bytesAvailable;i++)
  128.                             {
  129.                                 byte b = packetBytes[i];
  130.                                 if(b == delimiter)
  131.                                 {
  132.                                     byte[] encodedBytes = new byte[readBufferPosition];
  133.                                     System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
  134.                                     final String data = new String(encodedBytes, "US-ASCII");
  135.                                     readBufferPosition = 0;
  136.  
  137.                                     handler.post(new Runnable()
  138.                                     {
  139.                                         public void run()
  140.                                         {
  141.                                             Log.v(TAG, "data = " + data);
  142.                                         }
  143.                                     });
  144.                                 }
  145.                                 else
  146.                                 {
  147.                                     readBuffer[readBufferPosition++] = b;
  148.                                 }
  149.                             }
  150.                         }
  151.                     }
  152.                     catch (IOException ex)
  153.                     {
  154.                         stopWorker = true;
  155.                     }
  156.                 }
  157.             }
  158.         });
  159.  
  160.         workerThread.start();
  161.     }
  162.  
  163.     public void sendData() throws IOException
  164.     {
  165.  
  166.         String msg = "wiadomosc";
  167.  
  168.         msg += "\r";
  169.         mmOutputStream.write(msg.getBytes());
  170.         Toast.makeText(getApplicationContext(),"Dane zostały przesłane", Toast.LENGTH_SHORT).show();
  171.  
  172.         Log.v(TAG, "wysylanie wiadomosci");
  173.  
  174.         //TextView textViewBT = (TextView) findViewById(R.id.BTzobacz);
  175.         //textViewBT.setText();
  176.     }
  177.  
  178.  
  179.  
  180.  
  181.     @Override
  182.     public boolean onCreateOptionsMenu(Menu menu) {
  183.         // Inflate the menu; this adds items to the action bar if it is present.
  184.         getMenuInflater().inflate(R.menu.menu_bluetooth_ustawienia, menu);
  185.         return true;
  186.     }
  187.  
  188.     @Override
  189.     public boolean onOptionsItemSelected(MenuItem item) {
  190.         // Handle action bar item clicks here. The action bar will
  191.         // automatically handle clicks on the Home/Up button, so long
  192.         // as you specify a parent activity in AndroidManifest.xml.
  193.         int id = item.getItemId();
  194.  
  195.         //noinspection SimplifiableIfStatement
  196.         if (id == R.id.action_settings) {
  197.             return true;
  198.         }
  199.  
  200.         return super.onOptionsItemSelected(item);
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment