Skip to content

Commit

Permalink
新增扫描指定设备规则判断接口
Browse files Browse the repository at this point in the history
  • Loading branch information
r17171709 committed Nov 20, 2018
1 parent cc94a48 commit aa5ecaa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.renyu.blelibrary.impl;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.ScanResult;

/**
* Created by Administrator on 2018/11/20.
*/
public interface IScanAndConnRule {
boolean rule21(ScanResult result);
boolean rule(BluetoothDevice device);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.renyu.blelibrary.impl.BLEScanCallBack;
import com.renyu.blelibrary.impl.BLEStateChangeListener;
import com.renyu.blelibrary.impl.BLEWriteResponseListener;
import com.renyu.blelibrary.impl.IScanAndConnRule;

import java.lang.reflect.Method;
import java.util.HashMap;
Expand Down Expand Up @@ -345,16 +346,16 @@ public void run() {

/**
* 扫描完成直接连接
* @param deviceName
* @param iScanAndConnRule
*/
public synchronized void startScanAndConn(final String deviceName) {
public synchronized void startScanAndConn(final IScanAndConnRule iScanAndConnRule) {
// BLE扫描回调
if (Build.VERSION_CODES.LOLLIPOP <= Build.VERSION.SDK_INT) {
bleScan21CallBack=new BLEScan21CallBack() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
if (result !=null && result.getDevice()!=null && !TextUtils.isEmpty(result.getDevice().getName()) && result.getDevice().getName().equals(deviceName)) {
if (result !=null && result.getDevice()!=null && iScanAndConnRule.rule21(result)) {
BLEDevice device1=new BLEDevice();
device1.setRssi(result.getRssi());
device1.setDevice(result.getDevice());
Expand All @@ -379,7 +380,7 @@ public void run() {
leScanCallback=new BLEScanCallBack() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
if (device!=null && !TextUtils.isEmpty(device.getName()) && deviceName.equals(device.getName())) {
if (device!=null && !TextUtils.isEmpty(device.getName()) && iScanAndConnRule.rule(device)) {
BLEDevice device1=new BLEDevice();
device1.setRssi(rssi);
device1.setDevice(device);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import android.app.Service;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.ScanResult;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.Log;

Expand All @@ -19,6 +22,7 @@
import com.renyu.blelibrary.impl.BLEReadResponseListener;
import com.renyu.blelibrary.impl.BLEStateChangeListener;
import com.renyu.blelibrary.impl.BLEWriteResponseListener;
import com.renyu.blelibrary.impl.IScanAndConnRule;
import com.renyu.blelibrary.utils.BLEFramework;

import org.greenrobot.eventbus.EventBus;
Expand Down Expand Up @@ -115,7 +119,19 @@ public int onStartCommand(Intent intent, int flags, int startId) {
}
// 家iite-j6J7Nj 公司iite-N3Uf2e
if (intent.getStringExtra(Params.COMMAND).equals(Params.SCANCONN)) {
bleFramework.startScanAndConn(intent.getStringExtra(Params.DEVICE));
final String deviceName = intent.getStringExtra(Params.DEVICE);
bleFramework.startScanAndConn(new IScanAndConnRule() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean rule21(ScanResult result) {
return !TextUtils.isEmpty(result.getDevice().getName()) && result.getDevice().getName().equals(deviceName);
}

@Override
public boolean rule(BluetoothDevice device) {
return deviceName.equals(device.getName());
}
});
}
if (intent.getStringExtra(Params.COMMAND).equals(Params.WRITE)) {
bleFramework.addWriteCommand(Params.UUID_SERVICE_MILI, Params.UUID_SERVICE_WRITE, intent.getByteArrayExtra(Params.BYTECODE));
Expand Down

0 comments on commit aa5ecaa

Please sign in to comment.