Skip to content

Commit

Permalink
Update Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
whilu committed Jan 15, 2016
1 parent ecaaae5 commit 8cef27a
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 7 deletions.
3 changes: 3 additions & 0 deletions lmbluetoothsdk/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.lujun.lmbluetoothsdk">

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<application android:label="@string/app_name">

</application>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
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;

/**
* Author: lujun(http://blog.lujun.co)
* Date: 2016/1/14 10:59
* Date: 2016-1-14 10:59
*/
public class BluetoothManager {
public class BluetoothManager implements BaseManager {

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

private static BluetoothManager sBluetoothManager;

private static final String TAG = "BluetoothManager";
Expand All @@ -27,27 +39,116 @@ public static BluetoothManager getInstance(){
return sBluetoothManager;
}

/**
* Build this instance.
* @param context
*/
public void build(Context context){
mContext = context;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}

/**
* Set bluetooth listener, you can check all bluetooth status with this listener's callback.
* @param listener
*/
public void setBluetoothListener(BluetoothListener listener){
this.mBluetoothListener = listener;
registerReceiver();
}

private void registerReceiver(){
if (mBluetoothListener == null || mContext == null){
return;
}

IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);

mReceiver = new BlueToothReceiver(mBluetoothListener);
mContext.registerReceiver(mReceiver, filter);
}

@Override
public boolean isAvaliable(){
return mBluetoothAdapter != null;
}

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

public void openBluetooth(){
@Override
public void onOpenBluetooth(){
if (!isAvaliable()){
return;
}
// Intent intent = new Intent();
// intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
// startActivityForResult(intent, REQUEST_ENABLE_BT);
mBluetoothAdapter.enable();
}

@Override
public void onCloseBluetooth(){
if (!isAvaliable() && !isEnabled()){
return;
}
mBluetoothAdapter.disable();
}

public boolean setDiscoverable(long time){
@Override
public boolean setDiscoverable(int time){
if (!isAvaliable() && !isEnabled()){
return false;
}
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, time);
mContext.startActivity(intent);
return true;
}

return false;
@Override
public Set<BluetoothDevice> getBondedDevices(){
if (!isAvaliable() || !isEnabled()){
throw new RuntimeException("Bluetooth is not avaliable!");
}
return mBluetoothAdapter.getBondedDevices();
}

@Override
public List<BluetoothDevice> findAllDevices(){

if (!isAvaliable() || !isEnabled()){
throw new RuntimeException("Bluetooth is not avaliable!");
}
return null;
}

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

return null;
@Override
public void connect(String mac){
if (!isAvaliable() || !isEnabled()){
throw new RuntimeException("Bluetooth is not avaliable!");
}
if (mBluetoothAdapter.isDiscovering()){
mBluetoothAdapter.cancelDiscovery();
}
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(mac);
// thread start...connect thread connected thread read write
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package co.lujun.lmbluetoothsdk.receiver;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import co.lujun.lmbluetoothsdk.base.BluetoothListener;

/**
* Author: lujun(http://blog.lujun.co)
* Date: 2016-1-15 10:52
*/
public class BlueToothReceiver extends BroadcastReceiver {

private BluetoothListener mBluetoothListener;

public BlueToothReceiver(BluetoothListener listener){
mBluetoothListener = listener;
}

@Override
public void onReceive(Context context, Intent intent) {
if (intent == null || mBluetoothListener == null){
return;
}
String action = intent.getAction();
switch (action){
case BluetoothAdapter.ACTION_STATE_CHANGED:
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
int preState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, 0);
mBluetoothListener.onActionStateChanged(preState, state);
break;

case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
mBluetoothListener.onActionDiscoveryStateChanged(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
break;

case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
mBluetoothListener.onActionDiscoveryStateChanged(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
break;

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

case BluetoothAdapter.ACTION_SCAN_MODE_CHANGED:
int scanMode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE, 0);
int preScanMode = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_SCAN_MODE, 0);
mBluetoothListener.onActionScanModeChanged(preScanMode, scanMode);
break;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package co.lujun.lmbluetoothsdk.service;

/**
* Author: lujun(http://blog.lujun.co)
* Date: 2016-1-15 15:30
*/
public class BluetoothService {
}

0 comments on commit 8cef27a

Please sign in to comment.