it.gerdavax.android.bluetooth
Class LocalBluetoothDevice

java.lang.Object
  extended by it.gerdavax.android.bluetooth.LocalBluetoothDevice
All Implemented Interfaces:
BluetoothDevice

public final class LocalBluetoothDevice
extends java.lang.Object
implements BluetoothDevice

This class is the entry point of the Android Bluetooth Library. It implements a singleton pattern.
WARNING: do not forget to invoke close(Context) when your application leaves, otherwise the LocalBluetoothDevice may not work on next invocations.
First step for using the Bluetooth services is to initialize the instance of the LocalBluetoothDevice:

 LocalBluetoothDevice localBluetoothDevice = LocalBluetoothDevice.init(context);
 
where context is the context of Activity which manages the LocalBluetoothDevice instance. Bluetooth service status (enabled/disabled) can be retrieved using method
 localBluetoothDevice.isEnabled();
 
and the service can be enabled and disabled using method
 localBluetoothDevice.setEnabled(boolean);
 
To start scanning (discovery) of surrounding Bluetooth devices just invoke the
 localBluetoothDevice.scan();
 
method. Both service on/off and device scanning are asynchronous processes, therefore to receive notification about that, the application must provide a LocalBluetoothDeviceListener and set it to the LocalBluetoothDevice instance.

Author:
Stefano Sanna - gerdavax@gmail.com - http://www.gerdavax.it

Nested Class Summary
 
Nested classes/interfaces inherited from interface it.gerdavax.android.bluetooth.BluetoothDevice
BluetoothDevice.BluetoothClasses, BluetoothDevice.BluetoothProfiles, BluetoothDevice.BluetoothProtocols
 
Field Summary
static int SCAN_MODE_CONNECTABLE
          The local device is connectable, however it is not discoverable
static int SCAN_MODE_CONNECTABLE_DISCOVERABLE
          The local device is both discoverable and connectable
static int SCAN_MODE_NONE
          The local device is not discoverable nor connectable
 
Method Summary
 void clearSystemNotification()
          Removes the "Pin requested" notification from the notification area
 void close()
          Closes the LocalBluetoothDevice and unregisters any Bluetooth service broadcast listener.
 java.lang.String getAddress()
          Gets the BD Address of local device
 java.lang.String getCompany()
           
static LocalBluetoothDevice getLocalDevice()
          Gets unique instance of LocalBluetoothDevice.
 java.lang.String getManufacturer()
           
 java.lang.String getName()
          Gets friendly name assigned to local device
 RemoteBluetoothDevice getRemoteBluetoothDevice(java.lang.String address)
           
 int getRemoteClass(java.lang.String address)
           
 int getScanMode()
          Returns current scan mode
static LocalBluetoothDevice initLocalDevice(android.content.Context context)
           
 boolean isEnabled()
           
 boolean isScanning()
          Checks if scanning is in progress
 BluetoothSocket openServerSocket()
          Opens a server socket on the first available channel (between 12 and 30)
 BluetoothSocket openServerSocket(int channel)
          Opens a server socket on given channel (between 12 and 30)
 void scan()
          Starts discovery process for remote Bluetooth devices
 boolean setEnabled(boolean enabled)
          Enables the Bluetooth service on this handset
 void setListener(LocalBluetoothDeviceListener listener)
          Sets the LocalBluetoothDeviceListener which will receive events about Bluetooth activation and device discovery progress WARNING: all invocations to the listener are synchronous, therefore listener's method implementation must avoid long operations.
 void setScanMode(int mode)
          Configures current scan mode
 void showDefaultPinInputActivity(java.lang.String address, boolean clearSystemNotification)
          Shows system activity to input PIN for assigned device Thanks to Emanuele Di Saverio
 void stopScanning()
          Stops current device scanning (only if it has been started by this LocalBluetoothDevice)
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SCAN_MODE_NONE

public static final int SCAN_MODE_NONE
The local device is not discoverable nor connectable

Since:
0.3
See Also:
Constant Field Values

SCAN_MODE_CONNECTABLE

public static final int SCAN_MODE_CONNECTABLE
The local device is connectable, however it is not discoverable

Since:
0.3
See Also:
Constant Field Values

SCAN_MODE_CONNECTABLE_DISCOVERABLE

public static final int SCAN_MODE_CONNECTABLE_DISCOVERABLE
The local device is both discoverable and connectable

Since:
0.3
See Also:
Constant Field Values
Method Detail

initLocalDevice

public static LocalBluetoothDevice initLocalDevice(android.content.Context context)
                                            throws java.lang.Exception
Parameters:
context - the context of the Activity which uses the LocalBluetoothDevice
Returns:
the unique instance of LocalBluetoothDevice
Throws:
java.lang.Exception - if system's Bluetooth service can't be addressed

getLocalDevice

public static LocalBluetoothDevice getLocalDevice()
                                           throws java.lang.IllegalStateException
Gets unique instance of LocalBluetoothDevice.

Returns:
Throws:
java.lang.IllegalStateException

getAddress

public java.lang.String getAddress()
                            throws BluetoothException
Gets the BD Address of local device

Specified by:
getAddress in interface BluetoothDevice
Returns:
Throws:
java.lang.Exception
BluetoothException

getName

public java.lang.String getName()
                         throws BluetoothException
Gets friendly name assigned to local device

Specified by:
getName in interface BluetoothDevice
Returns:
The friendly name of this device
Throws:
BluetoothException

getManufacturer

public java.lang.String getManufacturer()
                                 throws java.lang.Exception
Throws:
java.lang.Exception

getCompany

public java.lang.String getCompany()
                            throws java.lang.Exception
Throws:
java.lang.Exception

scan

public void scan()
          throws java.lang.Exception
Starts discovery process for remote Bluetooth devices

Throws:
java.lang.Exception

isScanning

public boolean isScanning()
                   throws java.lang.Exception
Checks if scanning is in progress

Returns:
true device discovery is running
Throws:
java.lang.Exception
Since:
0.2

stopScanning

public void stopScanning()
                  throws java.lang.Exception
Stops current device scanning (only if it has been started by this LocalBluetoothDevice)

Throws:
java.lang.Exception
Since:
0.2

setListener

public void setListener(LocalBluetoothDeviceListener listener)
Sets the LocalBluetoothDeviceListener which will receive events about Bluetooth activation and device discovery progress WARNING: all invocations to the listener are synchronous, therefore listener's method implementation must avoid long operations.

Parameters:
listener -

getRemoteClass

public int getRemoteClass(java.lang.String address)
                   throws java.lang.Exception
Parameters:
address -
Returns:
Throws:
java.lang.Exception

close

public void close()
Closes the LocalBluetoothDevice and unregisters any Bluetooth service broadcast listener. All opened BluetoothSocket will be closed.


isEnabled

public boolean isEnabled()
                  throws java.lang.Exception
Returns:
true is Bluetooth is enabled
Throws:
java.lang.Exception

setEnabled

public boolean setEnabled(boolean enabled)
                   throws BluetoothException
Enables the Bluetooth service on this handset

Parameters:
enabled -
Returns:
true if the Bluetooth service can be enabled
Throws:
BluetoothException

getRemoteBluetoothDevice

public RemoteBluetoothDevice getRemoteBluetoothDevice(java.lang.String address)
Parameters:
address - the BDADDR (address) of required device
Returns:
the RemoteBluetoothDevice instance associated with assigned address

getScanMode

public int getScanMode()
                throws java.lang.Exception
Returns current scan mode

Throws:
java.lang.Exception
Since:
0.3

setScanMode

public void setScanMode(int mode)
                 throws java.lang.Exception
Configures current scan mode

Throws:
java.lang.Exception
Since:
0.3

openServerSocket

public BluetoothSocket openServerSocket()
                                 throws BluetoothException
Opens a server socket on the first available channel (between 12 and 30)

Throws:
BluetoothException
Since:
0.3

openServerSocket

public BluetoothSocket openServerSocket(int channel)
                                 throws BluetoothException
Opens a server socket on given channel (between 12 and 30)

Throws:
BluetoothException
Since:
0.3

showDefaultPinInputActivity

public void showDefaultPinInputActivity(java.lang.String address,
                                        boolean clearSystemNotification)
Shows system activity to input PIN for assigned device Thanks to Emanuele Di Saverio

Since:
0.3

clearSystemNotification

public void clearSystemNotification()
Removes the "Pin requested" notification from the notification area

Since:
0.3