在BleBluetooth.java中,使用了这个全局回调变量coreGattCallback,一旦设备不支持BLE4.0,这里就会出问题,导致实例化的时候,程序崩溃,所以,目前我的是直接在用的那里new一个,不知道还有什么解决方案
private BleGattCallback coreGattCallback = new BleGattCallback() {
@Override
public void onNotFoundDevice() {
BleLog.i("coreGattCallback:onNotFoundDevice ");
bluetoothGatt = null;
Iterator iterator = callbackHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object call = entry.getValue();
if (call instanceof BleGattCallback) {
((BleGattCallback) call).onNotFoundDevice();
}
}
}
@Override
public void onConnectSuccess(BluetoothGatt gatt, int status) {
BleLog.i("coreGattCallback:onConnectSuccess ");
bluetoothGatt = gatt;
Iterator iterator = callbackHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object call = entry.getValue();
if (call instanceof BleGattCallback) {
((BleGattCallback) call).onConnectSuccess(gatt, status);
}
}
bluetoothGatt.discoverServices();
}
@Override
public void onConnectFailure(BleException exception) {
BleLog.i("coreGattCallback:onConnectFailure ");
bluetoothGatt = null;
Iterator iterator = callbackHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object call = entry.getValue();
if (call instanceof BleGattCallback) {
((BleGattCallback) call).onConnectFailure(exception);
}
}
}
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
BleLog.i("coreGattCallback:onConnectionStateChange "
+ '\n' + "status: " + status
+ '\n' + "newState: " + newState
+ '\n' + "thread: " + Thread.currentThread().getId());
if (newState == BluetoothGatt.STATE_CONNECTED) {
connectionState = STATE_CONNECTED;
onConnectSuccess(gatt, status);
} else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
connectionState = STATE_DISCONNECTED;
onConnectFailure(new ConnectException(gatt, status));
} else if (newState == BluetoothGatt.STATE_CONNECTING) {
connectionState = STATE_CONNECTING;
}
Iterator iterator = callbackHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object call = entry.getValue();
if (call instanceof BluetoothGattCallback) {
((BluetoothGattCallback) call).onConnectionStateChange(gatt, status, newState);
}
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
BleLog.i("coreGattCallback:onServicesDiscovered ");
connectionState = STATE_SERVICES_DISCOVERED;
Iterator iterator = callbackHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object call = entry.getValue();
if (call instanceof BluetoothGattCallback) {
((BluetoothGattCallback) call).onServicesDiscovered(gatt, status);
}
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
BleLog.i("coreGattCallback:onCharacteristicRead ");
Iterator iterator = callbackHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object call = entry.getValue();
if (call instanceof BluetoothGattCallback) {
((BluetoothGattCallback) call).onCharacteristicRead(gatt, characteristic, status);
}
}
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
BleLog.i("coreGattCallback:onCharacteristicWrite ");
Iterator iterator = callbackHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object call = entry.getValue();
if (call instanceof BluetoothGattCallback) {
((BluetoothGattCallback) call).onCharacteristicWrite(gatt, characteristic, status);
}
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
BleLog.i("coreGattCallback:onCharacteristicChanged ");
Iterator iterator = callbackHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object call = entry.getValue();
if (call instanceof BluetoothGattCallback) {
((BluetoothGattCallback) call).onCharacteristicChanged(gatt, characteristic);
}
}
}
@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
BleLog.i("coreGattCallback:onDescriptorRead ");
Iterator iterator = callbackHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object call = entry.getValue();
if (call instanceof BluetoothGattCallback) {
((BluetoothGattCallback) call).onDescriptorRead(gatt, descriptor, status);
}
}
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
BleLog.i("coreGattCallback:onDescriptorWrite ");
Iterator iterator = callbackHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object call = entry.getValue();
if (call instanceof BluetoothGattCallback) {
((BluetoothGattCallback) call).onDescriptorWrite(gatt, descriptor, status);
}
}
}
@Override
public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
BleLog.i("coreGattCallback:onReliableWriteCompleted ");
Iterator iterator = callbackHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object call = entry.getValue();
if (call instanceof BluetoothGattCallback) {
((BluetoothGattCallback) call).onReliableWriteCompleted(gatt, status);
}
}
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
BleLog.i("coreGattCallback:onReadRemoteRssi ");
Iterator iterator = callbackHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Object call = entry.getValue();
if (call instanceof BluetoothGattCallback) {
((BluetoothGattCallback) call).onReadRemoteRssi(gatt, rssi, status);
}
}
}
};