Skip to content

Commit

Permalink
修改连接方法,增加更多的连接方式,优化连接出现133的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
yinbao committed Jul 20, 2022
1 parent 8e52973 commit 105a7bb
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 64 deletions.
20 changes: 13 additions & 7 deletions app/src/main/java/com/xing/XBle/PhoneCentralActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.xing.XBle;

import android.Manifest;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
Expand All @@ -23,6 +24,12 @@
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.xing.xblelibrary.XBleManager;
import com.xing.xblelibrary.bean.BleBroadcastBean;
import com.xing.xblelibrary.config.XBleStaticConfig;
Expand All @@ -37,12 +44,6 @@
import java.util.List;
import java.util.UUID;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

/**
* xing<br>
* 2021/8/2<br>
Expand Down Expand Up @@ -114,7 +115,12 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
BleBroadcastBean bleBroadcastBean = mListBle.get(position);
mConnectMac = bleBroadcastBean.getMac();
XBleManager.getInstance().stopScan();
XBleManager.getInstance().connectDevice(mConnectMac);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//可以优化连接BLE 4.0出现133的问题
XBleManager.getInstance().connectDevice(mConnectMac, BluetoothDevice.TRANSPORT_LE);
}else {
XBleManager.getInstance().connectDevice(mConnectMac);
}
}
});

Expand Down
4 changes: 2 additions & 2 deletions xblelibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
defaultConfig {
minSdkVersion 19
targetSdkVersion 30
versionCode 13
versionName "2.1.5"
versionCode 14
versionName "2.1.6"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down
37 changes: 32 additions & 5 deletions xblelibrary/src/main/java/com/xing/xblelibrary/XBleManager.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.xing.xblelibrary;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.xing.xblelibrary.bean.AdBleBroadcastBean;
import com.xing.xblelibrary.bean.BleBroadcastBean;
import com.xing.xblelibrary.config.XBleConfig;
Expand All @@ -25,10 +30,6 @@
import java.util.List;
import java.util.UUID;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

/**
* xing<br>
* 2021/7/17<br>
Expand Down Expand Up @@ -190,7 +191,8 @@ private void onServiceSuccess() {
if (autoConnectSystemBle) {
mXBleServer.autoConnectSystemBle();
}
mXBleServer.setAutoMonitorSystemConnectBle(XBleConfig.getInstance().isAutoMonitorSystemConnectBle());
mXBleServer.setAutoMonitorSystemConnectBle(XBleConfig.getInstance()
.isAutoMonitorSystemConnectBle());
}
}

Expand Down Expand Up @@ -243,6 +245,19 @@ public void connectDevice(BleBroadcastBean bleValueBean) {
}


/**
* 连接设备
*
* @param bleValueBean BleValueBean
* @param transport {@link BluetoothDevice#TRANSPORT_AUTO} or {@link BluetoothDevice#TRANSPORT_BREDR} or {@link BluetoothDevice#TRANSPORT_LE}
*/
@RequiresApi(api = Build.VERSION_CODES.M)
public void connectDevice(BleBroadcastBean bleValueBean, int transport) {
if (checkBluetoothServiceStatus()) {
mXBleServer.connectDevice(bleValueBean, transport);
}
}

/**
* 连接设备
*
Expand All @@ -254,6 +269,18 @@ public void connectDevice(String mAddress) {
}
}

/**
* @param mAddress mac地址
* @param transport {@link BluetoothDevice#TRANSPORT_AUTO} or {@link BluetoothDevice#TRANSPORT_BREDR} or {@link BluetoothDevice#TRANSPORT_LE}
*/
@RequiresApi(api = Build.VERSION_CODES.M)
public void connectDevice(String mAddress, int transport) {
if (checkBluetoothServiceStatus()) {
mXBleServer.connectDevice(mAddress, transport);
}
}


/**
* 断开指定mac地址的蓝牙连接,正在连接中的设备也会断开
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
import android.os.Build;
import android.os.ParcelUuid;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.xing.xblelibrary.utils.BleBroadcastUtils;
import com.xing.xblelibrary.utils.XBleL;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

/**
* 蓝牙广播内容bean<br>
* 搜索到的外围设备
Expand Down Expand Up @@ -159,11 +159,7 @@ public boolean equals(@Nullable Object obj) {
private boolean equals(BleBroadcastBean mBleValueBean) {
BluetoothDevice mNewDevice = mBleValueBean.getDevice();
if (mDevice != null) {
if (mDevice == mBleValueBean.getDevice() || equals(mNewDevice.getAddress())) {
return true;
} else {
return false;
}
return mDevice == mBleValueBean.getDevice() || equals(mNewDevice.getAddress());
}
return false;
}
Expand All @@ -177,11 +173,7 @@ private boolean equals(BleBroadcastBean mBleValueBean) {
*/
private boolean equals(String mBleAddress) {
if (mDevice != null) {
if (mDevice.getAddress().equals(mBleAddress)) {
return true;
} else {
return false;
}
return mDevice.getAddress().equals(mBleAddress);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@
import android.os.Looper;
import android.os.Message;

import androidx.annotation.CallSuper;
import androidx.annotation.RequiresApi;

import com.xing.xblelibrary.config.XBleStaticConfig;
import com.xing.xblelibrary.listener.OnBleCharacteristicListener;
import com.xing.xblelibrary.listener.OnBleMtuListener;
import com.xing.xblelibrary.listener.OnBleNotifyDataListener;
import com.xing.xblelibrary.listener.OnBleRssiListener;
import com.xing.xblelibrary.listener.OnBleSendResultListener;
import com.xing.xblelibrary.listener.OnBleCharacteristicListener;
import com.xing.xblelibrary.listener.OnBleNotifyDataListener;
import com.xing.xblelibrary.listener.onBleDisConnectedListener;
import com.xing.xblelibrary.utils.XBleL;
import com.xing.xblelibrary.utils.MyBleDeviceUtils;
import com.xing.xblelibrary.utils.XBleL;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import androidx.annotation.CallSuper;
import androidx.annotation.RequiresApi;

/**
* xing<br>
* 2021/07/21<br>
Expand Down Expand Up @@ -127,7 +127,7 @@ public List<BluetoothGattCharacteristic> getBluetoothGattCharacteristicList(Blue
return bleGattService.getCharacteristics();
}

private void init() {
public void init() {
//TODO 可进行所有模块都要进行的初始化操作

}
Expand Down Expand Up @@ -388,7 +388,7 @@ public void descriptorWriteOk(BluetoothGattDescriptor descriptor) {
mOnCharacteristicListener.onDescriptorWriteOK(descriptor);
}
}
if (mLinkedListNotify != null && mLinkedListNotify.size() > 0) {
if (mLinkedListNotify.size() > 0) {
mLinkedListNotify.removeLast();
if (mLinkedListNotify.size() > 0) {
SendDataBean sendDataBean = mLinkedListNotify.getLast();
Expand Down
Loading

0 comments on commit 105a7bb

Please sign in to comment.