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 dd3ab16 commit 841034a
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import android.content.IntentFilter;

import java.util.Set;
import java.util.UUID;

import co.lujun.lmbluetoothsdk.base.BaseManager;
import co.lujun.lmbluetoothsdk.base.BluetoothListener;
Expand Down Expand Up @@ -201,6 +202,9 @@ public void disconnect() {
if (mBluetoothService != null){
mBluetoothService.stop();
}
if (mContext != null && mReceiver != null){
mContext.unregisterReceiver(mReceiver);
}
}

@Override
Expand All @@ -209,4 +213,25 @@ public void write(byte[] data) {
mBluetoothService.write(data);
}
}

/**
* Get UUID.
* @return
*/
public UUID getAppUuid(){
if (mBluetoothService != null){
return mBluetoothService.getAppUuid();
}
return null;
}

/**
* Set UUID.
* @param uuid
*/
public void setAppUuid(UUID uuid){
if (mBluetoothService != null){
mBluetoothService.setAppUuid(uuid);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package co.lujun.lmbluetoothsdk.base;

/**
* Author: lujun(http://blog.lujun.co)
* Date: 2016/1/19 12:12
*/
public class State {

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

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

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

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

// unknown state
public static final int STATE_UNKNOWN = 4;
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void onReceive(Context context, Intent intent) {

case BluetoothDevice.ACTION_FOUND:
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mBluetoothListener.onActionFound(device);
mBluetoothListener.onActionDeviceFound(device);
break;

case BluetoothAdapter.ACTION_SCAN_MODE_CHANGED:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.UUID;

import co.lujun.lmbluetoothsdk.base.BluetoothListener;
import co.lujun.lmbluetoothsdk.base.State;

/**
* Author: lujun(http://blog.lujun.co)
Expand All @@ -37,23 +38,18 @@ public class BluetoothService {

private static final String TAG = "LMBluetoothSdk";

private static final UUID ANDROID_UUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");

private final BluetoothAdapter mAdapter;
private AcceptThread mAcceptThread;
private ConnectThread mConnectThread;
private ConnectedThread mConnectedThread;
private BluetoothListener mBluetoothListener;

private int mState;
public static final int STATE_NONE = 0; // we're doing nothing
public static final int STATE_LISTEN = 1; // now listening for incoming connections
public static final int STATE_CONNECTING = 2; // now initiating an outgoing connection
public static final int STATE_CONNECTED = 3; // now connected to a remote device
private UUID mAppUuid = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");

public BluetoothService() {
mAdapter = BluetoothAdapter.getDefaultAdapter();
mState = STATE_NONE;
mState = State.STATE_NONE;
}

/**
Expand All @@ -64,6 +60,22 @@ public synchronized void setBluetoothListener(BluetoothListener listener) {
this.mBluetoothListener = listener;
}

/**
* Get UUID.
* @return
*/
public UUID getAppUuid() {
return mAppUuid;
}

/**
* Set UUID.
* @param uuid
*/
public void setAppUuid(UUID uuid) {
this.mAppUuid = uuid;
}

/**
* Set the current state of the connection.
* @param state An integer defining the current connection state
Expand Down Expand Up @@ -93,7 +105,7 @@ public synchronized void start() {
mAcceptThread = new AcceptThread();
mAcceptThread.start();
}
setState(STATE_LISTEN);
setState(State.STATE_LISTEN);
}

/**
Expand All @@ -102,7 +114,7 @@ public synchronized void start() {
*/
public synchronized void connect(BluetoothDevice device) {
Log.d(TAG, "connect to: " + device);
if (mState == STATE_CONNECTING && mConnectThread != null) {
if (mState == State.STATE_CONNECTING && mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
Expand All @@ -112,7 +124,7 @@ public synchronized void connect(BluetoothDevice device) {
}
mConnectThread = new ConnectThread(device);
mConnectThread.start();
setState(STATE_CONNECTING);
setState(State.STATE_CONNECTING);
}
/**
* Start the ConnectedThread to begin managing a Bluetooth connection.
Expand All @@ -135,7 +147,7 @@ public synchronized void connected(BluetoothSocket socket, BluetoothDevice devic
}
mConnectedThread = new ConnectedThread(socket);
mConnectedThread.start();
setState(STATE_CONNECTED);
setState(State.STATE_CONNECTED);
}

/**
Expand All @@ -155,7 +167,7 @@ public synchronized void stop() {
mAcceptThread.cancel();
mAcceptThread = null;
}
setState(STATE_NONE);
setState(State.STATE_NONE);
}

/**
Expand All @@ -166,7 +178,7 @@ public synchronized void stop() {
public void write(byte[] out) {
ConnectedThread r;
synchronized (this) {
if (mState != STATE_CONNECTED) {
if (mState != State.STATE_CONNECTED) {
return;
}
r = mConnectedThread;
Expand All @@ -186,7 +198,7 @@ private class AcceptThread extends Thread {
public AcceptThread() {
BluetoothServerSocket tmp = null;
try {
tmp = mAdapter.listenUsingRfcommWithServiceRecord(TAG, ANDROID_UUID);
tmp = mAdapter.listenUsingRfcommWithServiceRecord(TAG, mAppUuid);
} catch (IOException e) {
Log.e(TAG, "listen() failed", e);
}
Expand All @@ -196,7 +208,7 @@ public AcceptThread() {
public void run() {
Log.d(TAG, "BEGIN mAcceptThread" + this);
BluetoothSocket socket = null;
while (mState != STATE_CONNECTED) {
while (mState != co.lujun.lmbluetoothsdk.base.State.STATE_CONNECTED) {
try {
socket = mmServerSocket.accept();
} catch (IOException e) {
Expand All @@ -206,12 +218,12 @@ public void run() {
if (socket != null) {
synchronized (BluetoothService.this) {
switch (mState) {
case STATE_LISTEN:
case STATE_CONNECTING:
case co.lujun.lmbluetoothsdk.base.State.STATE_LISTEN:
case co.lujun.lmbluetoothsdk.base.State.STATE_CONNECTING:
connected(socket, socket.getRemoteDevice());
break;
case STATE_NONE:
case STATE_CONNECTED:
case co.lujun.lmbluetoothsdk.base.State.STATE_NONE:
case co.lujun.lmbluetoothsdk.base.State.STATE_CONNECTED:
try {
socket.close();
} catch (IOException e) {
Expand Down Expand Up @@ -251,7 +263,7 @@ public ConnectThread(BluetoothDevice device) {
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
tmp = device.createRfcommSocketToServiceRecord(ANDROID_UUID);
tmp = device.createRfcommSocketToServiceRecord(mAppUuid);
} catch (IOException e) {
Log.e(TAG, "create() failed", e);
}
Expand All @@ -268,7 +280,7 @@ public void run() {
// successful connection or an exception
mmSocket.connect();
} catch (IOException e) {
setState(STATE_LISTEN);
setState(co.lujun.lmbluetoothsdk.base.State.STATE_LISTEN);
// Close the socket
try {
mmSocket.close();
Expand Down Expand Up @@ -333,7 +345,7 @@ public void run() {
}
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
setState(STATE_LISTEN);
setState(co.lujun.lmbluetoothsdk.base.State.STATE_LISTEN);
break;
}
}
Expand Down
51 changes: 39 additions & 12 deletions sample/src/main/java/co/lujun/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import co.lujun.lmbluetoothsdk.BluetoothManager;
import co.lujun.lmbluetoothsdk.base.BluetoothListener;
import co.lujun.lmbluetoothsdk.base.State;

public class MainActivity extends AppCompatActivity {

private BluetoothManager mBluetoothManager;

private Button btnScanAvaliabe, btnScan, btnSend, btnOpen, btnStartServer;
private TextView tvContent;
private TextView tvContent, tvState;
private EditText etSend;
private ListView lvDevices;

private List<String> mDevicesList;
private BluetoothDevice mConnectDevice;
private List<BluetoothDevice> mDevicesList;
private List<String> mList;
private BaseAdapter mFoundAdapter;

private static final String TAG = "LMBluetoothSdk";
Expand All @@ -51,6 +55,7 @@ protected void onCreate(Bundle savedInstanceState) {

private void initBT(){
mBluetoothManager = BluetoothManager.getInstance().build(this);
mBluetoothManager.setAppUuid(UUID.fromString("fa87c0d0-afac-12de-8a39-0450200c9a66"));
mBluetoothManager.setBluetoothListener(new BluetoothListener() {
@Override
public void onActionStateChanged(int preState, int state) {
Expand All @@ -68,13 +73,31 @@ public void onActionScanModeChanged(int preScanMode, int scanMode) {
}

@Override
public void onBluetoothServiceStateChanged(int state) {
Log.d(TAG, "Service State:" + state);
public void onBluetoothServiceStateChanged(final int state) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String stateStr;
if (state == State.STATE_NONE) {
stateStr = "STATE_NONE";
}else if (state == State.STATE_LISTEN) {
stateStr = "STATE_LISTEN";
}else if (state == State.STATE_CONNECTING) {
stateStr = "STATE_CONNECTING";
}else if (state == State.STATE_CONNECTED) {
stateStr = "STATE_CONNECTED";
}else {
stateStr = "STATE_UNKNOWN";
}
tvState.setText("State: " + stateStr);
}
});
}

@Override
public void onActionDeviceFound(BluetoothDevice device) {
mDevicesList.add(device.getName() + "@" + device.getAddress());
mDevicesList.add(device);
mList.add(device.getName() + "@" + device.getAddress());
mFoundAdapter.notifyDataSetChanged();
}

Expand All @@ -83,28 +106,31 @@ public void onReadData(final byte[] data) {
runOnUiThread(new Runnable() {
@Override
public void run() {
tvContent.append(new String(data) + "\n");
String deviceName = mConnectDevice == null ? "" : mConnectDevice.getName();
tvContent.append(deviceName + ": " + new String(data) + "\n");
}
});
}
});
}

private void init(){
mDevicesList = new ArrayList<String>();
mFoundAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDevicesList);

initBT();
mDevicesList = new ArrayList<BluetoothDevice>();
mList = new ArrayList<String>();
mFoundAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mList);

btnScanAvaliabe = (Button) findViewById(R.id.btn_scan_avaliable);
btnScan = (Button) findViewById(R.id.btn_scan);
btnSend = (Button) findViewById(R.id.btn_send);
btnOpen = (Button) findViewById(R.id.btn_open_bt);
btnStartServer = (Button) findViewById(R.id.btn_start_as_server);
tvContent = (TextView) findViewById(R.id.tv_chat_content);
tvState = (TextView) findViewById(R.id.tv_state);
etSend = (EditText) findViewById(R.id.et_send_content);
lvDevices = (ListView) findViewById(R.id.lv_devices);

initBT();

lvDevices.setAdapter(mFoundAdapter);

btnScanAvaliabe.setOnClickListener(new View.OnClickListener() {
Expand All @@ -128,7 +154,7 @@ public void onClick(View v) {
return;
}
mBluetoothManager.write(msg.getBytes());
tvContent.append(msg + "\n");
tvContent.append("Me: " + msg + "\n");
}
});
btnOpen.setOnClickListener(new View.OnClickListener() {
Expand All @@ -148,7 +174,8 @@ public void onClick(View v) {
lvDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemStr = mDevicesList.get(position);
mConnectDevice = mDevicesList.get(position);
String itemStr = mList.get(position);
mBluetoothManager.connect(itemStr.substring(itemStr.length() - 17));
}
});
Expand Down
6 changes: 6 additions & 0 deletions sample/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
android:text="打开服务端" />
</LinearLayout>

<TextView
android:id="@+id/tv_state"
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="150dp">
Expand Down

0 comments on commit 841034a

Please sign in to comment.