2017-05-14 8 views
0

サムスンS7マシュマロで「ガットスタックに失敗しました」というエラーが表示されました。このシナリオでは、21個のBLEペリフェラル(nrf52)で作業しています。私は利用可能なデバイスをスキャンし、次にデバイスを接続する=>書き込みデータ=>デバイスを1つずつ切断します。何度か試してみると、このエラーが出ます。私がアプリケーションが存在すれば、それは元に戻ります。助けてください!gattスタックに登録できませんでしたエラーBLE androidサムスン

既にお試しください:すべてのconnectGatt、disconnectGattメソッドのメインUIスレッドに書き込みます。私は質問が重複していることを知っている GATT callback fails to register on stackoverflow。しかし、それは私のために働いていません。

これは私の主な活動でコード

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; 
     } 
     // We want to directly connect to the device, so we are setting the autoConnect 
     // parameter to false. 
     mBluetoothGatt = device.connectGatt(this,false, mGattCallback); 


     Log.d(TAG, "Trying to create a new connection."); 
     mBluetoothDeviceAddress = address; 
     mConnectionState = STATE_CONNECTING; 
     return true; 
    } 

public void disconnect() { 
     if (mBluetoothAdapter == null || mBluetoothGatt == null) { 
      Log.w(TAG, "BluetoothAdapter not initialized"); 
      return; 
     } 
     Handler handler = new Handler(Looper.getMainLooper()); 
     handler.post(new Runnable() { 
      @Override 
      public void run() { 
       mBluetoothGatt.disconnect(); 
      } 
     }); 
    } 

public void close() { 
     if (mBluetoothGatt == null) { 
      return; 
     } 
     Log.w(TAG, "mBluetoothGatt closed"); 
     mBluetoothDeviceAddress = null; 
     mBluetoothGatt.close(); 
     mBluetoothGatt = null; 
    } 

ですが、私は放送受信機を使用

public final BroadcastReceiver myUpdateReceiver = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
       /* 
    Broadcast receiver will listen the listener respond from registered service UartService.class 
    */ 
      final String action = intent.getAction(); 
      if (BluetoothConstants.ACTION_GATT_CONNECTED.equals(action)) { 
       Log.d("Connected Device ", intent.getStringExtra(action) + "is connected "); 

       //Connection made, here you can make a decision: do you want to initiate service discovery. 
       // P.S. If you are working with multiple devices, 
       // make sure that you start the service discovery 
       // after all desired connections are made 
       // } 
      } 
      if (BluetoothConstants.ACTION_GATT_DISCONNECTED.equals(action)) { 
       if (filterQueue.size() != 0) { 
        mDevice = filterQueue.poll(); 
        progressDlg.setMessage("Start Initiating to " + mDevice.getName()); 
        progressDlg.show(); 
        Handler handler = new Handler(Looper.getMainLooper()); 
        handler.post(new Runnable() { 
         @Override 
         public void run() { 
          mService.connect(mDevice.getAddress(), getApplicationContext()); 
          scanLeDevice(false); 
         } 
        }); 

       } else { 
        progressDlg.cancel(); 
        connectionQueue.clear(); 
       }    
      } 

      /* if uart servcie is discovered, will preload based on the appointement name, 
      * Setting value will get from global variable.*/ 

      if (BluetoothConstants.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) { 
       mService.enableTXNotification(); 
       mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(intent.getStringExtra(action)); 


        Handler handler = new Handler(Looper.getMainLooper()); 
        handler.post(new Runnable() { 
         @Override 
         public void run() { 
          mService.writeRXCharacteristic(value); 
         } 
        }); 
        Log.d("Sent Value", "Sent Value"); 

       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 

      } 

      // Action Data Available is thel listener which will be trigger when data is successfully send. 
      if (action.equals(UartService.ACTION_DATA_AVAILABLE)) { 

         Handler handler = new Handler(Looper.getMainLooper()); 
         handler.post(new Runnable() { 
          @Override 
          public void run() { 
           mService.disconnect(); 
          } 
         }); 


         // mService.disconnect(); 
         if (filterQueue.size() == 0) { 
          progressDlg.cancel(); 
          connectionQueue.clear(); 
         } 
         if (txValue[0] == 0) { 
         } else { 
         } 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 


       } 


       if (action.equals(UartService.DEVICE_DOES_NOT_SUPPORT_UART)) { 
        mService.disconnect(); 
        if (connectionQueue != null) { 
         connectionQueue.poll(); 
        } else { 
         progressDlg.cancel(); 
        } 
       } 
      } 
     } 
    }; 


private ScanCallback mScanCallback = new ScanCallback() { 

     @Override 
     public void onScanResult(int callbackType, ScanResult result) { 
    /* Connect to device found */ 
      Log.i("callbackType", String.valueOf(callbackType)); 

      BluetoothDevice device = result.getDevice(); 
      byte[] manufacturerData = BleUtils.getManufacturerAdvertisementData(result.getScanRecord().getBytes()); 

      Log.e(TAG, Arrays.toString(manufacturerData)); 
      if (device != null) { 
       if (result.getRssi() > -92 && device != null && device.getName() != null) { 
        { 
         if (device.getName().contains("-")) { 
          mLeDeviceListAdapter.addDevice(new BleDevice(device, result.getRssi(), manufacturerData)); 
          mLeDeviceListAdapter.notifyDataSetChanged(); 
          onDeviceFound(device); 
         } 
        } 
       } else { 
        if (mLeDeviceListAdapter != null) { 
         mLeDeviceListAdapter.clear(); 
        } 
       } 

      } else { 
       // adapter.resetColor(0); 
       //writeLine("No Device Detected"); 
      } 


     } 

     @Override 
     public void onBatchScanResults(List<ScanResult> results) { 
    /* Process a batch scan results */ 
      for (ScanResult sr : results) { 
       Log.i("Scan Item: ", sr.toString()); 
      } 
     } 
    }; 

次の私のlogcatです:

05-14 17:45:05.254 5018-5200/? E/BtGatt.GattService: [GSIM LOG]: gsimLogHandler: com.example.jinghu.smartmagazine, msg: MESSAGE_STOP_SCAN 
05-14 17:45:05.254 5018-5182/? E/BtGatt.ScanManager: default value of curScanSetting 0 is choosen 
05-14 17:45:08.064 5018-5115/? E/BluetoothRemoteDevices: aclStateChangeCallback: State:Connected to Device:F5:E8:84:5E:09:XX, linktype is 2 
05-14 17:45:08.094 5018-5352/? E/bt_btif: already has a pending command!! 
05-14 17:45:08.804 5018-5115/? E/bt_btif_gatt: bta_to_btif_uuid: Unknown UUID length 63201! 
05-14 17:45:09.434 24160-24160/com.example.jinghu.myapp E/UartService: mBluetoothGatt [email protected] 
05-14 17:45:09.604 5018-5352/? E/bt_btif: Connection doesnt exist. Drop indication! 
05-14 17:45:10.254 5018-5352/? E/bt_btm: btm_ble_get_search_if search_if=4 
05-14 17:45:10.254 5018-5352/? E/bt_btif: bta_gattc_mark_bg_conn unable to find the bg connection mask for: f5:e8:84:5e:09:a6 
05-14 17:45:10.294 5018-5352/? E/bt_btm: btm_sec_disconnected :: clear unused flags. p_dev_rec->sm4 : 0 
05-14 17:45:10.304 5018-5115/? E/BluetoothRemoteDevices: aclStateChangeCallback: State:DisConnected to Device:F5:E8:84:5E:09:XX 
05-14 17:45:10.304 3981-4166/? E/BluetoothEventManager: ACTION_ACL_DISCONNECTED 
05-14 17:45:10.324 5018-5352/? E/bt_btif: Register with GATT stack failed. 
05-14 17:45:10.324 5018-5352/? E/bt_btif: Register with GATT stack failed. 
05-14 17:45:10.324 5018-5352/? E/bt_btif: Register with GATT stack failed. 
05-14 17:45:10.344 5018-5018/? E/BluetoothPbapService: Device F5:E8:84:5E:09:A6 not waiting for authorization..Skipping cancel dialog... 
05-14 17:45:10.424 5018-5352/? E/bt_btif: Register with GATT stack failed. 
05-14 17:45:10.424 5018-5352/? E/bt_btif: Register with GATT stack failed. 
05-14 17:45:10.424 5018-5352/? E/bt_btif: Register with GATT stack failed. 
05-14 17:45:10.494 5018-5352/? E/bt_btif: Register with GATT stack failed. 
05-14 17:45:10.494 5018-5352/? E/bt_btif: Register with GATT stack failed. 
05-14 17:45:10.494 5018-5352/? E/bt_btif: Register with GATT stack failed. 
05-14 17:45:10.584 5018-5352/? E/bt_btif: Register with GATT stack failed. 
05-14 17:45:10.584 5018-5352/? E/bt_btif: Register with GATT stack failed. 
05-14 17:45:10.584 5018-5352/? E/bt_btif: Register with GATT stack failed. 
05-14 17:45:10.694 5018-5352/? E/bt_btif: Register with GATT stack failed. 
05-14 17:45:10.694 5018-5352/? E/bt_btif: Register with GATT stack failed. 
05-14 17:45:10.694 5018-5352/? E/bt_btif: Register with GATT stack failed. 
05-14 17:45:10.754 24160-24160/com.example.jinghu.myApp E/ViewRootImpl: sendUserActionEvent() mView == null 
05-14 17:45:12.054 5018-5352/? E/bt_btif: Register with GATT stack failed. 
05-14 17:45:12.054 5018-5352/? E/bt_btif: Register with GATT stack failed. 
05-14 17:45:12.054 5018-5352/? E/bt_btif: Register with GATT stack failed. 
05-14 17:45:12.064 5018-5352/? E/bt_btif: bta_gattc_deregister Deregister Failedm unknown client cif 
05-14 17:45:33.104 3528-4160/? E/Watchdog: [email protected] 1813 [05-14 17:45:33.111] 
05-14 17:46:03.104 3528-4160/? E/Watchdog: [email protected] 1814 [05-14 17:46:03.113] 
05-14 17:46:33.104 3528-4160/? E/Watchdog: [email protected] 1815 [05-14 17:46:33.115] 
+0

このエラーを引き起こすコードを送って、メインスレッドでこれを行うべきではなく、intentserviceまたはasynctaskを代わりに使用してください。 –

+0

@Mehran Zamani私は質問を編集しました –

+0

あなたは 'public boolean connect(final String address)'をどこで呼びますか? –

答えて

2

あなたが近い(呼び出す必要があります)あなたがそれを完了したら、BluetoothGattオブジェクト上で。そうしないと、最終的に利用可能なBluetoothGattオブジェクトがなくなります。

+0

しかし、close()を呼び出すと、bleは別のbleデバイスに接続されません。私のロジックはconnect => write data => disconnect =>別のデバイスに接続しています...リピート –

+0

AndroidはBluetoothGattオブジェクトの利用可能な最大数が非常に限られています。したがって、デバイスの接続を解除した後、各BluetoothGattオブジェクトを閉じる必要があります。 – Emil

+0

私が言ったように、私は接続ごとにBluetoothGattオブジェクトを閉じる関数を置く、このエラーは解決されています。ありがとう。 –

関連する問題