Advertisement
sfat

Client Bluetooth

May 19th, 2011
1,458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 KB | None | 0 0
  1. package com.sfatandrei.android.bluetoothclient;
  2.  
  3. import java.io.IOException;
  4. import java.lang.reflect.Method;
  5. import java.util.Set;
  6.  
  7. import android.app.Activity;
  8. import android.bluetooth.BluetoothAdapter;
  9. import android.bluetooth.BluetoothDevice;
  10. import android.bluetooth.BluetoothSocket;
  11. import android.content.Intent;
  12. import android.os.Bundle;
  13. import android.util.Log;
  14. import android.view.View;
  15. import android.view.View.OnClickListener;
  16. import android.widget.Button;
  17. import android.widget.Toast;
  18.  
  19. public class BluetoothClient extends Activity {
  20.  
  21.     private static final String TAG = "MyActivity";
  22.     private static final int REQUEST_ENABLE_BT = 0;
  23.     private BluetoothAdapter mBtAdapter;
  24.     private BluetoothSocket socket;
  25.     private Button button;
  26.  
  27.     /** Called when the activity is first created. */
  28.     @Override
  29.     public void onCreate(Bundle savedInstanceState) {
  30.         super.onCreate(savedInstanceState);
  31.         setContentView(R.layout.main);
  32.         Log.d(TAG, "ON CREATE");
  33.     }
  34.    
  35.  
  36.     @Override
  37.     protected void onStart() {
  38.         super.onStart();
  39.         Log.d(TAG, "ON START");
  40.         button = (Button) findViewById(R.id.button1);
  41.  
  42.         establishConnection();
  43.        
  44.    
  45.     }
  46.    
  47.  
  48.    
  49.     private void establishConnection() {
  50.         try {
  51.             mBtAdapter = BluetoothAdapter.getDefaultAdapter();
  52.             if (mBtAdapter == null) {
  53.                 Toast.makeText(BluetoothClient.this, "Bluetooth not supported", Toast.LENGTH_SHORT).show();
  54.                 Log.d(TAG,"Bluetooth not supported.");
  55.                 finish();
  56.             }
  57.            
  58.         } catch (Exception e) {
  59.             Log.e(TAG, "Error connecting to device", e);
  60.             Toast.makeText(BluetoothClient.this, "Error connecting to destkop application.", Toast.LENGTH_SHORT).show();
  61.         }
  62.        
  63.         try
  64.         {
  65.             if (!mBtAdapter.isEnabled()) {
  66.                 mBtAdapter.enable();
  67.             }
  68.             while(true){
  69.                 Log.d(TAG, "INSIDE WHILE STATE ON");
  70.                 if (mBtAdapter.getState() == BluetoothAdapter.STATE_ON)
  71.                     break;
  72.             }
  73.            
  74.             boolean result = mBtAdapter.startDiscovery();
  75.             Log.d(TAG, "Start discovery = " + result);
  76.            
  77.             while(true){
  78.                 Log.d(TAG, "INSIDE WHILE IS DISCOVERING");
  79.                 if (mBtAdapter.isDiscovering() == true){
  80.                     mBtAdapter.cancelDiscovery();
  81.                     break;
  82.                 }
  83.             }
  84.  
  85.             connectToBondedDevice();
  86.            
  87.             button.setOnClickListener(new OnClickListener() {
  88.                 public void onClick(View v) {
  89.                     Log.d(TAG,"In listener button.");
  90.                     try {
  91.                         if (socket != null)
  92.                             socket.getOutputStream().write("Hello, world!".getBytes());
  93.                         else
  94.                             Toast.makeText(BluetoothClient.this, "Not connected to destkop application.", Toast.LENGTH_SHORT).show();
  95.                     } catch (IOException e) {
  96.                         Log.d(TAG, e.toString());
  97.                         e.printStackTrace();
  98.                     }
  99.                 }  
  100.             });
  101.         }
  102.         catch(Exception ex)
  103.         {
  104.             Log.d(TAG,ex.toString());
  105.         }
  106.     }
  107.  
  108.     private void connectToBondedDevice(){
  109.         Set<BluetoothDevice> setOfDevices = mBtAdapter.getBondedDevices();
  110.         if (setOfDevices.isEmpty())
  111.             Log.d(TAG, "NULL BLUETOOTH SET");
  112.         for (BluetoothDevice bt : setOfDevices){
  113.             if (bt.getAddress().equals("50:63:13:A1:21:9E")){
  114.                 try {
  115.                     Method m;
  116.                     try {
  117.                         m = bt.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
  118.                         socket = (BluetoothSocket) m.invoke(bt, 1);
  119.                     } catch (Exception e) {
  120.                         Log.d(TAG, e.toString());
  121.                     }
  122.                     Log.d(TAG, "about to connect");
  123.                     mBtAdapter.cancelDiscovery();
  124.                     socket.connect();
  125.                     Log.d(TAG, "Connected!");
  126.                 } catch (IOException e) {
  127.                     Log.d(TAG, e.toString() + " " + e.getStackTrace().toString());
  128.                 }
  129.             }
  130.         }
  131.     }
  132.  
  133.     @Override
  134.     protected void onPause() {
  135.         super.onPause();
  136.         Log.d(TAG, "ON PAUSE");
  137.         try {
  138.             socket.close();
  139.         } catch (IOException e) {
  140.             e.printStackTrace();
  141.         }
  142.     }
  143.  
  144.     @Override
  145.     protected void onDestroy() {
  146.         super.onDestroy();
  147.         Log.d(TAG, "ON DESTROY");
  148.         try {
  149.             socket.close();
  150.         } catch (IOException e) {
  151.             e.printStackTrace();
  152.         }
  153.     }
  154.  
  155.    
  156.    
  157.    
  158.     @Override
  159.     protected void onResume() {
  160.         super.onResume();
  161.         Log.d(TAG, "ON RESUME");
  162.     }
  163.    
  164.     @Override
  165.     public void onBackPressed() {
  166.         super.onBackPressed();
  167.         try {
  168.             socket.close();
  169.         } catch (IOException e) {
  170.             e.printStackTrace();
  171.         }
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement