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

当设备Android系统处于4.3以下的时候,会直接强退 #46

Closed
ouyangshengduo opened this issue Mar 28, 2017 · 3 comments
Closed

Comments

@ouyangshengduo
Copy link

在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);
            }
        }
    }
};
@Jasonchenlijian
Copy link
Owner

BleManager bleManager = new BleManager(this);
if( bleManager.isSupportBle() ){

}

@ouyangshengduo
Copy link
Author

首先是先实例化:
bleManager = new BleManager(mContext);
然后进来:
public BleManager(Context context) {
this.mContext = context;

    if (bleBluetooth == null) {
        bleBluetooth = new BleBluetooth(context);
    }

    bleExceptionHandler = new DefaultBleExceptionHandler(context);

}

其中 bleBluetooth = new BleBluetooth(context);这一句里面有个回调的全局变量coreGattCallback,
coreGattCallback = new BleGattCallback(),而BleGattCallback是继承自BluetoothGattCallback的,然而我用android4.1.1的设备测试的时候,实例化就直接崩溃,所以:
if( bleManager.isSupportBle() ){

}
这一句走不到这里来

@Jasonchenlijian
Copy link
Owner

建议在使用库之前,自主判断Android版本

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants