BluetoothGattCallback returns 129 & # 8594; GATT_INTERNAL_ERROR for a beacon, but returns 0 for phones

I call on my bluetooth adapter mBluetoothAdapter.startDiscovery(); which then returns me a list of BluetoothDevices. When I click on 1 of these devices, I do this:

  mBluetoothGatt = bluetoothDevice.connectGatt(MainActivity.this, false, mGattCallback); 

Where is my callback:

  private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { mConnectionState = STATE_CONNECTED; Log.i(TAG, "Connected to GATT server."); Log.i(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices()); List<BluetoothGattService> listBGS = mBluetoothGatt.getServices(); Log.i("","list size: " + listBGS.size()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { mConnectionState = STATE_DISCONNECTED; Log.i(TAG, "Disconnected from GATT server."); } } @Override // New services discovered public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { Log.w(TAG, "onServicesDiscovered GATT_SUCCESS: " + status); List<BluetoothGattService> listBGS = mBluetoothGatt.getServices(); Log.i("","list size: " + listBGS.size()); } else { Log.w(TAG, "onServicesDiscovered received: " + status); } } @Override // Result of a characteristic read operation public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { Log.w(TAG, "onCharacteristicRead GATT_SUCCESS: " + status + " / char: " + characteristic); } } }; 

Now when I try to use it on phones, I get STATE_CONNECTED on onConnectionStateChange and then call DiscoverServices and I return GATT_SUCCESS onServicesDiscoveres. Therefore, I can take uuids from my services. BUT, when I try this on the lighthouse, I return STATE_CONNECTED, and when I call DiscoverServices, I return the status code: 129 β†’ GATT_INTERNAL_ERROR Why does this happen for my lighthouses? Is there any other way how can I use uuid devices? I just need a UUID to be able to do this afterwards for beaconManager:

  try { beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null)); } catch (RemoteException e) { } 

EDIT

I know that the lighthouse should ask me to enter the code, but this is not so. What for? I tried to add this code:

  final IntentFilter pairingRequestFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST); pairingRequestFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY - 1); registerReceiver(mPairingRequestRecevier, pairingRequestFilter); 

But still I do not receive a request to pair and enter my passkey

This is my paired RequestReceiver:

  private final BroadcastReceiver mPairingRequestRecevier = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(intent.getAction())) { final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR); if (type == BluetoothDevice.PAIRING_VARIANT_PIN) { device.setPin(intToByteArray(123456)); abortBroadcast(); } else { Log.e("","Unexpected pairing type: " + type); } } } }; 
+7
android bluetooth service-discovery bluetooth-lowenergy
source share
1 answer

After several hours of trial and error, I managed to find a way to make it work as follows: 1. I start the opening the same way, but when I select a bluetooth device, I ask you to connect it with it as follows:

  private void pairDevice(BluetoothDevice device) { try { Method method = device.getClass().getMethod("createBond", (Class[]) null); method.invoke(device, (Object[]) null); } catch (Exception e) { e.printStackTrace(); } } 

I have an ACTION_BOND_STATE_CHANGED recipient:

  private final BroadcastReceiver mPairReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR); final int prevState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR); if (state == BluetoothDevice.BOND_BONDED && prevState == BluetoothDevice.BOND_BONDING) { Log.i("","Paired"); mBluetoothGatt = btDevice.connectGatt(MainActivity.this, false, mGattCallback); } else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDED){ Log.i("","Unpaired"); } } } }; 

What I am registering like this:

  IntentFilter intent = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED); registerReceiver(mPairReceiver, intent); 

And only after that I try to connect to my BluetoothGattCallback. Therefore, I can open services, etc.

+3
source share

All Articles