Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.BluetoothController;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.UUID;
- import android.app.Activity;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- import android.bluetooth.BluetoothSocket;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- public class Main extends Activity {
- //string that defines the title of the message in logcat
- private final String TAG = "Main";
- //objects for the graphical elements
- private Button mButton = null; //button to toggle led
- private TextView textField = null; //text field to show status
- //bluetooth related objects
- private BluetoothAdapter mBluetoothAdapter = null;
- private BluetoothSocket mBTSock = null;
- //constants for DeviceListActivity usage
- private static final int REQUEST_CONNECT_DEVICE = 1;
- private static final int REQUEST_ENABLE_BT = 2;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //bind the objects to their view elements
- mButton = (Button) findViewById(R.id.mButton);
- textField = (TextView) findViewById(R.id.textField);
- textField.setText("init");
- //define that if the button is clicked the led should be toggled
- mButton.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View arg0) {
- toggleLed();
- }
- });
- //Get the default bluetooth adapter
- mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- //If the adapter is null, then Bluetooth is not supported
- if (mBluetoothAdapter == null) {
- Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
- finish();
- return;
- }
- Log.d(TAG, "got bluetooth adapter");
- //Check if the adapter is enabled
- if (!mBluetoothAdapter.isEnabled()) {
- Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
- }else{
- setupBT();
- }
- Log.d(TAG,"onCreate:done");
- }
- @Override
- public void onDestroy(){
- //if the app is closed, should try to close the socket
- try {
- mBTSock.close();
- Log.d(TAG,"socket closed");
- } catch (IOException e) {
- Log.e(TAG,"error closing socket");
- }
- super.onDestroy();
- }
- //here the status returned from the DeviceListActivity or Intent to activate bluetooth is treated
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- Log.d(TAG, "onActivityResult():" + resultCode);
- switch (requestCode) {
- case REQUEST_CONNECT_DEVICE:
- // When DeviceListActivity returns with a device to connect
- if (resultCode == Activity.RESULT_OK) {
- // Get the device MAC address
- String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
- // Get the BLuetoothDevice object
- BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
- //this UUID is the default id for devices/modules
- UUID mUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
- try {
- //create socket
- mBTSock = device.createRfcommSocketToServiceRecord(mUUID);
- textField.setText("connecting...");
- //connect to device
- mBTSock.connect();
- Log.d(TAG,"socket connected");
- textField.setText("connected.");
- } catch (IOException e) {
- Log.d(TAG,"ioexception creating socket");
- textField.setText("IOException!");
- }
- }
- break;
- case REQUEST_ENABLE_BT:
- // When the request to enable Bluetooth returns
- if (resultCode == Activity.RESULT_OK) {
- // Bluetooth is now enabled, so set up a chat session
- setupBT();
- } else {
- // User did not enable Bluetooth or an error occured
- Log.d(TAG, "BT is not enabled");
- Toast.makeText(this, "Bluetooth is not enabled!", Toast.LENGTH_SHORT).show();
- finish();
- }
- break;
- }
- }
- void setupBT(){
- Log.d(TAG,"setupBT");
- Intent deviceList = new Intent(this, DeviceListActivity.class);
- startActivityForResult(deviceList, REQUEST_CONNECT_DEVICE);
- }
- void toggleLed(){
- try {
- //create streams for sending and receiving data
- OutputStream outStream = mBTSock.getOutputStream();
- InputStream inStream = mBTSock.getInputStream();
- Log.d(TAG,"streams created");
- //send message via bluetooth
- textField.setText("sending message...");
- String msg = "led";
- outStream.write(msg.getBytes());
- Log.d(TAG,"message sent");
- //wait for answer
- //TODO:timeout?
- textField.setText("waiting for some data...");
- while(inStream.available() == 0);
- Log.d(TAG,"data available");
- byte[] bytez = new byte[50];
- if(inStream.read(bytez) != 0){
- textField.setText("recv:"+new String(bytez));
- }else{
- textField.setText("no data...");
- }
- } catch (IOException e) {
- textField.setText("IOException while connecting to client");
- Log.d(TAG,"IOException...");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement