Skip to content

Commit

Permalink
BLE service
Browse files Browse the repository at this point in the history
  • Loading branch information
whilu committed Jan 25, 2016
1 parent 1afea1b commit 89590e0
Show file tree
Hide file tree
Showing 6 changed files with 371 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.Set;

import co.lujun.lmbluetoothsdk.base.BaseController;
import co.lujun.lmbluetoothsdk.base.BluetoothLEListener;
import co.lujun.lmbluetoothsdk.base.BluetoothListener;
import co.lujun.lmbluetoothsdk.receiver.BlueToothReceiver;
import co.lujun.lmbluetoothsdk.service.BluetoothLEService;
Expand All @@ -59,7 +60,7 @@ public class BluetoothLEController implements BaseController {

private BluetoothAdapter mBluetoothAdapter;
private BluetoothLeScanner mLEScanner;
private BluetoothListener mBluetoothListener;
private BluetoothLEListener mBluetoothLEListener;
private BlueToothReceiver mReceiver;
private BluetoothLEService mBluetoothLEService;
private ScanSettings mLeSettings;
Expand Down Expand Up @@ -106,19 +107,19 @@ public BluetoothLEController build(Context context){
* Set bluetooth listener, you can check all bluetooth status and read data with this listener's callback.
* @param listener a BluetoothListener
*/
public void setBluetoothListener(BluetoothListener listener){
this.mBluetoothListener = listener;
public void setBluetoothListener(BluetoothLEListener listener){
this.mBluetoothLEListener = listener;
registerReceiver();
if (mBluetoothLEService != null) {
// mBluetoothLEService.setBluetoothListener(mBluetoothListener);
mBluetoothLEService.setBluetoothLEListener(mBluetoothLEListener);
}
}

/**
* Register broadcast receiver for current context.
*/
private void registerReceiver(){
if (mBluetoothListener == null || mContext == null){
if (mBluetoothLEListener == null || mContext == null){
return;
}

Expand All @@ -129,7 +130,7 @@ private void registerReceiver(){
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);

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

Expand Down Expand Up @@ -251,17 +252,23 @@ public void startAsServer() {}

@Override
public void connect(String mac) {

if (mBluetoothLEService != null) {
mBluetoothLEService.connect(mContext, mBluetoothAdapter.getRemoteDevice(mac));
}
}

@Override
public void reConnect(String mac) {

if (mBluetoothLEService != null) {
mBluetoothLEService.reConnect();
}
}

@Override
public void disconnect() {

if (mBluetoothLEService != null) {
mBluetoothLEService.disConnect();
}
}

@Override
Expand Down Expand Up @@ -296,8 +303,8 @@ public BluetoothDevice getConnectedDevice() {
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
if (mBluetoothListener != null) {
mBluetoothListener.onActionDeviceFound(device);
if (mBluetoothLEListener != null) {
mBluetoothLEListener.onActionDeviceFound(device);
}
}

Expand All @@ -310,8 +317,8 @@ private class CBTScanCallback extends ScanCallback {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
if (mBluetoothListener != null) {
mBluetoothListener.onActionDeviceFound(result.getDevice());
if (mBluetoothLEListener != null) {
mBluetoothLEListener.onActionDeviceFound(result.getDevice());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.base;

import android.bluetooth.BluetoothDevice;

/**
* Author: lujun(http://blog.lujun.co)
* Date: 2016-1-15 10:53
*/
public interface BaseListener {

/**
* Callback when bluetooth power state changed.
*
* @param preState previous power state
* @param state current power state
* Possible values are STATE_OFF, STATE_TURNING_ON,
* STATE_ON, STATE_TURNING_OFF in {@link android.bluetooth.BluetoothAdapter} class.
*/
void onActionStateChanged(int preState, int state);

/**
* Callback when local Bluetooth adapter discovery process state changed.
* @param discoveryState the state of local Bluetooth adapter discovery process.
* Possible values are ACTION_DISCOVERY_STARTED,
* ACTION_DISCOVERY_FINISHED in {@link android.bluetooth.BluetoothAdapter} class.
*
*/
void onActionDiscoveryStateChanged(String discoveryState);

/**
* Callback when the current scan mode changed.
* @param preScanMode previous scan mode
* @param scanMode current scan mode
* Possible values are SCAN_MODE_NONE, SCAN_MODE_CONNECTABLE,
* SCAN_MODE_CONNECTABLE_DISCOVERABLE in {@link android.bluetooth.BluetoothAdapter} class.
*/
void onActionScanModeChanged(int preScanMode, int scanMode);

/**
* Callback when the connection state changed.
* @param state connection state
* Possible values are STATE_NONE, STATE_LISTEN, STATE_CONNECTING, STATE_CONNECTED,
* STATE_DISCONNECTED and STATE_UNKNOWN in {@link State} class.
*/
void onBluetoothServiceStateChanged(int state);

/**
* Callback when found device.
* @param device a remote device
*/
void onActionDeviceFound(BluetoothDevice device);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.base;

import android.bluetooth.BluetoothGattCharacteristic;

/**
* Author: lujun(http://blog.lujun.co)
* Date: 2016-1-15 10:53
*/
public interface BluetoothLEListener extends BaseListener {

/**
* Read data.
* @param characteristic
*/
void onReadData(BluetoothGattCharacteristic characteristic);

/**
* When write data to remote BLE device, the notification will send to here.
* @param characteristic
*/
void onWriteData(BluetoothGattCharacteristic characteristic);

/**
* When data changed, the notification will send to here.
* @param characteristic
*/
void onDataChanged(BluetoothGattCharacteristic characteristic);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,49 +32,7 @@
* Author: lujun(http://blog.lujun.co)
* Date: 2016-1-15 10:53
*/
public interface BluetoothListener {

/**
* Callback when bluetooth power state changed.
*
* @param preState previous power state
* @param state current power state
* Possible values are STATE_OFF, STATE_TURNING_ON,
* STATE_ON, STATE_TURNING_OFF in {@link android.bluetooth.BluetoothAdapter} class.
*/
void onActionStateChanged(int preState, int state);

/**
* Callback when local Bluetooth adapter discovery process state changed.
* @param discoveryState the state of local Bluetooth adapter discovery process.
* Possible values are ACTION_DISCOVERY_STARTED,
* ACTION_DISCOVERY_FINISHED in {@link android.bluetooth.BluetoothAdapter} class.
*
*/
void onActionDiscoveryStateChanged(String discoveryState);

/**
* Callback when the current scan mode changed.
* @param preScanMode previous scan mode
* @param scanMode current scan mode
* Possible values are SCAN_MODE_NONE, SCAN_MODE_CONNECTABLE,
* SCAN_MODE_CONNECTABLE_DISCOVERABLE in {@link android.bluetooth.BluetoothAdapter} class.
*/
void onActionScanModeChanged(int preScanMode, int scanMode);

/**
* Callback when the connection state changed.
* @param state connection state
* Possible values are STATE_NONE, STATE_LISTEN, STATE_CONNECTING, STATE_CONNECTED,
* STATE_DISCONNECTED and STATE_UNKNOWN in {@link co.lujun.lmbluetoothsdk.base.State} class.
*/
void onBluetoothServiceStateChanged(int state);

/**
* Callback when found device.
* @param device a remote device
*/
void onActionDeviceFound(BluetoothDevice device);
public interface BluetoothListener extends BaseListener {

/**
* Callback when remote device send data to current device.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@
import android.content.Context;
import android.content.Intent;

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

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

private BluetoothListener mBluetoothListener;
private BaseListener mBluetoothListener;

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

Expand Down
Loading

0 comments on commit 89590e0

Please sign in to comment.