Android BluetoothGatt - status 133 - register callback

前端 未结 6 694
庸人自扰
庸人自扰 2021-01-30 08:56

First of all I read SOLVED: GATT callback fails to register and took the steps suggested in that post to solve this issue with no success. The recommended fix in there if you ha

6条回答
  •  天命终不由人
    2021-01-30 09:29

    Try to use next workaround:

    private static boolean gatt_status_133 = false;
    
    final Handler handler = new Handler();
    
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            mConnectionState = STATE_CONNECTED;
            Log.i(TAG, "Connected to GATT server.");
            // Attempts to discover services after successful connection.
            Log.i(TAG, "Attempting to start service discovery:" +
                    mBluetoothGatt.discoverServices());
    
        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            if(status == 133)
            {
                gatt_status_133=true;
            }
            else{
                mConnectionState = STATE_DISCONNECTED;
                Log.i(TAG, "Disconnected from GATT server."); 
            }
        }
    }
    
    
    @RequiresApi(api = Build.VERSION_CODES.M)
    public boolean connect(final String address) {
        if (mBluetoothAdapter == null || address == null) {
            Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
            return false;
        }
    
        // Previously connected device.  Try to reconnect.
        if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
                && mBluetoothGatt != null) {
            Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
            if (mBluetoothGatt.connect()) {
                mConnectionState = 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, BluetoothDevice.TRANSPORT_LE);
        Log.d(TAG, "Trying to create a new connection.");
        mBluetoothDeviceAddress = address;
        mConnectionState = STATE_CONNECTING;
    
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(gatt_status_133)
                {
                    Log.d(TAG, "Catch issue");
                    connect(address);
                    gatt_status_133=false;
                }
            }
        }, 4000);
    
        return true;
    }
    

提交回复
热议问题