【问题标题】:BLE Gatt onConnectionStateChange failed, Status 133 and 257BLE Gatt onConnectionStateChange 失败,状态 133 和 257
【发布时间】:2017-11-24 12:02:13
【问题描述】:

我正在尝试将我的信标连接到 Gattservice。在回调 onConnectionStateChange 中,它总是失败,我得到了

状态码 133 和 257。

Somehwere 写道 133 代表许多连接。在我的代码中有带有 gatt.disconnect() 的行。 我不知道如何解决它,因为所有其他 gattexamples 都是相同的。如果发现错误很重要,我正在使用 Android 6.0.1 版本和 API 23。 这是我的代码:

public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

        if(status == BluetoothGatt.GATT_SUCCESS) {
            switch (newState) {
                case BluetoothProfile.STATE_CONNECTED:
                    mBleDevices.add(gatt.getDevice());
                    Log.i("Beacons", "STATE_CONNECTED");

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mBluetoothGatt.discoverServices();
                        }
                    });
                    break;
                case BluetoothProfile.STATE_DISCONNECTED:
                    Log.e("Beacons", "STATE_DISCONNECTED");
                    mBleDevices.remove(gatt.getDevice());
                    mBluetoothGatt.disconnect();
                    mBluetoothGatt = null;
                    break;
                default:
                    Log.e("Beacons", "STATE_OTHER");
            }
        } else {
            Log.e("Beacons", "ERROR WITH CONNECTING " + status);
            mBleDevices.remove(gatt.getDevice());
        }
 }

我的 ScanCallback 如下所示:

 public void onScanResult(int callbackType, ScanResult result) {
    runOnUiThread(new Runnable() {
       @Override
       public void run() {
          BluetoothDevice btDevice = result.getDevice();
          connectToDevice(btDevice);
       }
    });
 }

然后像这样开始连接:

 runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mBluetoothGatt = btDevice.connectGatt(getApplicationContext(), true, connectCallback);
        }
    });

connectCallback 会导致 onConnectionStateChange 函数。 感谢您的帮助!

【问题讨论】:

  • 你有beacon的服务uuid​​吗?
  • Michalik,您将多次收到扫描回调,因此 onScanResult() 正在多次调用,并且您尝试为每个回调连接设备,即您尝试多次连接设备.尝试在列表视图中显示扫描回调结果,然后连接到相应的设备
  • 是的,但这是连接后应该做的事情,对吗?如果有连接,我调用 gatt.getServices(),将它们保存在一个变量中并检查其中一项服务是否与我想要的相同:gattServices.get(i).getUuid().equals(Battery_Service_UUID) ,并对其进行进一步的操作。

标签: android bluetooth-lowenergy beacon gatt


【解决方案1】:

在某些 Android 设备上,如果 Android 手机之前未与外围设备配对(绑定),我会在连接时看到 133 错误。如果我在应用程序尝试在后台连接的同时进入设置 -> 蓝牙,则连接有效。 (奇怪的是,你不需要在屏幕上做任何事情,它只需要启动即可。)一旦第一次成功,错误就永远消失了。

我猜这是一个安全限制,但我还没有找到优雅地解决它的正确方法。

【讨论】:

    【解决方案2】:

    我必须在调用中指定传输参数来解决这个问题。这是一个可选的第四个参数来指定它是一个 BLE 设备。

    mBluetoothGatt = device.connectGatt(this, false, mGattCallback, 2);
    
    

    https://developer.android.com/reference/android/bluetooth/BluetoothDevice#connectGatt(android.content.Context,%20boolean,%20android.bluetooth.BluetoothGattCallback,%20int)

    【讨论】:

    • 添加第四个参数对我有用。谢谢
    【解决方案3】:

    试试这个:开始扫描:

        private void scanLeDevice(final boolean enable) {
            if (enable) {
                ParcelUuid uuid = ParcelUuid.fromString("Your Beacon Service UUID");
                ScanFilter scanFilter = new ScanFilter.Builder().setServiceUuid(uuid).build();
                ScanSettings settings = new ScanSettings.Builder()
                        .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
                        .setReportDelay(0)
                        .build();
    
                if (Build.VERSION.SDK_INT < 21) {
                    mBluetoothAdapter.startLeScan(mLeScanCallback);
                } else {
                   mLEScanner.startScan(Collections.singletonList(scanFilter), settings, mScanCallback);
                }
            } else {
                if (Build.VERSION.SDK_INT < 21) {
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                } else {
                    mLEScanner.stopScan(mScanCallback);
                }
            }
        }
    

    对于scanCallBack

    private ScanCallback mScanCallback = new ScanCallback() {
            @Override
            public void onScanResult(int callbackType, ScanResult result) {
                Log.i("callbackType", String.valueOf(callbackType));
                Log.i("result", result.toString());
                BluetoothDevice btDevice = result.getDevice();
                connectToDevice(btDevice);
            }
    
            @Override
            public void onBatchScanResults(List<ScanResult> results) {
                for (ScanResult sr : results) {
                    Log.i("ScanResult - Results", sr.toString());
                }
            }
    
            @Override
            public void onScanFailed(int errorCode) {
                Log.e("Scan Failed", "Error Code: " + errorCode);
            }
        };
    
    private BluetoothAdapter.LeScanCallback mLeScanCallback =
                new BluetoothAdapter.LeScanCallback() {
                    @Override
                    public void onLeScan(final BluetoothDevice device, int rssi,
                                         byte[] scanRecord) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Log.i("onLeScan", device.toString());
                                connectToDevice(device);
                            }
                        });
                    }
                };
    

    连接设备

    public void connectToDevice(BluetoothDevice device) {
        if (mGatt == null) {
            mGatt = device.connectGatt(this, false, gattCallback);
            scanLeDevice(false);// will stop after first device detection
        }
    }
    

    gatt回调

     private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
            @Override
            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                Log.i("onConnectionStateChange", "Status: " + status);
                switch (newState) {
                    case BluetoothProfile.STATE_CONNECTED:
                        Log.i("gattCallback", "STATE_CONNECTED");
                        gatt.discoverServices();
                        break;
                    case BluetoothProfile.STATE_DISCONNECTED:
    
                        Log.e("gattCallback", "STATE_DISCONNECTED");
                        break;
                    default:
                        Log.e("gattCallback", "STATE_OTHER");
                }
    
            }
    

    然后重写 onServicesDiscovered 方法。

    【讨论】:

    • 好的,错误状态消失了,但是现在代码没有进入mScanCallback。它只会启动和停止扫描。也许调用 scanLeDevice(false) 放错地方了?
    • 首先你必须设置 scanLeDevice(true) 并在设备连接后设置 scanLeDevice(false);
    • 对于按钮单击事件的演示,在“connectToDevice()”方法设置“scanLeDevice(false)”中连接设备后放置“scanLeDevice(true)”。
    • 是的,好吧我明白了,但它并没有涉及到设备正在连接的那部分,然后我在成功连接后放置了 scanLeDevice(false)(在 gattCallback 中) .之后出现错误。
    • 扫描失败,错误代码:1
    【解决方案4】:

    133 是一个通用错误,除了连接失败之外没有任何意义。它可能意味着许多不同的东西。您需要查看 Logcat 并找出它未连接的原因或 hci snoop 日志。

    【讨论】:

      猜你喜欢
      • 2017-12-16
      • 1970-01-01
      • 1970-01-01
      • 2022-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多