Advertisement
snakerdlk

BluetoothController.Main.java

Aug 8th, 2011
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.28 KB | None | 0 0
  1. package com.BluetoothController;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.util.UUID;
  7.  
  8. import android.app.Activity;
  9. import android.bluetooth.BluetoothAdapter;
  10. import android.bluetooth.BluetoothDevice;
  11. import android.bluetooth.BluetoothSocket;
  12. import android.content.Intent;
  13. import android.os.Bundle;
  14. import android.util.Log;
  15. import android.view.View;
  16. import android.view.View.OnClickListener;
  17. import android.widget.Button;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20.  
  21. public class Main extends Activity {
  22.     //string that defines the title of the message in logcat
  23.     private final String TAG = "Main";
  24.  
  25.     //objects for the graphical elements
  26.     private Button mButton = null; //button to toggle led
  27.     private TextView textField = null; //text field to show status
  28.  
  29.     //bluetooth related objects
  30.     private BluetoothAdapter mBluetoothAdapter = null;
  31.     private BluetoothSocket mBTSock = null;
  32.  
  33.     //constants for DeviceListActivity usage
  34.     private static final int REQUEST_CONNECT_DEVICE = 1;
  35.     private static final int REQUEST_ENABLE_BT = 2;
  36.    
  37.     @Override
  38.     public void onCreate(Bundle savedInstanceState) {
  39.         super.onCreate(savedInstanceState);
  40.         setContentView(R.layout.main);
  41.        
  42.         //bind the objects to their view elements
  43.         mButton = (Button) findViewById(R.id.mButton);
  44.         textField = (TextView) findViewById(R.id.textField);
  45.        
  46.         textField.setText("init");
  47.  
  48.         //define that if the button is clicked the led should be toggled
  49.         mButton.setOnClickListener(new OnClickListener(){
  50.             @Override
  51.             public void onClick(View arg0) {
  52.                 toggleLed();
  53.             }
  54.         });
  55.        
  56.         //Get the default bluetooth adapter
  57.         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();        
  58.        
  59.         //If the adapter is null, then Bluetooth is not supported
  60.         if (mBluetoothAdapter == null) {
  61.             Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
  62.             finish();
  63.             return;
  64.         }
  65.         Log.d(TAG, "got bluetooth adapter");
  66.        
  67.         //Check if the adapter is enabled
  68.         if (!mBluetoothAdapter.isEnabled()) {
  69.             Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  70.             startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
  71.         }else{
  72.             setupBT();
  73.         }
  74.         Log.d(TAG,"onCreate:done");
  75.     }
  76.    
  77.     @Override
  78.     public void onDestroy(){
  79.         //if the app is closed, should try to close the socket
  80.         try {
  81.             mBTSock.close();
  82.             Log.d(TAG,"socket closed");
  83.         } catch (IOException e) {
  84.             Log.e(TAG,"error closing socket");
  85.         }
  86.         super.onDestroy();
  87.     }
  88.    
  89.     //here the status returned from the DeviceListActivity or Intent to activate bluetooth is treated
  90.     public void onActivityResult(int requestCode, int resultCode, Intent data) {
  91.         Log.d(TAG, "onActivityResult():" + resultCode);
  92.         switch (requestCode) {
  93.             case REQUEST_CONNECT_DEVICE:
  94.                 // When DeviceListActivity returns with a device to connect
  95.                 if (resultCode == Activity.RESULT_OK) {
  96.                     // Get the device MAC address
  97.                     String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
  98.  
  99.                     // Get the BLuetoothDevice object
  100.                     BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
  101.  
  102.                     //this UUID is the default id for devices/modules
  103.                     UUID mUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  104.                     try {
  105.                         //create socket    
  106.                         mBTSock = device.createRfcommSocketToServiceRecord(mUUID);
  107.  
  108.                         textField.setText("connecting...");
  109.  
  110.                         //connect to device
  111.                         mBTSock.connect();
  112.  
  113.                         Log.d(TAG,"socket connected");
  114.                         textField.setText("connected.");
  115.  
  116.                     } catch (IOException e) {
  117.                         Log.d(TAG,"ioexception creating socket");
  118.                         textField.setText("IOException!");
  119.                     }
  120.                 }
  121.                 break;
  122.             case REQUEST_ENABLE_BT:
  123.                 // When the request to enable Bluetooth returns
  124.                 if (resultCode == Activity.RESULT_OK) {
  125.                     // Bluetooth is now enabled, so set up a chat session
  126.                     setupBT();
  127.  
  128.                 } else {
  129.                     // User did not enable Bluetooth or an error occured
  130.                     Log.d(TAG, "BT is not enabled");
  131.                     Toast.makeText(this, "Bluetooth is not enabled!", Toast.LENGTH_SHORT).show();
  132.                     finish();
  133.                 }
  134.                 break;
  135.         }
  136.     }
  137.  
  138.     void setupBT(){
  139.         Log.d(TAG,"setupBT");
  140.         Intent deviceList = new Intent(this, DeviceListActivity.class);
  141.         startActivityForResult(deviceList, REQUEST_CONNECT_DEVICE);
  142.     }
  143.  
  144.     void toggleLed(){
  145.         try {  
  146.             //create streams for sending and receiving data    
  147.                 OutputStream outStream = mBTSock.getOutputStream();
  148.                 InputStream inStream = mBTSock.getInputStream();
  149.                 Log.d(TAG,"streams created");
  150.                
  151.             //send message via bluetooth
  152.                 textField.setText("sending message...");
  153.                 String msg = "led";
  154.                 outStream.write(msg.getBytes());
  155.                 Log.d(TAG,"message sent");
  156.                
  157.             //wait for answer
  158.             //TODO:timeout?
  159.                 textField.setText("waiting for some data...");
  160.                 while(inStream.available() == 0);
  161.  
  162.                 Log.d(TAG,"data available");
  163.                
  164.                 byte[] bytez = new byte[50];
  165.                 if(inStream.read(bytez) != 0){
  166.                     textField.setText("recv:"+new String(bytez));
  167.                 }else{
  168.                     textField.setText("no data...");
  169.                 }
  170.                
  171.         } catch (IOException e) {
  172.             textField.setText("IOException while connecting to client");
  173.             Log.d(TAG,"IOException...");
  174.         }
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement