Android Open Source - mha-android Bluetooth Service






From Project

Back to project page mha-android.

License

The source code is released under:

Copyright (c) 2011-2012 Cameron Porter, Ryan Brown http://github.com/camporter/mha-android Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated...

If you think the Android project mha-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.teamacra.myhomeaudio.bluetooth;
/*w  w  w  . ja va2 s . c o  m*/
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

public class BluetoothService extends Service {

  private final String TAG = "BluetoothService";

  public static final String DEVICE_UPDATE = "com.teamacra.myhomeaudio.bluetooth.device";
  public static final String DISCOVERY_START = "com.teamacra.myhomeaudio.bluetooth.start";
  public static final String DISCOVERY_FINISH = "com.teamacra.myhomeaudio.bluetooth.finish";

  private BluetoothAdapter mAdapter;

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    Log.i(TAG, "BluetoothService is being created");

    mAdapter = BluetoothAdapter.getDefaultAdapter();
    registerReceiver(mReceiver, new IntentFilter(
        BluetoothDevice.ACTION_FOUND));
    registerReceiver(mReceiver, new IntentFilter(
        BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
    registerReceiver(mReceiver, new IntentFilter(
        BluetoothAdapter.ACTION_DISCOVERY_STARTED));

  }

  @Override
  public void onStart(Intent intent, int startid) {
    Log.i(TAG, "BluetoothService is being started");
    if (!mAdapter.isDiscovering()) {
      mAdapter.startDiscovery();
    } else {
      stopSelf();
    }
  }

  @Override
  public void onDestroy() {
    Log.i(TAG, "MHA BluetoothService being destroyed!");
    unregisterReceiver(mReceiver);
    super.onDestroy();

  }

  private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
      String action = intent.getAction();

      if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        // Discovery has found a device
        Log.i(TAG, "A bluetooth device was found");

        // Get the corresponding BluetoothDevice object
        final BluetoothDevice device = intent
            .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        // get the RSSI value for this device
        Integer rssi = (int) intent.getShortExtra(
            BluetoothDevice.EXTRA_RSSI, (short) 0);

        // Broadcast to anyone listening all about the device found
        Intent bIntent = new Intent();
        bIntent.setAction(BluetoothService.DEVICE_UPDATE);
        bIntent.putExtra("deviceName", device.getName());
        bIntent.putExtra("deviceAddress", device.getAddress());
        bIntent.putExtra("deviceRssi", rssi);
        context.sendBroadcast(bIntent);

      } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
          .equals(action)) {
        // Done trying to discover bluetooth devices
        Log.i(TAG, "Bluetooth discovery finished");

        Intent finishIntent = new Intent();
        finishIntent.setAction(BluetoothService.DISCOVERY_FINISH);
        context.sendBroadcast(finishIntent);

        BluetoothService.this.mAdapter.cancelDiscovery();
        BluetoothService.this.stopSelf();

      } else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
        Log.i(TAG, "Bluetooth discovery starting");
        Intent startIntent = new Intent();
        startIntent.setAction(BluetoothService.DISCOVERY_START);
      }

    }
  };

}




Java Source Code List

com.teamacra.myhomeaudio.MHAApplication.java
com.teamacra.myhomeaudio.bluetooth.BluetoothService.java
com.teamacra.myhomeaudio.discovery.DiscoveryConstants.java
com.teamacra.myhomeaudio.discovery.DiscoveryDescription.java
com.teamacra.myhomeaudio.discovery.DiscoverySearchListener.java
com.teamacra.myhomeaudio.discovery.DiscoverySearch.java
com.teamacra.myhomeaudio.discovery.MDNSDiscovery.java
com.teamacra.myhomeaudio.http.HttpBase.java
com.teamacra.myhomeaudio.http.HttpClient.java
com.teamacra.myhomeaudio.http.HttpNode.java
com.teamacra.myhomeaudio.http.HttpSource.java
com.teamacra.myhomeaudio.http.HttpStream.java
com.teamacra.myhomeaudio.http.StatusCode.java
com.teamacra.myhomeaudio.locations.NodeSignalRange.java
com.teamacra.myhomeaudio.locations.NodeSignature.java
com.teamacra.myhomeaudio.manager.ConfigurationManager.java
com.teamacra.myhomeaudio.manager.LocationManager.java
com.teamacra.myhomeaudio.manager.NodeManager.java
com.teamacra.myhomeaudio.manager.StreamManager.java
com.teamacra.myhomeaudio.media.MediaDescriptor.java
com.teamacra.myhomeaudio.node.Node.java
com.teamacra.myhomeaudio.source.Source.java
com.teamacra.myhomeaudio.stream.StreamAction.java
com.teamacra.myhomeaudio.stream.Stream.java
com.teamacra.myhomeaudio.ui.InitialConfigActivity.java
com.teamacra.myhomeaudio.ui.LoginActivity.java
com.teamacra.myhomeaudio.ui.MyHomeAudioActivity.java
com.teamacra.myhomeaudio.ui.RegisterActivity.java
com.teamacra.myhomeaudio.ui.fragment.SongFragment.java
com.teamacra.myhomeaudio.ui.fragment.SourceFragment.java
com.teamacra.myhomeaudio.ui.fragment.TestFragment.java