Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增requestConnectionPriority接口 #158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ public void requestMtu(String mac, int mtu, BleMtuResponse response) {
mClient.requestMtu(mac, mtu, response);
}

@Override
public void requestConnectionPriority(String mac, int connectionPriority) {
BluetoothLog.v(String.format("requestConnectionPriority %s", mac));
mClient.requestConnectionPriority(mac, connectionPriority);
}

@Override
public void search(SearchRequest request, SearchResponse response) {
BluetoothLog.v(String.format("search %s", request));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import static com.inuker.bluetooth.library.Constants.CODE_READ_DESCRIPTOR;
import static com.inuker.bluetooth.library.Constants.CODE_READ_RSSI;
import static com.inuker.bluetooth.library.Constants.CODE_REFRESH_CACHE;
import static com.inuker.bluetooth.library.Constants.CODE_REQUEST_PRIORITY;
import static com.inuker.bluetooth.library.Constants.CODE_SEARCH;
import static com.inuker.bluetooth.library.Constants.CODE_STOP_SESARCH;
import static com.inuker.bluetooth.library.Constants.CODE_UNNOTIFY;
Expand All @@ -70,6 +71,7 @@
import static com.inuker.bluetooth.library.Constants.EXTRA_MAC;
import static com.inuker.bluetooth.library.Constants.EXTRA_MTU;
import static com.inuker.bluetooth.library.Constants.EXTRA_OPTIONS;
import static com.inuker.bluetooth.library.Constants.EXTRA_PRIORITY;
import static com.inuker.bluetooth.library.Constants.EXTRA_REQUEST;
import static com.inuker.bluetooth.library.Constants.EXTRA_RSSI;
import static com.inuker.bluetooth.library.Constants.EXTRA_SEARCH_RESULT;
Expand Down Expand Up @@ -418,7 +420,7 @@ protected void onAsyncResponse(int code, Bundle data) {

@Override
public void unindicate(String mac, UUID service, UUID character, BleUnnotifyResponse response) {
unnotify(mac, service, character, response);
unnotify(mac, service, character, response);
}

@Override
Expand Down Expand Up @@ -452,6 +454,14 @@ protected void onAsyncResponse(int code, Bundle data) {
});
}

@Override
public void requestConnectionPriority(String mac, int connectionPriority) {
Bundle args = new Bundle();
args.putString(EXTRA_MAC, mac);
args.putInt(EXTRA_PRIORITY, connectionPriority);
safeCallBluetoothApi(CODE_REQUEST_PRIORITY, args, null);
}

@Override
public void search(SearchRequest request, final SearchResponse response) {
Bundle args = new Bundle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static com.inuker.bluetooth.library.Constants.CODE_READ_RSSI;
import static com.inuker.bluetooth.library.Constants.CODE_REFRESH_CACHE;
import static com.inuker.bluetooth.library.Constants.CODE_REQUEST_MTU;
import static com.inuker.bluetooth.library.Constants.CODE_REQUEST_PRIORITY;
import static com.inuker.bluetooth.library.Constants.CODE_SEARCH;
import static com.inuker.bluetooth.library.Constants.CODE_STOP_SESARCH;
import static com.inuker.bluetooth.library.Constants.CODE_UNNOTIFY;
Expand All @@ -37,6 +38,7 @@
import static com.inuker.bluetooth.library.Constants.EXTRA_MAC;
import static com.inuker.bluetooth.library.Constants.EXTRA_MTU;
import static com.inuker.bluetooth.library.Constants.EXTRA_OPTIONS;
import static com.inuker.bluetooth.library.Constants.EXTRA_PRIORITY;
import static com.inuker.bluetooth.library.Constants.EXTRA_REQUEST;
import static com.inuker.bluetooth.library.Constants.EXTRA_SERVICE_UUID;
import static com.inuker.bluetooth.library.Constants.EXTRA_TYPE;
Expand Down Expand Up @@ -167,6 +169,10 @@ public boolean handleMessage(Message msg) {
case CODE_REFRESH_CACHE:
BleConnectManager.refreshCache(mac);
break;
case CODE_REQUEST_PRIORITY:
int priority = args.getInt(EXTRA_PRIORITY);
BleConnectManager.requestConnectionPriority(mac, priority);
break;
}
return true;
}
Expand Down
25 changes: 19 additions & 6 deletions library/src/main/java/com/inuker/bluetooth/library/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class Constants {
public static final String EXTRA_OPTIONS = "extra.options";
public static final String EXTRA_TYPE = "extra.type";
public static final String EXTRA_MTU = "extra.mtu";
public static final String EXTRA_PRIORITY = "extra.priority";

/**
* CallBluetoothApi response code
Expand Down Expand Up @@ -79,6 +80,7 @@ public class Constants {
public static final int CODE_CLEAR_REQUEST = 20;
public static final int CODE_REFRESH_CACHE = 21;
public static final int CODE_REQUEST_MTU = 22;
public static final int CODE_REQUEST_PRIORITY = 23;

public static final int STATUS_UNKNOWN = -1;
public static final int STATUS_DEVICE_CONNECTED = BluetoothProfile.STATE_CONNECTED;
Expand All @@ -94,12 +96,18 @@ public class Constants {

public static String getStatusText(int status) {
switch (status) {
case Constants.STATUS_DEVICE_CONNECTED: return "Connected";
case Constants.STATUS_DEVICE_CONNECTING: return "Connecting";
case Constants.STATUS_DEVICE_DISCONNECTING: return "Disconnecting";
case Constants.STATUS_DEVICE_DISCONNECTED: return "Disconnected";
case Constants.STATUS_DEVICE_SERVICE_READY: return "Service Ready";
default: return String.format("Unknown %d", status);
case Constants.STATUS_DEVICE_CONNECTED:
return "Connected";
case Constants.STATUS_DEVICE_CONNECTING:
return "Connecting";
case Constants.STATUS_DEVICE_DISCONNECTING:
return "Disconnecting";
case Constants.STATUS_DEVICE_DISCONNECTED:
return "Disconnected";
case Constants.STATUS_DEVICE_SERVICE_READY:
return "Service Ready";
default:
return String.format("Unknown %d", status);
}
}

Expand All @@ -114,4 +122,9 @@ public static String getStatusText(int status) {

public static final int GATT_DEF_BLE_MTU_SIZE = 23;
public static final int GATT_MAX_MTU_SIZE = 517;

public static final int PRIORITY_BALANCED = 0;
public static final int PRIORITY_HIGH = 1;
public static final int PRIORITY_LOW_POWER = 2;

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public interface IBluetoothClient {

void requestMtu(String mac, int mtu, BleMtuResponse response);

void requestConnectionPriority(String mac, int connectionPriority);

void search(SearchRequest request, SearchResponse response);

void stopSearch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.inuker.bluetooth.library.connect.request.BleIndicateRequest;
import com.inuker.bluetooth.library.connect.request.BleMtuRequest;
import com.inuker.bluetooth.library.connect.request.BleNotifyRequest;
import com.inuker.bluetooth.library.connect.request.BlePriorityRequest;
import com.inuker.bluetooth.library.connect.request.BleReadDescriptorRequest;
import com.inuker.bluetooth.library.connect.request.BleReadRequest;
import com.inuker.bluetooth.library.connect.request.BleReadRssiRequest;
Expand Down Expand Up @@ -164,6 +165,10 @@ public void requestMtu(int mtu, BleGeneralResponse response) {
addNewRequest(new BleMtuRequest(mtu, response));
}

public void requestConnectionPriority(int priority) {
addNewRequest(new BlePriorityRequest(priority, null));
}

private void addNewRequest(BleRequest request) {
checkRuntime();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public static void requestMtu(String mac, int mtu, BleGeneralResponse response)
getBleConnectMaster(mac).requestMtu(mtu, response);
}

public static void requestConnectionPriority(String mac, int priority) {
getBleConnectMaster(mac).requestConnectionPriority(priority);
}

public static void clearRequest(String mac, int type) {
getBleConnectMaster(mac).clearRequest(type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ public void requestMtu(int mtu, BleGeneralResponse response) {
getConnectDispatcher().requestMtu(mtu, response);
}

@Override
public void requestConnectionPriority(int connectionPriority) {
getConnectDispatcher().requestConnectionPriority(connectionPriority);
}

@Override
public void clearRequest(int clearType) {
getConnectDispatcher().clearRequest(clearType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ private void broadcastConnectStatus(int status) {
}

private void broadcastCharacterChanged(UUID service, UUID character,
byte[] value) {
byte[] value) {
Intent intent = new Intent(
Constants.ACTION_CHARACTER_CHANGED);
intent.putExtra(Constants.EXTRA_MAC,
Expand Down Expand Up @@ -718,6 +718,30 @@ public boolean requestMtu(int mtu) {
return true;
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean requestConnectionPriority(int connectionPriority) {
checkRuntime();

BluetoothLog.v(String.format("requestConnectionPriority for %s, priority = %s", getAddress(), connectionPriority));

if (connectionPriority < Constants.PRIORITY_BALANCED ||
connectionPriority > Constants.PRIORITY_LOW_POWER) {
BluetoothLog.e(String.format("requestConnectionPriority- params: %s not within valid range", connectionPriority));
return false;
}
if (mBluetoothGatt == null) {
BluetoothLog.e("ble gatt null");
return false;
}

if (!mBluetoothGatt.requestConnectionPriority(connectionPriority)) {
BluetoothLog.e("requestConnectionPriority failed");
return false;
}
return true;
}

@Override
public BleGattProfile getGattProfile() {
return mBleGattProfile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public interface IBleConnectMaster {

void requestMtu(int mtu, BleGeneralResponse response);

void requestConnectionPriority(int connectionPriority);

void clearRequest(int clearType);

void refreshCache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@ public interface IBleConnectWorker {

boolean requestMtu(int mtu);

boolean requestConnectionPriority(int connectionPriority);

BleGattProfile getGattProfile();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.inuker.bluetooth.library.connect.request;

import com.inuker.bluetooth.library.Code;
import com.inuker.bluetooth.library.Constants;
import com.inuker.bluetooth.library.connect.response.BleGeneralResponse;

public class BlePriorityRequest extends BleRequest {

private int priority;

public BlePriorityRequest(int priority, BleGeneralResponse response) {
super(response);
this.priority = priority;
}

@Override
public void processRequest() {
switch (getCurrentStatus()) {

case Constants.STATUS_DEVICE_CONNECTED:
case Constants.STATUS_DEVICE_SERVICE_READY:
requestConnectionPriority();
break;

default://Constants.STATUS_DEVICE_DISCONNECTED or Others
onRequestCompleted(Code.REQUEST_FAILED);
break;
}
}

private void requestConnectionPriority() {
if (!requestConnectionPriority(priority)) {
onRequestCompleted(Code.REQUEST_FAILED);
} else {
onRequestCompleted(Code.REQUEST_SUCCESS);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ public boolean requestMtu(int mtu) {
return mWorker.requestMtu(mtu);
}

@Override
public boolean requestConnectionPriority(int connectionPriority) {
return mWorker.requestConnectionPriority(connectionPriority);
}

protected void log(String msg) {
BluetoothLog.v(String.format("%s %s >>> %s", getClass().getSimpleName(), getAddress(), msg));
}
Expand Down