Skip to content

Commit

Permalink
Small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
whilu committed Jan 25, 2016
1 parent 511a469 commit 1afea1b
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,19 @@
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;

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

import co.lujun.lmbluetoothsdk.base.BaseController;
Expand All @@ -48,10 +58,17 @@
public class BluetoothLEController implements BaseController {

private BluetoothAdapter mBluetoothAdapter;
private BluetoothLeScanner mLEScanner;
private BluetoothListener mBluetoothListener;
private BlueToothReceiver mReceiver;
private BluetoothLEService mBluetoothLEService;
private ScanSettings mLeSettings;
private List<ScanFilter> mLeFilters;

private Context mContext;
private Handler mHandler;

private int mScanTime = 120000;

private static BluetoothLEController sBluetoothLEController;

Expand All @@ -77,6 +94,7 @@ public static BluetoothLEController getInstance(){
*/
public BluetoothLEController build(Context context){
mContext = context;
mHandler = new Handler();
final BluetoothManager bluetoothManager =
(BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
Expand Down Expand Up @@ -144,40 +162,92 @@ public void closeBluetooth() {
mBluetoothAdapter.disable();
}

/**
* Check to determine whether BLE is supported on the device
* @return
*/
public boolean isSupportBLE(){
return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
}

// Set Android device as a server is not support, you shouldn't call this method in BLE.
@Override
public boolean setDiscoverable(int time) {
return false;
}

@Override
public int getBluetoothState() {
return 0;
if (!isAvailable()){
return BluetoothAdapter.STATE_OFF;
}
return mBluetoothAdapter.getState();
}

@Override
public boolean startScan() {
return false;
if (!isAvailable() && !isEnabled()){
return false;
}
if (Build.VERSION.SDK_INT >= 21){
mLEScanner = mBluetoothAdapter.getBluetoothLeScanner();
mLeSettings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.build();
mLeFilters = new ArrayList<ScanFilter>();
}
scanLeDevice();
return true;
}

private void scanLeDevice(){
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
cancelScan();
}
}, mScanTime);
if (Build.VERSION.SDK_INT < 21) {
// 搜索指定UUID的外设, startLeScan(UUID[], BluetoothAdapter.LeScanCallback)
mBluetoothAdapter.startLeScan(mLeScanCallback);
}else {
mCbtScanCallback = new CBTScanCallback();
mLEScanner.startScan(mLeFilters, mLeSettings, mCbtScanCallback);
}
}

@Override
public boolean cancelScan() {
return false;
if (!isAvailable() && !isEnabled()){
return false;
}
if (Build.VERSION.SDK_INT < 21) {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}else {
mLEScanner.stopScan(mCbtScanCallback);
}
return true;
}

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

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

// Set Android device as a server is not support.
@Override
public void startAsServer() {

}
public void startAsServer() {}

@Override
public void connect(String mac) {
Expand All @@ -199,8 +269,60 @@ public void write(byte[] data) {

}

/**
* Set scan time(unit millisecond)
* @param time
*/
public void setScanTime(int time){
mScanTime = time;
}

/**
* Get scan time.
* @return
*/
public int getScanTime() {
return mScanTime;
}

@Override
public BluetoothDevice getConnectedDevice() {
return null;
}



private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
if (mBluetoothListener != null) {
mBluetoothListener.onActionDeviceFound(device);
}
}

};

private CBTScanCallback mCbtScanCallback = new CBTScanCallback();

private class CBTScanCallback extends ScanCallback {

@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
if (mBluetoothListener != null) {
mBluetoothListener.onActionDeviceFound(result.getDevice());
}
}

@Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
}

@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package co.lujun.lmbluetoothsdk.base;

/**
* Author: lujun(http://blog.lujun.co)
* Date: 2016-1-25 12:19
*/
public class Bluetooth {
}
74 changes: 44 additions & 30 deletions sample/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,57 @@
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="90dp"
android:orientation="horizontal">
android:orientation="vertical">

<Button
android:id="@+id/btn_open_bt"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:singleLine="true"
android:text="开启蓝牙" />
android:orientation="horizontal">

<Button
android:id="@+id/btn_scan_avaliable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:singleLine="true"
android:text="可被检测" />
<Button
android:id="@+id/btn_open_bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:singleLine="true"
android:text="Open Bluetooth" />

<Button
android:id="@+id/btn_scan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:singleLine="true"
android:text="扫描" />
<Button
android:id="@+id/btn_scan_avaliable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:singleLine="true"
android:text="Set Discoverable" />

<Button
android:id="@+id/btn_start_as_server"
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:singleLine="true"
android:text="打开服务端" />
android:orientation="horizontal">

<Button
android:id="@+id/btn_scan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:singleLine="true"
android:text="Scan Device" />

<Button
android:id="@+id/btn_start_as_server"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:singleLine="true"
android:text="Start As Server" />

</LinearLayout>
</LinearLayout>

<TextView
Expand All @@ -63,7 +77,7 @@
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="150dp"
android:layout_marginBottom="210dp"
android:layout_weight="1"
android:background="#33ff0000"
android:gravity="center"
Expand Down

0 comments on commit 1afea1b

Please sign in to comment.