diff --git a/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/BluetoothManager.java b/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/BluetoothManager.java index 9c78d6a..728dbd0 100644 --- a/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/BluetoothManager.java +++ b/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/BluetoothManager.java @@ -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; @@ -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 @@ -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()){ @@ -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 @@ -214,6 +231,13 @@ public void write(byte[] data) { } } +// public void write(T obj){ +// +// if (mBluetoothService != null){ +// +// } +// } + /** * Get UUID. * @return diff --git a/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/base/BaseManager.java b/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/base/BaseManager.java index 8f396f7..a45721d 100644 --- a/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/base/BaseManager.java +++ b/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/base/BaseManager.java @@ -50,8 +50,9 @@ public interface BaseManager { /** * Open bluetooth. + * @return */ - void openBluetooth(); + boolean openBluetooth(); /** * Close bluetooth. @@ -65,6 +66,12 @@ public interface BaseManager { */ boolean setDiscoverable(int time); + /** + * Get current bluetooth state. + * @return + */ + int getBluetoothState(); + /** * Start scan. * @return diff --git a/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/base/BluetoothListener.java b/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/base/BluetoothListener.java index 8211a79..18ac0f7 100644 --- a/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/base/BluetoothListener.java +++ b/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/base/BluetoothListener.java @@ -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); } diff --git a/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/service/BluetoothService.java b/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/service/BluetoothService.java index c2dd97d..3705d3c 100644 --- a/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/service/BluetoothService.java +++ b/lmbluetoothsdk/src/main/java/co/lujun/lmbluetoothsdk/service/BluetoothService.java @@ -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() { @@ -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() @@ -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); diff --git a/sample/src/main/java/co/lujun/sample/MainActivity.java b/sample/src/main/java/co/lujun/sample/MainActivity.java index 062f756..276446b 100644 --- a/sample/src/main/java/co/lujun/sample/MainActivity.java +++ b/sample/src/main/java/co/lujun/sample/MainActivity.java @@ -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; @@ -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; @@ -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 mDevicesList; private List mList; private BaseAdapter mFoundAdapter; + private int mConnectState; private static final String TAG = "LMBluetoothSdk"; @@ -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 @@ -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)); } }); } @@ -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"); } }); @@ -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 @@ -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()); @@ -160,8 +169,11 @@ 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(); } } }); @@ -169,15 +181,57 @@ public void onClick(View v) { @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; + } } diff --git a/sample/src/main/res/layout/content_main.xml b/sample/src/main/res/layout/content_main.xml index d76ab65..96bbf90 100644 --- a/sample/src/main/res/layout/content_main.xml +++ b/sample/src/main/res/layout/content_main.xml @@ -9,6 +9,11 @@ tools:context="co.lujun.sample.MainActivity" tools:showIn="@layout/activity_main"> + +