BLuetooth-Gatt-Rückruf funktioniert nicht mit der neuen API für Lollipop

Ich habe derzeit eine Methode, die schreibt, um die BLE-Geräte zu PIEPEN. Mein Bluetooth-Rückruf geht wie folgt :

ReadCharacteristic rc = new ReadCharacteristic(context, ds.getMacAddress(), serviceUUID, UUID.fromString(myUUID), "") {
                @Override
                public void onRead() {
                    Log.w(TAG, "callDevice onRead");
                    try{Thread.sleep(1000);}catch(InterruptedException ex){}
                    WriteCharacteristic wc = new WriteCharacteristic(activity, context, getMacAddress(), serviceUUID, UUID.fromString(myUUID), ""){
                        @Override
                        public void onWrite(){
                            Log.w(TAG, "callDevice onWrite");
                        }
                        @Override
                        public void onError(){
                            Log.w(TAG, "callDevice onWrite-onError");
                        }
                    };

//                 Store data in writeBuffer
                    wc.writeCharacteristic(writeBuffer);
                }

                @Override
                public void onError(){
                    Log.w(TAG, "callDevice onRead-onError");
                }
            };

            rc.readCharacteristic();

Meine ReadCharacteristic Umsetzung ist wie folgt :

public class ReadCharacteristic extends BluetoothGattCallback {
    public ReadCharacteristic(Context context, String macAddress, UUID service, UUID characteristic, Object tag) {
        mMacAddress = macAddress;
        mService = service;
        mCharacteristic = characteristic;
        mTag = tag;
        mContext = context;
        this.activity =activity;
        final BluetoothManager bluetoothManager =
                (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();

    }

    final private static String TAG = "ReadCharacteristic";
    private Object mTag;
    private String mMacAddress;
    private UUID mService;
    private UUID mCharacteristic;
    private byte[] mValue;
    private Activity activity;
    private BluetoothAdapter mBluetoothAdapter;
    private Context mContext;

    private int retry = 5;


    public String getMacAddress() {
        return mMacAddress;
    }

    public UUID getService() {
        return mService;
    }

    public UUID getCharacteristic() {
        return mCharacteristic;
    }

    public byte[] getValue() { return mValue; }

    public void onRead() {
        Log.w(TAG, "onRead: " + getDataHex(getValue()));
    }

    public void onError() {
        Log.w(TAG, "onError");
    }

    public void readCharacteristic(){
        if (retry == 0)
        {
            onError();
            return;
        }
        retry--;



                final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(getMacAddress());
                if (device != null) {
                    Log.w(TAG, "Starting Read [" + getService() + "|" + getCharacteristic() + "]");
                    final ReadCharacteristic rc = ReadCharacteristic.this;
                    device.connectGatt(mContext, false, rc);
                }

    }

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        Log.w(TAG,"onConnectionStateChange [" + status + "|" + newState + "]");
        if ((newState == 2)&&(status ==0)) {
            gatt.discoverServices();
        }

        else{
            Log.w(TAG, "[" + status + "]");
         //  gatt.disconnect();
            gatt.close();
            try
            {
                Thread.sleep(2000);
            }
            catch(Exception e)
            {

            }
            readCharacteristic();
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        Log.w(TAG,"onServicesDiscovered [" + status + "]");
        BluetoothGattService bgs = gatt.getService(getService());
        if (bgs != null) {
            BluetoothGattCharacteristic bgc = bgs.getCharacteristic(getCharacteristic());
            gatt.readCharacteristic(bgc);
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status) {
        Log.w(TAG,"onCharacteristicRead [" + status + "]");
        if (status == BluetoothGatt.GATT_SUCCESS) {
            mValue = characteristic.getValue();
            Log.w(TAG,"onCharacteristicRead [" + mValue + "]");
            gatt.disconnect();
            gatt.close();
            onRead();
        }

        else {
            gatt.disconnect();
            gatt.close();
        }
    }


}

Diese aktuelle Methode funktioniert perfekt für Geräte mit KitKat und unten. Aber wenn ich die gleiche Funktion auf Lollipop, es piept das Gerät ein paar mal und dann nicht mehr funktioniert. Dann ab auf den Stationen, immer wenn ich versuche zu verbinden, sagt Sie das Gerät getrennt und gibt mir eine Fehler-code-257 in OnConnectionStateChanged Methode.

Ich bekomme auch diese Fehlermeldung, wenn ich diese Methode aufrufen -

04-20 14:14:23.503  12329-12384/com.webble.xy W/BluetoothGatt Unhandled exception in callback
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.bluetooth.BluetoothGattCallback.onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)' on a null object reference
            at android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:181)
            at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:70)
            at android.os.Binder.execTransact(Binder.java:446)

Gibt es jemand, der hat vor dem gleichen problem? Ich habe nie festgestellt das Objekt null sein, wenn ich schon mal versucht zu Debuggen.

Ich habe das gleiche problem in meiner app auf >= 5.0. Nichts neues auf eurer Seite ?
Ich habe gerade herausgefunden, dass dieser Fehler tritt auf, wenn ich disconnect aufrufen, gefolgt von schließen, weil die onConnectionStateChange-Objekt auf null gesetzt ist, bevor Sie die Verbindung trennen. Dieser Fehler wird nicht angezeigt wenn ich Anrufe nur in der Nähe. Aber das hat das problem nicht lösen. Ich bin immer noch versuchen zu brechen den Kopf über das Problem . Es beginnt zu arbeiten wenn ich wieder neu starten, bluetooth, aber wieder das Problem weiterhin besteht, nachdem Sie eine Weile. Bitte lassen Sie mich wissen, wenn Sie finden keine Lösung für Sie

InformationsquelleAutor Shashank Hebbale | 2015-04-20

Schreibe einen Kommentar