Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sfatandrei.android.bluetoothclient;
- import java.io.IOException;
- import java.lang.reflect.Method;
- import java.util.Set;
- 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.Toast;
- public class BluetoothClient extends Activity {
- private static final String TAG = "MyActivity";
- private static final int REQUEST_ENABLE_BT = 0;
- private BluetoothAdapter mBtAdapter;
- private BluetoothSocket socket;
- private Button button;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Log.d(TAG, "ON CREATE");
- }
- @Override
- protected void onStart() {
- super.onStart();
- Log.d(TAG, "ON START");
- button = (Button) findViewById(R.id.button1);
- establishConnection();
- }
- private void establishConnection() {
- try {
- mBtAdapter = BluetoothAdapter.getDefaultAdapter();
- if (mBtAdapter == null) {
- Toast.makeText(BluetoothClient.this, "Bluetooth not supported", Toast.LENGTH_SHORT).show();
- Log.d(TAG,"Bluetooth not supported.");
- finish();
- }
- } catch (Exception e) {
- Log.e(TAG, "Error connecting to device", e);
- Toast.makeText(BluetoothClient.this, "Error connecting to destkop application.", Toast.LENGTH_SHORT).show();
- }
- try
- {
- if (!mBtAdapter.isEnabled()) {
- mBtAdapter.enable();
- }
- while(true){
- Log.d(TAG, "INSIDE WHILE STATE ON");
- if (mBtAdapter.getState() == BluetoothAdapter.STATE_ON)
- break;
- }
- boolean result = mBtAdapter.startDiscovery();
- Log.d(TAG, "Start discovery = " + result);
- while(true){
- Log.d(TAG, "INSIDE WHILE IS DISCOVERING");
- if (mBtAdapter.isDiscovering() == true){
- mBtAdapter.cancelDiscovery();
- break;
- }
- }
- connectToBondedDevice();
- button.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Log.d(TAG,"In listener button.");
- try {
- if (socket != null)
- socket.getOutputStream().write("Hello, world!".getBytes());
- else
- Toast.makeText(BluetoothClient.this, "Not connected to destkop application.", Toast.LENGTH_SHORT).show();
- } catch (IOException e) {
- Log.d(TAG, e.toString());
- e.printStackTrace();
- }
- }
- });
- }
- catch(Exception ex)
- {
- Log.d(TAG,ex.toString());
- }
- }
- private void connectToBondedDevice(){
- Set<BluetoothDevice> setOfDevices = mBtAdapter.getBondedDevices();
- if (setOfDevices.isEmpty())
- Log.d(TAG, "NULL BLUETOOTH SET");
- for (BluetoothDevice bt : setOfDevices){
- if (bt.getAddress().equals("50:63:13:A1:21:9E")){
- try {
- Method m;
- try {
- m = bt.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
- socket = (BluetoothSocket) m.invoke(bt, 1);
- } catch (Exception e) {
- Log.d(TAG, e.toString());
- }
- Log.d(TAG, "about to connect");
- mBtAdapter.cancelDiscovery();
- socket.connect();
- Log.d(TAG, "Connected!");
- } catch (IOException e) {
- Log.d(TAG, e.toString() + " " + e.getStackTrace().toString());
- }
- }
- }
- }
- @Override
- protected void onPause() {
- super.onPause();
- Log.d(TAG, "ON PAUSE");
- try {
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- Log.d(TAG, "ON DESTROY");
- try {
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @Override
- protected void onResume() {
- super.onResume();
- Log.d(TAG, "ON RESUME");
- }
- @Override
- public void onBackPressed() {
- super.onBackPressed();
- try {
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement