下面列出了android.bluetooth.BluetoothGatt#requestConnectionPriority ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED && status == gatt.GATT_SUCCESS) {
Log.i(TAG, "Connected to GATT server");
mBluetoothGatt = gatt;
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
gatt.requestConnectionPriority(CONNECTION_PRIORITY_HIGH);
gatt.requestMtu(505);
} else {
gatt.discoverServices();
}
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG, "Disconnected to GATT server");
// ensure progress dialog is removed and running is set false
close();
} else if (status != gatt.GATT_SUCCESS) {
Log.i(TAG, "Status is " + status);
close();
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private boolean internalRequestConnectionPriority(@ConnectionPriority final int priority) {
final BluetoothGatt gatt = bluetoothGatt;
if (gatt == null || !connected)
return false;
String text, priorityText;
switch (priority) {
case ConnectionPriorityRequest.CONNECTION_PRIORITY_HIGH:
text = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ?
"HIGH (11.25–15ms, 0, 20s)" : "HIGH (7.5–10ms, 0, 20s)";
priorityText = "HIGH";
break;
case ConnectionPriorityRequest.CONNECTION_PRIORITY_LOW_POWER:
text = "LOW POWER (100–125ms, 2, 20s)";
priorityText = "LOW POWER";
break;
default:
case ConnectionPriorityRequest.CONNECTION_PRIORITY_BALANCED:
text = "BALANCED (30–50ms, 0, 20s)";
priorityText = "BALANCED";
break;
}
log(Log.VERBOSE, "Requesting connection priority: " + text + "...");
log(Log.DEBUG, "gatt.requestConnectionPriority(" + priorityText + ")");
return gatt.requestConnectionPriority(priority);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private boolean internalRequestConnectionPriority(final int priority) {
final BluetoothGatt gatt = bluetoothGatt;
if (gatt == null)
return false;
return gatt.requestConnectionPriority(priority);
}
@Override
protected void transaction(GattTransactionCallback callback) {
super.transaction(callback);
getConnection().setState(GattState.REQUESTING_CONNECTION_INTERVAL_CHANGE);
TransactionResult.Builder builder = new TransactionResult.Builder().transactionName(getName());
builder.resultStatus(TransactionResult.TransactionResultStatus.FAILURE);
boolean success = false;
if(FitbitGatt.atLeastSDK(LOLLIPOP)) {
BluetoothGatt localGatt = getConnection().getGatt();
if(localGatt != null) {
success = localGatt.requestConnectionPriority(speed.getConnectionPriority());
} else {
Timber.w("Couldn't request connection priority because gatt was null");
}
if(!success) {
getConnection().setState(GattState.REQUEST_CONNECTION_INTERVAL_FAILURE);
builder.responseStatus(GattStatus.GATT_NO_RESOURCES.getCode());
builder.gattState(getConnection().getGattState());
mainThreadHandler.post(() -> {
callCallbackWithTransactionResultAndRelease(callback, builder.build());
// even if we can't change the interval, we can still use the connection
getConnection().setState(GattState.IDLE);
});
} else {
getConnection().setState(GattState.REQUEST_CONNECTION_INTERVAL_SUCCESS);
builder.gattState(getConnection().getGattState());
builder.resultStatus(TransactionResult.TransactionResultStatus.SUCCESS);
mainThreadHandler.post(() -> {
callCallbackWithTransactionResultAndRelease(callback, builder.build());
// we were able to send the request, so we good.
getConnection().setState(GattState.IDLE);
});
}
} else {
getConnection().setState(GattState.REQUEST_CONNECTION_INTERVAL_FAILURE);
builder.responseStatus(GattStatus.GATT_NO_RESOURCES.getCode());
builder.gattState(getConnection().getGattState());
mainThreadHandler.post(() -> {
callCallbackWithTransactionResultAndRelease(callback, builder.build());
// even if we can't change the interval, we can still use the connection
getConnection().setState(GattState.IDLE);
});
}
}
@RequiresApi(21 /* Build.VERSION_CODES.LOLLIPOP */)
@Override
protected boolean startOperation(BluetoothGatt bluetoothGatt) throws IllegalArgumentException, BleGattCannotStartException {
return bluetoothGatt.requestConnectionPriority(connectionPriority);
}
public static boolean requestConnectionPriority(BluetoothGatt gatt, int mode) {
return gatt.requestConnectionPriority(mode);
}
private synchronized void processRxCharacteristic(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Log.i(TAG, "onCharacteristicChanged On Main Thread? " + isOnMainThread());
Log.e(TAG, "CharBytes-nfy" + Arrays.toString(characteristic.getValue()));
Log.i(TAG, "CharHex-nfy" + Extensions.bytesToHex(characteristic.getValue()));
byte[] buffer = characteristic.getValue();
byte firstByte = buffer[0];
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && gatt != null) {
gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH);
}
Log.d(TAG, "Received opcode reply: " + JoH.bytesToHex(new byte[] { firstByte }));
if (firstByte == 0x2f) {
SensorRxMessage sensorRx = new SensorRxMessage(characteristic.getValue());
ByteBuffer sensorData = ByteBuffer.allocate(buffer.length);
sensorData.order(ByteOrder.LITTLE_ENDIAN);
sensorData.put(buffer, 0, buffer.length);
int sensor_battery_level = 0;
if (sensorRx.status == TransmitterStatus.BRICKED) {
//TODO Handle this in UI/Notification
sensor_battery_level = 206; //will give message "EMPTY"
} else if (sensorRx.status == TransmitterStatus.LOW) {
sensor_battery_level = 209; //will give message "LOW"
} else {
sensor_battery_level = 216; //no message, just system status "OK"
}
//Log.e(TAG, "filtered: " + sensorRx.filtered);
disconnected133 = 0; // reset as we got a reading
disconnected59 = 0;
lastState = "Got data OK: " + JoH.hourMinuteString();
successes++;
failures=0;
Log.e(TAG, "SUCCESS!! unfiltered: " + sensorRx.unfiltered + " timestamp: " + sensorRx.timestamp + " " + JoH.qs((double)sensorRx.timestamp / 86400, 1) + " days");
if (sensorRx.unfiltered == 0) {
lastState = "Transmitter sent raw sensor value of 0 !! This isn't good. " + JoH.hourMinuteString();
}
last_transmitter_timestamp = sensorRx.timestamp;
if ((getVersionDetails) && (!haveFirmwareDetails())) {
doVersionRequestMessage(gatt, characteristic);
} else if ((getBatteryDetails) && (getBatteryStatusNow || !haveCurrentBatteryStatus())) {
doBatteryInfoRequestMessage(gatt, characteristic);
} else {
doDisconnectMessage(gatt, characteristic);
}
// TODO beware that wear G5CollectionService is now getting rather out of sync with app version
final boolean g6 = usingG6();
processNewTransmitterData(g6 ? sensorRx.unfiltered * G6_SCALING : sensorRx.unfiltered, g6 ? sensorRx.filtered * G6_SCALING : sensorRx.filtered, sensor_battery_level, new Date().getTime());
// was this the first success after we force enabled always_authenticate?
if (force_always_authenticate && (successes == 1)) {
Log.wtf(TAG, "We apparently only got a reading after forcing the Always Authenticate option");
Home.toaststaticnext("Please Enable G5 Always Authenticate debug option!");
// TODO should we actually change the settings here?
}
} else if (firstByte == GlucoseRxMessage.opcode) {
disconnected133 = 0; // reset as we got a reading
disconnected59 = 0;
GlucoseRxMessage glucoseRx = new GlucoseRxMessage(characteristic.getValue());
Log.e(TAG, "SUCCESS!! glucose unfiltered: " + glucoseRx.unfiltered);
successes++;
failures=0;
doDisconnectMessage(gatt, characteristic);
processNewTransmitterData(glucoseRx.unfiltered, glucoseRx.filtered, 216, new Date().getTime());
} else if (firstByte == VersionRequestRxMessage.opcode) {
if (!setStoredFirmwareBytes(defaultTransmitter.transmitterId, characteristic.getValue(), true)) {
Log.wtf(TAG, "Could not save out firmware version!");
}
doDisconnectMessage(gatt, characteristic);
} else if (firstByte == BatteryInfoRxMessage.opcode) {
if (!setStoredBatteryBytes(defaultTransmitter.transmitterId, characteristic.getValue())) {
Log.wtf(TAG, "Could not save out battery data!");
}
getBatteryStatusNow = false;
doDisconnectMessage(gatt, characteristic);
} else {
Log.e(TAG, "onCharacteristic CHANGED unexpected opcode: " + firstByte + " (have not disconnected!)");
}
Log.e(TAG, "OnCharacteristic CHANGED finished: ");
}
private synchronized void processRxCharacteristic(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Log.i(TAG, "onCharacteristicChanged On Main Thread? " + isOnMainThread());
Log.e(TAG, "CharBytes-nfy" + Arrays.toString(characteristic.getValue()));
Log.i(TAG, "CharHex-nfy" + Extensions.bytesToHex(characteristic.getValue()));
byte[] buffer = characteristic.getValue();
byte firstByte = buffer[0];
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && gatt != null) {
gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH);
}
Log.d(TAG, "Received opcode reply: " + JoH.bytesToHex(new byte[] { firstByte }));
if (firstByte == 0x2f) {
SensorRxMessage sensorRx = new SensorRxMessage(characteristic.getValue());
ByteBuffer sensorData = ByteBuffer.allocate(buffer.length);
sensorData.order(ByteOrder.LITTLE_ENDIAN);
sensorData.put(buffer, 0, buffer.length);
int sensor_battery_level = 0;
if (sensorRx.status == TransmitterStatus.BRICKED) {
//TODO Handle this in UI/Notification
sensor_battery_level = 206; //will give message "EMPTY"
} else if (sensorRx.status == TransmitterStatus.LOW) {
sensor_battery_level = 209; //will give message "LOW"
} else {
sensor_battery_level = 216; //no message, just system status "OK"
}
//Log.e(TAG, "filtered: " + sensorRx.filtered);
disconnected133 = 0; // reset as we got a reading
disconnected59 = 0;
lastState = "Got data OK: " + JoH.hourMinuteString();
successes++;
failures=0;
Log.e(TAG, "SUCCESS!! unfiltered: " + sensorRx.unfiltered + " timestamp: " + sensorRx.timestamp + " " + JoH.qs((double)sensorRx.timestamp / 86400, 1) + " days");
if (sensorRx.unfiltered == 0) {
lastState = "Transmitter sent raw sensor value of 0 !! This isn't good. " + JoH.hourMinuteString();
}
last_transmitter_timestamp = sensorRx.timestamp;
if ((getVersionDetails) && (!haveFirmwareDetails())) {
doVersionRequestMessage(gatt, characteristic);
} else if ((getBatteryDetails) && (getBatteryStatusNow || !haveCurrentBatteryStatus())) {
doBatteryInfoRequestMessage(gatt, characteristic);
} else {
doDisconnectMessage(gatt, characteristic);
}
final boolean g6 = usingG6();
processNewTransmitterData(g6 ? sensorRx.unfiltered * G6_SCALING : sensorRx.unfiltered, g6 ? sensorRx.filtered * G6_SCALING : sensorRx.filtered, sensor_battery_level, new Date().getTime());
// was this the first success after we force enabled always_authenticate?
if (force_always_authenticate && (successes == 1)) {
Log.wtf(TAG, "We apparently only got a reading after forcing the Always Authenticate option");
Home.toaststaticnext("Please Enable G5 Always Authenticate debug option!");
// TODO should we actually change the settings here?
}
} else if (firstByte == GlucoseRxMessage.opcode) {
// TODO doesn't support firmware version reading in GlucoseRX
disconnected133 = 0; // reset as we got a reading
disconnected59 = 0;
GlucoseRxMessage glucoseRx = new GlucoseRxMessage(characteristic.getValue());
Log.e(TAG, "SUCCESS!! glucose unfiltered: " + glucoseRx.unfiltered);
successes++;
failures=0;
doDisconnectMessage(gatt, characteristic);
processNewTransmitterData(glucoseRx.unfiltered, glucoseRx.filtered, 216, new Date().getTime());
} else if (firstByte == VersionRequestRxMessage.opcode) {
if (!setStoredFirmwareBytes(defaultTransmitter.transmitterId, characteristic.getValue(), true)) {
Log.wtf(TAG, "Could not save out firmware version!");
}
doDisconnectMessage(gatt, characteristic);
} else if (firstByte == BatteryInfoRxMessage.opcode) {
if (!setStoredBatteryBytes(defaultTransmitter.transmitterId, characteristic.getValue())) {
Log.wtf(TAG, "Could not save out battery data!");
}
getBatteryStatusNow = false;
doDisconnectMessage(gatt, characteristic);
} else {
Log.e(TAG, "onCharacteristic CHANGED unexpected opcode: " + firstByte + " (have not disconnected!)");
}
Log.e(TAG, "OnCharacteristic CHANGED finished: ");
}
private synchronized void processRxCharacteristic(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Log.i(TAG, "onCharacteristicChanged On Main Thread? " + isOnMainThread());
Log.e(TAG, "CharBytes-nfy" + Arrays.toString(characteristic.getValue()));
Log.i(TAG, "CharHex-nfy" + Extensions.bytesToHex(characteristic.getValue()));
byte[] buffer = characteristic.getValue();
byte firstByte = buffer[0];
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && gatt != null) {
gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH);
}
Log.d(TAG, "Received opcode reply: " + JoH.bytesToHex(new byte[] { firstByte }));
if (firstByte == 0x2f) {
SensorRxMessage sensorRx = new SensorRxMessage(characteristic.getValue());
ByteBuffer sensorData = ByteBuffer.allocate(buffer.length);
sensorData.order(ByteOrder.LITTLE_ENDIAN);
sensorData.put(buffer, 0, buffer.length);
int sensor_battery_level = 0;
if (sensorRx.status == TransmitterStatus.BRICKED) {
//TODO Handle this in UI/Notification
sensor_battery_level = 206; //will give message "EMPTY"
} else if (sensorRx.status == TransmitterStatus.LOW) {
sensor_battery_level = 209; //will give message "LOW"
} else {
sensor_battery_level = 216; //no message, just system status "OK"
}
//Log.e(TAG, "filtered: " + sensorRx.filtered);
disconnected133 = 0; // reset as we got a reading
disconnected59 = 0;
lastState = "Got data OK: " + JoH.hourMinuteString();
successes++;
failures=0;
Log.e(TAG, "SUCCESS!! unfiltered: " + sensorRx.unfiltered + " timestamp: " + sensorRx.timestamp + " " + JoH.qs((double)sensorRx.timestamp / 86400, 1) + " days");
if (sensorRx.unfiltered == 0) {
lastState = "Transmitter sent raw sensor value of 0 !! This isn't good. " + JoH.hourMinuteString();
}
last_transmitter_timestamp = sensorRx.timestamp;
if ((getVersionDetails) && (!haveFirmwareDetails())) {
doVersionRequestMessage(gatt, characteristic);
} else if ((getBatteryDetails) && (getBatteryStatusNow || !haveCurrentBatteryStatus())) {
doBatteryInfoRequestMessage(gatt, characteristic);
} else {
doDisconnectMessage(gatt, characteristic);
}
// TODO beware that wear G5CollectionService is now getting rather out of sync with app version
final boolean g6 = usingG6();
processNewTransmitterData(g6 ? sensorRx.unfiltered * G6_SCALING : sensorRx.unfiltered, g6 ? sensorRx.filtered * G6_SCALING : sensorRx.filtered, sensor_battery_level, new Date().getTime());
// was this the first success after we force enabled always_authenticate?
if (force_always_authenticate && (successes == 1)) {
Log.wtf(TAG, "We apparently only got a reading after forcing the Always Authenticate option");
Home.toaststaticnext("Please Enable G5 Always Authenticate debug option!");
// TODO should we actually change the settings here?
}
} else if (firstByte == GlucoseRxMessage.opcode) {
disconnected133 = 0; // reset as we got a reading
disconnected59 = 0;
GlucoseRxMessage glucoseRx = new GlucoseRxMessage(characteristic.getValue());
Log.e(TAG, "SUCCESS!! glucose unfiltered: " + glucoseRx.unfiltered);
successes++;
failures=0;
doDisconnectMessage(gatt, characteristic);
processNewTransmitterData(glucoseRx.unfiltered, glucoseRx.filtered, 216, new Date().getTime());
} else if (firstByte == VersionRequestRxMessage.opcode) {
if (!setStoredFirmwareBytes(defaultTransmitter.transmitterId, characteristic.getValue(), true)) {
Log.wtf(TAG, "Could not save out firmware version!");
}
doDisconnectMessage(gatt, characteristic);
} else if (firstByte == BatteryInfoRxMessage.opcode) {
if (!setStoredBatteryBytes(defaultTransmitter.transmitterId, characteristic.getValue())) {
Log.wtf(TAG, "Could not save out battery data!");
}
getBatteryStatusNow = false;
doDisconnectMessage(gatt, characteristic);
} else {
Log.e(TAG, "onCharacteristic CHANGED unexpected opcode: " + firstByte + " (have not disconnected!)");
}
Log.e(TAG, "OnCharacteristic CHANGED finished: ");
}
private synchronized void processRxCharacteristic(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Log.i(TAG, "onCharacteristicChanged On Main Thread? " + isOnMainThread());
Log.e(TAG, "CharBytes-nfy" + Arrays.toString(characteristic.getValue()));
Log.i(TAG, "CharHex-nfy" + Extensions.bytesToHex(characteristic.getValue()));
byte[] buffer = characteristic.getValue();
byte firstByte = buffer[0];
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && gatt != null) {
gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH);
}
Log.d(TAG, "Received opcode reply: " + JoH.bytesToHex(new byte[] { firstByte }));
if (firstByte == 0x2f) {
SensorRxMessage sensorRx = new SensorRxMessage(characteristic.getValue());
ByteBuffer sensorData = ByteBuffer.allocate(buffer.length);
sensorData.order(ByteOrder.LITTLE_ENDIAN);
sensorData.put(buffer, 0, buffer.length);
int sensor_battery_level = 0;
if (sensorRx.status == TransmitterStatus.BRICKED) {
//TODO Handle this in UI/Notification
sensor_battery_level = 206; //will give message "EMPTY"
} else if (sensorRx.status == TransmitterStatus.LOW) {
sensor_battery_level = 209; //will give message "LOW"
} else {
sensor_battery_level = 216; //no message, just system status "OK"
}
//Log.e(TAG, "filtered: " + sensorRx.filtered);
disconnected133 = 0; // reset as we got a reading
disconnected59 = 0;
lastState = "Got data OK: " + JoH.hourMinuteString();
successes++;
failures=0;
Log.e(TAG, "SUCCESS!! unfiltered: " + sensorRx.unfiltered + " timestamp: " + sensorRx.timestamp + " " + JoH.qs((double)sensorRx.timestamp / 86400, 1) + " days");
if (sensorRx.unfiltered == 0) {
lastState = "Transmitter sent raw sensor value of 0 !! This isn't good. " + JoH.hourMinuteString();
}
last_transmitter_timestamp = sensorRx.timestamp;
if ((getVersionDetails) && (!haveFirmwareDetails())) {
doVersionRequestMessage(gatt, characteristic);
} else if ((getBatteryDetails) && (getBatteryStatusNow || !haveCurrentBatteryStatus())) {
doBatteryInfoRequestMessage(gatt, characteristic);
} else {
doDisconnectMessage(gatt, characteristic);
}
final boolean g6 = usingG6();
processNewTransmitterData(g6 ? sensorRx.unfiltered * G6_SCALING : sensorRx.unfiltered, g6 ? sensorRx.filtered * G6_SCALING : sensorRx.filtered, sensor_battery_level, new Date().getTime());
// was this the first success after we force enabled always_authenticate?
if (force_always_authenticate && (successes == 1)) {
Log.wtf(TAG, "We apparently only got a reading after forcing the Always Authenticate option");
Home.toaststaticnext("Please Enable G5 Always Authenticate debug option!");
// TODO should we actually change the settings here?
}
} else if (firstByte == GlucoseRxMessage.opcode) {
// TODO doesn't support firmware version reading in GlucoseRX
disconnected133 = 0; // reset as we got a reading
disconnected59 = 0;
GlucoseRxMessage glucoseRx = new GlucoseRxMessage(characteristic.getValue());
Log.e(TAG, "SUCCESS!! glucose unfiltered: " + glucoseRx.unfiltered);
successes++;
failures=0;
doDisconnectMessage(gatt, characteristic);
processNewTransmitterData(glucoseRx.unfiltered, glucoseRx.filtered, 216, new Date().getTime());
} else if (firstByte == VersionRequestRxMessage.opcode) {
if (!setStoredFirmwareBytes(defaultTransmitter.transmitterId, characteristic.getValue(), true)) {
Log.wtf(TAG, "Could not save out firmware version!");
}
doDisconnectMessage(gatt, characteristic);
} else if (firstByte == BatteryInfoRxMessage.opcode) {
if (!setStoredBatteryBytes(defaultTransmitter.transmitterId, characteristic.getValue())) {
Log.wtf(TAG, "Could not save out battery data!");
}
getBatteryStatusNow = false;
doDisconnectMessage(gatt, characteristic);
} else {
Log.e(TAG, "onCharacteristic CHANGED unexpected opcode: " + firstByte + " (have not disconnected!)");
}
Log.e(TAG, "OnCharacteristic CHANGED finished: ");
}
public static boolean requestConnectionPriority(BluetoothGatt gatt, int mode) {
return gatt.requestConnectionPriority(mode);
}