Skip to content

Commit

Permalink
Finished most methods
Browse files Browse the repository at this point in the history
  • Loading branch information
whilu committed Jan 18, 2016
1 parent 8d0c867 commit 1b73124
Show file tree
Hide file tree
Showing 4 changed files with 415 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import android.content.Intent;
import android.content.IntentFilter;

import java.util.List;
import java.util.Set;

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

/**
* Author: lujun(http://blog.lujun.co)
Expand All @@ -20,9 +20,10 @@
public class BluetoothManager implements BaseManager {

private BluetoothAdapter mBluetoothAdapter;
private Context mContext;
private BluetoothListener mBluetoothListener;
private BlueToothReceiver mReceiver;
private Context mContext;
private BluetoothService mBluetoothService;

private static BluetoothManager sBluetoothManager;

Expand All @@ -42,10 +43,13 @@ public static BluetoothManager getInstance(){
/**
* Build this instance.
* @param context
* @return
*/
public void build(Context context){
public BluetoothManager build(Context context){
mContext = context;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothService = new BluetoothService(mBluetoothListener);
return this;
}

/**
Expand Down Expand Up @@ -126,19 +130,18 @@ public Set<BluetoothDevice> getBondedDevices(){
}

@Override
public List<BluetoothDevice> findAllDevices(){
public BluetoothDevice findDeviceByMac(String mac){
if (!isAvaliable() || !isEnabled()){
throw new RuntimeException("Bluetooth is not avaliable!");
}
return null;
return mBluetoothAdapter.getRemoteDevice(mac);
}

@Override
public BluetoothDevice findDeviceByMac(String mac){
if (!isAvaliable() || !isEnabled()){
throw new RuntimeException("Bluetooth is not avaliable!");
public void onStartAsServer() {
if (mBluetoothService != null){
mBluetoothService.start();
}
return mBluetoothAdapter.getRemoteDevice(mac);
}

@Override
Expand All @@ -149,7 +152,15 @@ public void connect(String mac){
if (mBluetoothAdapter.isDiscovering()){
mBluetoothAdapter.cancelDiscovery();
}
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(mac);
// thread connect thread connected thread read writehhstart...
if (mBluetoothService != null){
mBluetoothService.connect(mBluetoothAdapter.getRemoteDevice(mac));
}
}

@Override
public void onWrite(byte[] data) {
if (mBluetoothService != null){
mBluetoothService.write(data);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.bluetooth.BluetoothDevice;

import java.util.List;
import java.util.Set;

/**
Expand Down Expand Up @@ -46,22 +45,27 @@ public interface BaseManager {
*/
Set<BluetoothDevice> getBondedDevices();

/**
* Find all bluetooth devices.
* @return
*/
List<BluetoothDevice> findAllDevices();

/**
* Find a bluetooth device by mac address.
* @param mac
* @return
*/
BluetoothDevice findDeviceByMac(String mac);

/**
* Start as a server.
*/
void onStartAsServer();

/**
* Connected a bluetooth device by mac address.
* @param mac
*/
void connect(String mac);

/**
* Write data to remote device.
* @param data
*/
void onWrite(byte[] data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface BluetoothListener {
void onActionDiscoveryStateChanged(String discoveryState);
void onActionFound(BluetoothDevice device);
void onActionScanModeChanged(int preScanMode, int scanMode);
void onBluetoothServiceStateChanged(int state);
void onReadData(byte[] data);
}
Loading

0 comments on commit 1b73124

Please sign in to comment.