BLE Gatt onConnectionStateChange失败,状态133和257

时间:2017-11-24 12:02:14

标签: android bluetooth-lowenergy beacon gatt

我试图将我的Beacons连接到Gattservice。在Callback onConnectionStateChange中,它始终失败并且正在获取

  

statuscodes 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函数。 谢谢你的帮助!

3 个答案:

答案 0 :(得分:0)

  

试试这个:开始扫描:

    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
    }
}
  

gattcallback

 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方法。

答案 1 :(得分:0)

133是一般错误,除了连接失败之外什么都没有。它可能意味着许多不同的事情。您需要查看Logcat并找出它没有连接的原因或者hci snoop日志。

答案 2 :(得分:0)

我必须在调用中指定传输参数以解决此问题。这是一个可选的第4参数,用于指定它是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)