添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

BLE Gatt onConnectionStateChange失败,状态133和257

2 人关注

我试图将我的信标连接到Gattservice上。在回调onConnectionStateChange中,它总是失败,我得到的结果是

状态代码133和257。

有些地方写到,133代表了许多连接。在我的代码中,有几行是gatt.disconnect()。 我不知道如何解决这个问题,因为所有其他gattexamples都是一样的。我使用的是安卓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函数。 谢谢你的帮助!

4 个评论
你有Beacon的服务uuid吗?
Michalik,你会多次得到一个扫描回调,所以onScanResult()被多次调用,你正试图为每个回调连接设备,即你正试图多次连接设备。尝试在列表视图中显示扫描回调结果,然后连接到相应的设备
如果有连接,我就调用gatt.getServices(),把它们安全地放在一个变量中,并检查其中一个服务是否与我想要的相同:gattServices.get(i).getUuid().equals(Battery_Service_UUID),并对其做进一步操作。
如果涉及到257个错误,请阅读我在这里的评论。 https://stackoverflow.com/a/74812993/11509261
android
bluetooth-lowenergy
beacon
gatt
Sunny
Sunny
发布于 2017-11-24
4 个回答
davidgyoung
davidgyoung
发布于 2017-11-24
已采纳
0 人赞同

On some Android devices, I see a 133 error on connect if the Android phone has not been paired (bonded) with the peripheral before. If I go to Settings -> Bluetooth at the same time as the app is trying to connect in the background, the connection works. (Strangely, you do not need to do anything on the screen, it just needs to be up.) Once this succeeds the first time, the error is gone forever.

我猜这是一个安全限制,但我还没有想出适当的方法来优雅地解决它。

falon89
falon89
发布于 2017-11-24
0 人赞同

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

添加第4个参数对我有用。谢谢
Shweta Chauhan
Shweta Chauhan
发布于 2017-11-24
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);
  

Connect the 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");