Skip to content

Commit

Permalink
Adaptation 5x
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasonchenlijian committed Dec 3, 2017
1 parent 83e9ffe commit 3be0528
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 164 deletions.
28 changes: 28 additions & 0 deletions FastBleLib/src/main/java/com/clj/fastble/BleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import android.annotation.TargetApi;
import android.app.Application;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.ScanRecord;
import android.bluetooth.le.ScanResult;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
Expand Down Expand Up @@ -467,10 +470,35 @@ public void disableBluetooth() {
}
}

/**
* judge Bluetooth is enable
*
* @return
*/
public boolean isBlueEnable() {
return bluetoothAdapter != null && bluetoothAdapter.isEnabled();
}


public BleDevice convertBleDevice(BluetoothDevice bluetoothDevice, int rssi, byte[] scanRecord, long timestampNanos) {
return new BleDevice(bluetoothDevice, rssi, scanRecord, timestampNanos);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public BleDevice convertBleDevice(ScanResult scanResult) {
if (scanResult == null) {
throw new IllegalArgumentException("scanResult can not be Null!");
}
BluetoothDevice bluetoothDevice = scanResult.getDevice();
int rssi = scanResult.getRssi();
ScanRecord scanRecord = scanResult.getScanRecord();
byte[] bytes = null;
if (scanRecord != null)
bytes = scanRecord.getBytes();
long timestampNanos = scanResult.getTimestampNanos();
return new BleDevice(bluetoothDevice, rssi, bytes, timestampNanos);
}

public BleBluetooth getBleBluetooth(BleDevice bleDevice) {
if (multipleBluetoothController != null) {
return multipleBluetoothController.getBleBluetooth(bleDevice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public abstract class BleScanPresenterJellyBean implements BluetoothAdapter.LeScanCallback {
public abstract class BleScanPresenter implements BluetoothAdapter.LeScanCallback {

private String[] mDeviceNames = null;
private String mDeviceMac = null;
Expand All @@ -28,7 +28,7 @@ public abstract class BleScanPresenterJellyBean implements BluetoothAdapter.LeSc
private Handler mHandler = new Handler(Looper.getMainLooper());
private long mScanTimeout = BleManager.getInstance().getScanTimeout();

public BleScanPresenterJellyBean(String[] names, String mac, boolean fuzzy, boolean needConnect, long timeOut) {
public BleScanPresenter(String[] names, String mac, boolean fuzzy, boolean needConnect, long timeOut) {
this.mDeviceNames = names;
this.mDeviceMac = mac;
this.mFuzzy = fuzzy;
Expand Down

This file was deleted.

50 changes: 14 additions & 36 deletions FastBleLib/src/main/java/com/clj/fastble/scan/BleScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@


import android.annotation.TargetApi;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanSettings;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
Expand All @@ -16,7 +12,6 @@
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.data.BleScanState;

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

Expand All @@ -31,13 +26,12 @@ private static class BleScannerHolder {
private static final BleScanner sBleScanner = new BleScanner();
}

private BleScanPresenterJellyBean bleScanPresenterJellyBean;
private BleScanPresenter bleScanPresenter;
private BleScanState scanState = BleScanState.STATE_IDLE;

public void scan(UUID[] serviceUuids, String[] names, String mac, boolean fuzzy,
long timeOut, final BleScanCallback callback) {

startLeScan(serviceUuids, new BleScanPresenterJellyBean(names, mac, fuzzy, false, timeOut) {
startLeScan(serviceUuids, new BleScanPresenter(names, mac, fuzzy, false, timeOut) {
@Override
public void onScanStarted(boolean success) {
if (callback != null) {
Expand All @@ -59,30 +53,29 @@ public void onScanFinished(List<BleDevice> scanResultList) {
}
}
});

}

private synchronized void startLeScan(UUID[] serviceUuids, BleScanPresenterJellyBean callback) {
if (callback == null)
private synchronized void startLeScan(UUID[] serviceUuids, BleScanPresenter presenter) {
if (presenter == null)
return;
this.bleScanPresenterJellyBean = callback;
boolean success = BleManager.getInstance().getBluetoothAdapter().startLeScan(serviceUuids, bleScanPresenterJellyBean);
this.bleScanPresenter = presenter;
boolean success = BleManager.getInstance().getBluetoothAdapter().startLeScan(serviceUuids, bleScanPresenter);
if (success) {
scanState = BleScanState.STATE_SCANNING;
bleScanPresenterJellyBean.notifyScanStarted(true);
bleScanPresenter.notifyScanStarted(true);
} else {
bleScanPresenterJellyBean.notifyScanStarted(false);
callback.removeHandlerMsg();
bleScanPresenter.notifyScanStarted(false);
presenter.removeHandlerMsg();
}
}

public synchronized void stopLeScan() {
if (bleScanPresenterJellyBean == null)
if (bleScanPresenter == null)
return;

BleManager.getInstance().getBluetoothAdapter().stopLeScan(bleScanPresenterJellyBean);
bleScanPresenterJellyBean.notifyScanStopped();
bleScanPresenterJellyBean = null;
BleManager.getInstance().getBluetoothAdapter().stopLeScan(bleScanPresenter);
bleScanPresenter.notifyScanStopped();
bleScanPresenter = null;

if (scanState == BleScanState.STATE_SCANNING) {
scanState = BleScanState.STATE_IDLE;
Expand All @@ -92,7 +85,7 @@ public synchronized void stopLeScan() {
public void scanAndConnect(UUID[] serviceUuids, String[] names, final String mac, boolean fuzzy,
long timeOut, final BleScanAndConnectCallback callback) {

startLeScan(serviceUuids, new BleScanPresenterJellyBean(names, mac, fuzzy, true, timeOut) {
startLeScan(serviceUuids, new BleScanPresenter(names, mac, fuzzy, true, timeOut) {

@Override
public void onScanStarted(boolean success) {
Expand Down Expand Up @@ -126,19 +119,4 @@ public void run() {
}
});
}


@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void scanOnLollipop() {
BluetoothLeScanner bluetoothLeScanner = BleManager.getInstance().getBluetoothAdapter().getBluetoothLeScanner();
ScanFilter scanFilter = new ScanFilter.Builder().build();
List<ScanFilter> scanFilterList = new ArrayList<>();
scanFilterList.add(scanFilter);
ScanSettings scanSettings = new ScanSettings.Builder().build();
bluetoothLeScanner.startScan(scanFilterList, scanSettings, new ScanCallback() {});
}




}

This file was deleted.

0 comments on commit 3be0528

Please sign in to comment.