Skip to content

Commit

Permalink
Finished part.1
Browse files Browse the repository at this point in the history
  • Loading branch information
whilu committed Jan 20, 2016
1 parent 25a46d1 commit 1175ba6
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import co.lujun.lmbluetoothsdk.base.BaseManager;
import co.lujun.lmbluetoothsdk.base.BluetoothListener;
import co.lujun.lmbluetoothsdk.base.State;
import co.lujun.lmbluetoothsdk.receiver.BlueToothReceiver;
import co.lujun.lmbluetoothsdk.service.BluetoothService;

Expand Down Expand Up @@ -119,11 +120,11 @@ public boolean isEnabled(){
}

@Override
public void openBluetooth(){
public boolean openBluetooth(){
if (!isAvaliable()){
return;
return false;
}
mBluetoothAdapter.enable();
return mBluetoothAdapter.enable();
}

@Override
Expand All @@ -145,6 +146,14 @@ public boolean setDiscoverable(int time){
return true;
}

@Override
public int getBluetoothState() {
if (!isAvaliable()){
return BluetoothAdapter.STATE_OFF;
}
return mBluetoothAdapter.getState();
}

@Override
public boolean startScan() {
if (!isAvaliable() && !isEnabled()){
Expand Down Expand Up @@ -202,9 +211,17 @@ public void disconnect() {
if (mBluetoothService != null){
mBluetoothService.stop();
}
if (mContext != null && mReceiver != null){
mContext.unregisterReceiver(mReceiver);
}

/**
* Get connection state.
* @return
*/
public int getConnectionState(){
if (mBluetoothService != null){
return mBluetoothService.getState();
}
return State.STATE_UNKNOWN;
}

@Override
Expand All @@ -214,6 +231,13 @@ public void write(byte[] data) {
}
}

// public <T extends Object> void write(T obj){
//
// if (mBluetoothService != null){
//
// }
// }

/**
* Get UUID.
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ public interface BaseManager {

/**
* Open bluetooth.
* @return
*/
void openBluetooth();
boolean openBluetooth();

/**
* Close bluetooth.
Expand All @@ -65,6 +66,12 @@ public interface BaseManager {
*/
boolean setDiscoverable(int time);

/**
* Get current bluetooth state.
* @return
*/
int getBluetoothState();

/**
* Start scan.
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public interface BluetoothListener {

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

private int mState;

// Hint: If you are connecting to a Bluetooth serial board then try
// using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB.
private UUID mAppUuid = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");

public BluetoothService() {
Expand Down Expand Up @@ -87,6 +90,14 @@ private synchronized void setState(int state) {
}
}

/**
* Get the current state of connection.
* @return
*/
public int getState() {
return mState;
}

/**
* Start AcceptThread to begin a session in listening (server) mode.
* Called by the Activity onResume()
Expand Down Expand Up @@ -318,7 +329,7 @@ public void run() {
try {
bytes = mmInStream.read(buffer);
if (mBluetoothListener != null){
mBluetoothListener.onReadData(buffer);
mBluetoothListener.onReadData(mmSocket.getRemoteDevice(), buffer);
}
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
Expand Down
104 changes: 79 additions & 25 deletions sample/src/main/java/co/lujun/sample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package co.lujun.sample;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
Expand All @@ -18,6 +19,7 @@
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -31,15 +33,15 @@ public class MainActivity extends AppCompatActivity {

private BluetoothManager mBluetoothManager;

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

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

private static final String TAG = "LMBluetoothSdk";

Expand All @@ -59,12 +61,16 @@ private void initBT(){
mBluetoothManager.setBluetoothListener(new BluetoothListener() {
@Override
public void onActionStateChanged(int preState, int state) {
Log.d(TAG, "preState:" + preState + ", state:" + state);
tvBTState.setText("BT state: " + transBtStateAsString(state));
}

@Override
public void onActionDiscoveryStateChanged(String discoveryState) {
Log.d(TAG, "discoveryState:" + discoveryState);
if (discoveryState.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) {
Toast.makeText(MainActivity.this, "scanning!", Toast.LENGTH_SHORT).show();
} else if (discoveryState.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
Toast.makeText(MainActivity.this, "scan finished!", Toast.LENGTH_SHORT).show();
}
}

@Override
Expand All @@ -74,22 +80,12 @@ public void onActionScanModeChanged(int preScanMode, int scanMode) {

@Override
public void onBluetoothServiceStateChanged(final int state) {
// If you want to update UI, please run this on UI thread
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);
mConnectState = state;
tvConnectState.setText("Connection state: " + transConnStateAsString(state));
}
});
}
Expand All @@ -102,11 +98,12 @@ public void onActionDeviceFound(BluetoothDevice device) {
}

@Override
public void onReadData(final byte[] data) {
public void onReadData(final BluetoothDevice device, final byte[] data) {
// If you want to update UI, please run this on UI thread
runOnUiThread(new Runnable() {
@Override
public void run() {
String deviceName = mConnectDevice == null ? "" : mConnectDevice.getName();
String deviceName = device == null ? "" : device.getName();
tvContent.append(deviceName + ": " + new String(data) + "\n");
}
});
Expand All @@ -124,14 +121,20 @@ private void init(){
btnSend = (Button) findViewById(R.id.btn_send);
btnOpen = (Button) findViewById(R.id.btn_open_bt);
btnStartServer = (Button) findViewById(R.id.btn_start_as_server);
btnDisconnect = (Button) findViewById(R.id.btn_disconnect);
tvContent = (TextView) findViewById(R.id.tv_chat_content);
tvState = (TextView) findViewById(R.id.tv_state);
tvConnectState = (TextView) findViewById(R.id.tv_connect_state);
tvBTState = (TextView) findViewById(R.id.tv_bt_state);
etSend = (EditText) findViewById(R.id.et_send_content);
lvDevices = (ListView) findViewById(R.id.lv_devices);

initBT();

lvDevices.setAdapter(mFoundAdapter);
tvConnectState.setText("Connection state: "
+ transConnStateAsString(mBluetoothManager.getConnectionState()));
tvBTState.setText("BT state: "
+ transBtStateAsString(mBluetoothManager.getBluetoothState()));

btnScanAvaliabe.setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -143,14 +146,20 @@ public void onClick(View v) {
btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBluetoothManager.startScan();
if(!mBluetoothManager.startScan()){
Toast.makeText(MainActivity.this, "Start scan failed!",
Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "Start scan success!",
Toast.LENGTH_SHORT).show();
}
}
});
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = etSend.getText().toString();
if (TextUtils.isEmpty(msg)){
if (TextUtils.isEmpty(msg)) {
return;
}
mBluetoothManager.write(msg.getBytes());
Expand All @@ -160,24 +169,69 @@ public void onClick(View v) {
btnOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mBluetoothManager.isEnabled()){
if (!mBluetoothManager.isEnabled()) {
mBluetoothManager.openBluetooth();
} else {
Toast.makeText(MainActivity.this, "Bluetooth has opened!",
Toast.LENGTH_SHORT).show();
}
}
});
btnStartServer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBluetoothManager.startAsServer();
Toast.makeText(MainActivity.this, "Start as a server!",
Toast.LENGTH_SHORT).show();
}
});
btnDisconnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mConnectState == State.STATE_CONNECTED) {
mBluetoothManager.disconnect();
}else {
Toast.makeText(MainActivity.this, "Connect is unavaliable!",
Toast.LENGTH_SHORT).show();
}
}
});
lvDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mConnectDevice = mDevicesList.get(position);
String itemStr = mList.get(position);
mBluetoothManager.connect(itemStr.substring(itemStr.length() - 17));
}
});
}

private String transBtStateAsString(int state){
String result = "UNKNOWN";
if (state == BluetoothAdapter.STATE_TURNING_ON) {
result = "TURNING_ON";
} else if (state == BluetoothAdapter.STATE_ON) {
result = "ON";
} else if (state == BluetoothAdapter.STATE_TURNING_OFF) {
result = "TURNING_OFF";
}else if (state == BluetoothAdapter.STATE_OFF) {
result = "OFF";
}
return result;
}

private String transConnStateAsString(int state){
String result;
if (state == State.STATE_NONE) {
result = "NONE";
} else if (state == State.STATE_LISTEN) {
result = "LISTEN";
} else if (state == State.STATE_CONNECTING) {
result = "CONNECTING";
} else if (state == State.STATE_CONNECTED) {
result = "CONNECTED";
} else {
result = "UNKNOWN";
}
return result;
}
}
Loading

0 comments on commit 1175ba6

Please sign in to comment.