private
final
BluetoothGattCallback mGattCallback =
new
BluetoothGattCallback() {
@Override
public
void
onConnectionStateChange(BluetoothGatt gatt,
int
status,
int
newState) {
String intentAction;
if
(status == BluetoothGatt.GATT_SUCCESS) {
if
(newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = BluetoothConstants.ACTION_GATT_CONNECTED;
mBLEConnectionState = BluetoothConstants.BLE_STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG,
"Connected to GATT server."
);
Log.i(TAG,
"Attempting to start service discovery:"
+ mBluetoothGatt.discoverServices());
}
else
if
(newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = BluetoothConstants.ACTION_GATT_DISCONNECTED;
mBLEConnectionState = BluetoothConstants.BLE_STATE_DISCONNECTED;
close();
// 防止出现status 133
Log.i(TAG,
"Disconnected from GATT server."
);
broadcastUpdate(intentAction);
}
}
else
{
Log.d(TAG,
"onConnectionStateChange received: "
+ status);
intentAction = BluetoothConstants.GATT_STATUS_133;
mBLEConnectionState = BluetoothConstants.BLE_STATE_DISCONNECTED;
close();
// 防止出现status 133
broadcastUpdate(intentAction);
connect(reConnectAddress);
}
}
@Override
public
void
onServicesDiscovered(BluetoothGatt gatt,
int
status) {
if
(status == BluetoothGatt.GATT_SUCCESS) {
Log.d(TAG,
"mBluetoothGatt = "
+ mBluetoothGatt);
broadcastUpdate(BluetoothConstants.ACTION_GATT_SERVICES_DISCOVERED);
BluetoothGattService gattService = mBluetoothGatt
.getService(UUID.fromString(UUIDS.KARINGUPGRADE_SERVICE_UUID));
if
(gattService ==
null
) {
showMessage(
"gattService not found!"
);
broadcastUpdate(BluetoothConstants.DEVICE_DOES_NOT_SUPPORT_UART);
return
;
}
Log.d(TAG,
"onServicesDiscovered************************gattService="
+ gattService);
BluetoothGattCharacteristic character = gattService
.getCharacteristic(UUID.fromString(UUIDS.RX_CHAR_UUID));
enableRXNotification(
true
, character);
}
else
{
Log.d(TAG,
"onServicesDiscovered received: "
+ status);
}
}
@Override
public
void
onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
int
status) {
if
(status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(BluetoothConstants.ACTION_DATA_AVAILABLE, characteristic);
}
}
@Override
public
void
onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
broadcastUpdate(BluetoothConstants.ACTION_DATA_AVAILABLE, characteristic);
}
};
public
void
setReConnectAddress(String address) {
reConnectAddress = address;
}
public
boolean
connect(
final
String address) {
if
(mBluetoothAdapter ==
null
) {
Log.w(TAG,
"BluetoothAdapter not initialized."
);
return
false
;
}
if
(address ==
null
) {
Log.w(TAG,
"Unspecified address."
);
}
if
(mBluetoothDeviceAddress !=
null
&& address.equals(mBluetoothDeviceAddress) && mBluetoothGatt !=
null
) {
Log.d(TAG,
"Trying to use an existing mBluetoothGatt for connection."
);
if
(mBluetoothGatt.connect()) {
mBLEConnectionState = BluetoothConstants.BLE_STATE_CONNECTING;
return
true
;
}
else
{
return
false
;
}
}
final
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if
(device ==
null
) {
Log.w(TAG,
"Device not found. Unable to connect."
);
return
false
;
}
mBluetoothGatt = device.connectGatt(
this
,
false
, mGattCallback);
Log.d(TAG,
"Trying to create a new connection."
);
mBluetoothDeviceAddress = address;
Log.d(TAG,
"BluetoothDeviceAddress is :"
+ mBluetoothDeviceAddress);
mBLEConnectionState = BluetoothConstants.BLE_STATE_CONNECTING;
return
true
;
}
public
void
close() {
if
(mBluetoothGatt ==
null
) {
return
;
}
Log.w(TAG,
"mBluetoothGatt closed"
);
mBluetoothDeviceAddress =
null
;
disconnect();
mBluetoothGatt.close();
mBluetoothGatt =
null
;
}