Skip to content

Commit

Permalink
Open all methods
Browse files Browse the repository at this point in the history
  • Loading branch information
whilu committed Jan 18, 2016
1 parent 1b73124 commit a19421e
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
/*
* The MIT License (MIT)
* Copyright (c) 2015 LinkMob.cc
* Contributors: lujun
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package co.lujun.lmbluetoothsdk;

import android.bluetooth.BluetoothAdapter;
Expand Down Expand Up @@ -48,7 +74,7 @@ public static BluetoothManager getInstance(){
public BluetoothManager build(Context context){
mContext = context;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothService = new BluetoothService(mBluetoothListener);
mBluetoothService = new BluetoothService();
return this;
}

Expand All @@ -59,6 +85,9 @@ public BluetoothManager build(Context context){
public void setBluetoothListener(BluetoothListener listener){
this.mBluetoothListener = listener;
registerReceiver();
if (mBluetoothService != null) {
mBluetoothService.setBluetoothListener(mBluetoothListener);
}
}

private void registerReceiver(){
Expand Down Expand Up @@ -93,7 +122,6 @@ public boolean isEnabled(){
@Override
public void onOpenBluetooth(){
if (!isAvaliable()){

return;
}
// Intent intent = new Intent();
Expand Down Expand Up @@ -121,6 +149,22 @@ public boolean setDiscoverable(int time){
return true;
}

@Override
public boolean startDiscovery() {
if (!isAvaliable() && !isEnabled()){
return false;
}
return mBluetoothAdapter.startDiscovery();
}

@Override
public boolean cancelDiscovery() {
if (!isAvaliable() && !isEnabled()){
return false;
}
return mBluetoothAdapter.cancelDiscovery();
}

@Override
public Set<BluetoothDevice> getBondedDevices(){
if (!isAvaliable() || !isEnabled()){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ public interface BaseManager {
*/
boolean setDiscoverable(int time);

/**
* Start discovery.
* @return
*/
boolean startDiscovery();

/**
* Cancel discovery.
* @return
*/
boolean cancelDiscovery();

/**
* Get paired devices.
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.util.Log;

import java.io.IOException;
Expand All @@ -20,29 +19,26 @@
*/
public class BluetoothService {

// Debugging
private static final String TAG = "BluetoothService";
private static final String TAG = "LMBluetoothSdk";

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

// Member fields
private final BluetoothAdapter mAdapter;
private AcceptThread mAcceptThread;
private ConnectThread mConnectThread;
private ConnectedThread mConnectedThread;

private BluetoothListener mBluetoothListener;

private int mState;

// Constants that indicate the current connection state
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

public BluetoothService(BluetoothListener listener) {
public BluetoothService() {
mAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothListener = listener;
mState = STATE_NONE;
}

Expand All @@ -51,15 +47,20 @@ public BluetoothService(BluetoothListener listener) {
* @param state An integer defining the current connection state
*/
private synchronized void setState(int state) {
Log.d(TAG, "setState() " + mState + " -> " + state);
mState = state;
if (mBluetoothListener != null){
mBluetoothListener.onBluetoothServiceStateChanged(state);
}
}

public synchronized void setBluetoothListener(BluetoothListener listener) {
this.mBluetoothListener = listener;
}

/**
* Return the current connection state. */
* Return the current connection state.
* @return
*/
public synchronized int getState() {
return mState;
}
Expand All @@ -71,7 +72,7 @@ public synchronized int getState() {
public synchronized void start() {
Log.d(TAG, "start");
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread.cancel();
mConnectThread = null;
}
if (mConnectedThread != null) {
Expand Down
1 change: 1 addition & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':lmbluetoothsdk')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
Expand Down
3 changes: 2 additions & 1 deletion sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
135 changes: 135 additions & 0 deletions sample/src/main/java/co/lujun/sample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,156 @@
package co.lujun.sample;

import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

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

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

public class MainActivity extends AppCompatActivity {

private BluetoothManager mBluetoothManager;

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

private List<String> mDevicesList;
private BaseAdapter mFoundAdapter;

private static final String TAG = "LMBluetoothSdk";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

init();
}

private void initBT(){
mBluetoothManager = BluetoothManager.getInstance().build(this);
mBluetoothManager.setBluetoothListener(new BluetoothListener() {
@Override
public void onActionStateChanged(int preState, int state) {
Log.d(TAG, "preState:" + preState + ", state:" + state);
}

@Override
public void onActionDiscoveryStateChanged(String discoveryState) {
Log.d(TAG, "discoveryState:" + discoveryState);
}

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

@Override
public void onActionScanModeChanged(int preScanMode, int scanMode) {
Log.d(TAG, "preScanMode:" + preScanMode + ", scanMode:" + scanMode);
}

@Override
public void onBluetoothServiceStateChanged(int state) {
Log.d(TAG, "Service State:" + state);
}

@Override
public void onReadData(final byte[] data) {
runOnUiThread(new Runnable() {
@Override
public void run() {
tvContent.append(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();

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);
etSend = (EditText) findViewById(R.id.et_send_content);
lvDevices = (ListView) findViewById(R.id.lv_devices);

lvDevices.setAdapter(mFoundAdapter);

btnScanAvaliabe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBluetoothManager.setDiscoverable(600);
}
});

btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBluetoothManager.startDiscovery();
}
});
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = etSend.getText().toString();
if (TextUtils.isEmpty(msg)){
return;
}
mBluetoothManager.onWrite(msg.getBytes());
tvContent.append(msg + "\n");
}
});
btnOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mBluetoothManager.isEnabled()){
mBluetoothManager.onOpenBluetooth();
}
}
});
btnStartServer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBluetoothManager.onStartAsServer();
}
});
lvDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemStr = mDevicesList.get(position);
mBluetoothManager.connect(itemStr.substring(itemStr.length() - 17));
}
});
}
}
Loading

0 comments on commit a19421e

Please sign in to comment.