Skip to content

Commit

Permalink
Small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
whilu committed Jan 19, 2016
1 parent 841034a commit 25a46d1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,42 @@
*/
public interface BluetoothListener {

/**
* Callback when bluetooth power state changed.
*
* @param preState previous power state
* @param state current power state
*/
void onActionStateChanged(int preState, int state);

/**
* Callback when local Bluetooth adapter discovery process state changed.
* @param discoveryState the state of local Bluetooth adapter discovery process.
*/
void onActionDiscoveryStateChanged(String discoveryState);

/**
* Callback when the current scan mode changed.
* @param preScanMode previous scan mode
* @param scanMode current scan mode
*/
void onActionScanModeChanged(int preScanMode, int scanMode);

/**
* Callback when the connection state changed.
* @param state connection state
*/
void onBluetoothServiceStateChanged(int state);

/**
* Callback when found device.
* @param device a remote device
*/
void onActionDeviceFound(BluetoothDevice device);

/**
* Callback when remote device send data to current device.
* @param data data
*/
void onReadData(byte[] data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
*/
public class State {

// we're doing nothing
/** we're doing nothing*/
public static final int STATE_NONE = 0;

// now listening for incoming connections
/** now listening for incoming connections*/
public static final int STATE_LISTEN = 1;

// now initiating an outgoing connection
/** now initiating an outgoing connection*/
public static final int STATE_CONNECTING = 2;

// now connected to a remote device
/** now connected to a remote device*/
public static final int STATE_CONNECTED = 3;

// unknown state
/** unknown state*/
public static final int STATE_UNKNOWN = 4;
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ private synchronized void setState(int state) {
* Called by the Activity onResume()
*/
public synchronized void start() {
Log.d(TAG, "start");
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
Expand All @@ -113,7 +112,6 @@ public synchronized void start() {
* @param device The BluetoothDevice to connect
*/
public synchronized void connect(BluetoothDevice device) {
Log.d(TAG, "connect to: " + device);
if (mState == State.STATE_CONNECTING && mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
Expand All @@ -129,10 +127,8 @@ public synchronized void connect(BluetoothDevice device) {
/**
* Start the ConnectedThread to begin managing a Bluetooth connection.
* @param socket The BluetoothSocket on which the connection was made
* @param device The BluetoothDevice that has been connected
*/
public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
Log.d(TAG, "connected");
public synchronized void connected(BluetoothSocket socket) {
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
Expand All @@ -154,7 +150,6 @@ public synchronized void connected(BluetoothSocket socket, BluetoothDevice devic
* Stop all threads.
*/
public synchronized void stop() {
Log.d(TAG, "stop");
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
Expand Down Expand Up @@ -206,7 +201,6 @@ public AcceptThread() {
}

public void run() {
Log.d(TAG, "BEGIN mAcceptThread" + this);
BluetoothSocket socket = null;
while (mState != co.lujun.lmbluetoothsdk.base.State.STATE_CONNECTED) {
try {
Expand All @@ -220,7 +214,7 @@ public void run() {
switch (mState) {
case co.lujun.lmbluetoothsdk.base.State.STATE_LISTEN:
case co.lujun.lmbluetoothsdk.base.State.STATE_CONNECTING:
connected(socket, socket.getRemoteDevice());
connected(socket);
break;
case co.lujun.lmbluetoothsdk.base.State.STATE_NONE:
case co.lujun.lmbluetoothsdk.base.State.STATE_CONNECTED:
Expand All @@ -234,11 +228,9 @@ public void run() {
}
}
}
Log.i(TAG, "END mAcceptThread");
}

public void cancel() {
Log.d(TAG, "cancel " + this);
try {
mmServerSocket.close();
} catch (IOException e) {
Expand All @@ -255,13 +247,9 @@ public void cancel() {
private class ConnectThread extends Thread {

private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;

public ConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
tmp = device.createRfcommSocketToServiceRecord(mAppUuid);
} catch (IOException e) {
Expand All @@ -271,32 +259,23 @@ public ConnectThread(BluetoothDevice device) {
}

public void run() {
Log.i(TAG, "BEGIN mConnectThread");
// Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery();
// Make a connection to the BluetoothSocket
try {
// This is a blocking call and will only return on a
// successful connection or an exception
mmSocket.connect();
} catch (IOException e) {
setState(co.lujun.lmbluetoothsdk.base.State.STATE_LISTEN);
// Close the socket
try {
mmSocket.close();
} catch (IOException e2) {
Log.e(TAG, "unable to close() socket during connection failure", e2);
}
// Start the service over to restart listening mode
BluetoothService.this.start();
return;
}
// Reset the ConnectThread because we're done
synchronized (BluetoothService.this) {
mConnectThread = null;
}
// Start the connected thread
connected(mmSocket, mmDevice);
connected(mmSocket);
}

public void cancel() {
Expand All @@ -319,7 +298,6 @@ private class ConnectedThread extends Thread {
private final OutputStream mmOutStream;

public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
Expand All @@ -334,7 +312,6 @@ public ConnectedThread(BluetoothSocket socket) {
}

public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
while (true) {
Expand All @@ -353,7 +330,7 @@ public void run() {

/**
* Write to the connected OutStream.
* @param buffer The bytes to write
* @param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
Expand Down

0 comments on commit 25a46d1

Please sign in to comment.