Bug? Failed to get gattService. the BluetoothGatt return with no services

b4x_android

New Member
Hi

I could not get the BT services list
the code line 65 return null Because code line 57 return BluetoothGatt with services size 0

I know for sure that the BT Device I am working with is working properly

This is my first task in a new job and it is very important for me to solve it as quickly as I can

I'm missing something but do not know what ...

Thanks a lot


My service:
public class TagService extends Service {
    String TAG = "TagService";

    public static boolean running;
    public e_status status;

    private BluetoothManager mBluetoothManager;
    private BluetoothAdapter mBtAdapter;
    private BluetoothLeScanner mScanner;
    private BluetoothDevice mDevice;
    private BluetoothGatt mBluetoothGatt;
    private BluetoothGattService mService;

    enum e_status {
        none, init, searchDevice, foundDevice, discoverServices, sendHeartBeat, finish
    }

    @Override
    public void onCreate() {
        super.onCreate();
        runThread();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        running = false;
    }

    @SuppressLint("MissingPermission")
    private void runThread() {
        status = e_status.none;
        running = true;

        new Thread(() -> {
            while (running && status != e_status.finish) {
                int interval=4000;

                try {
                    if (status == e_status.none) {
                        mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
                        mBtAdapter = mBluetoothManager.getAdapter();
                        setStatus(e_status.init);
                    }

                    if (status == e_status.init) {
                        mScanner = mBtAdapter.getBluetoothLeScanner();
                        ArrayList<ScanFilter> filters = new ArrayList<>();
                        ScanFilter tagFilter = new ScanFilter.Builder().setDeviceAddress(ConstParams.tagAddress).build();
                        filters.add(tagFilter);
                        ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
                        setStatus(e_status.searchDevice);
                        mScanner.startScan(filters, settings, getScanCallback());
                    }

                    if (status == e_status.foundDevice) {
                        mBluetoothGatt = mDevice.connectGatt(getApplicationContext(), false, getGattCallback());
                        if (mBluetoothGatt != null) {
                            setStatus(e_status.discoverServices);
                            mBluetoothGatt.discoverServices();
                        }
                    }

                    if (status == e_status.discoverServices) {
                        mService = mBluetoothGatt.getService(ConstParams.serviceUUID);
                        if (mService != null) {
                            setStatus(e_status.sendHeartBeat);
                            //
                            // this is no happen!!!
                            //
                            // TODO
                            //BluetoothGattCharacteristic charac = mService.getCharacteristic(ConstParams.serviceUUID);
                            //mBluetoothGatt.writeCharacteristic(characteristic);
                            //mService.addCharacteristic(charac, value));
                        }
                    }
                } catch (Exception ex) {
                    Log.e(TAG, "error" ,ex);
                    interval=30000;
                }

                sleep(interval);
            }
        }).start();
    }

    private ScanCallback getScanCallback() {
        return new ScanCallback() {
            @SuppressLint("MissingPermission")
            @Override
            public void onScanResult(int callbackType, ScanResult result) {
                super.onScanResult(callbackType, result);
                mDevice = mBtAdapter.getRemoteDevice(result.getDevice().getAddress());
                setStatus(e_status.foundDevice);
                mScanner.stopScan(this);
            }

            @Override
            public void onBatchScanResults(List<ScanResult> results) {
                super.onBatchScanResults(results);
            }

            @Override
            public void onScanFailed(int errorCode) {
                super.onScanFailed(errorCode);
                setStatus(e_status.init);
            }
        };
    }
    private void setStatus(e_status newStatus) {
        Log.i(TAG, "status: " + status + " -> " + newStatus);
        status = newStatus;
    }

    private BluetoothGattCallback getGattCallback() {
        return new BluetoothGattCallback() {
            @Override
            public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
                super.onPhyUpdate(gatt, txPhy, rxPhy, status);
                Log.i(TAG, "onPhyUpdate");
            }

            @Override
            public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
                super.onPhyRead(gatt, txPhy, rxPhy, status);
                Log.i(TAG, "onPhyRead");
            }

            @Override
            public void onConnectionStateChange(BluetoothGatt gatt, int status1, int newState) {
                super.onConnectionStateChange(gatt, status1, newState);
                Log.i(TAG, "onConnectionStateChange" + status + "," + newState);
            }

            @Override
            public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                super.onServicesDiscovered(gatt, status);
                Log.i(TAG, "onServicesDiscovered");
            }

            @Override
            public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicRead(gatt, characteristic, status);
                Log.i(TAG, "onCharacteristicRead");
            }

            @Override
            public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicWrite(gatt, characteristic, status);
                Log.i(TAG, "onCharacteristicWrite");
            }

            @Override
            public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
                super.onCharacteristicChanged(gatt, characteristic);
                Log.i(TAG, "onCharacteristicChanged");
            }

            @Override
            public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
                super.onDescriptorRead(gatt, descriptor, status);
                Log.i(TAG, "onDescriptorRead");
            }

            @Override
            public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
                super.onDescriptorWrite(gatt, descriptor, status);
                Log.i(TAG, "onDescriptorWrite");
            }

            @Override
            public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
                super.onReliableWriteCompleted(gatt, status);
                Log.i(TAG, "onReliableWriteCompleted");
            }

            @Override
            public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
                super.onReadRemoteRssi(gatt, rssi, status);
                Log.i(TAG, "onReadRemoteRssi");
            }

            @Override
            public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
                super.onMtuChanged(gatt, mtu, status);
                Log.i(TAG, "onMtuChanged");
            }

            @Override
            public void onServiceChanged(@NonNull BluetoothGatt gatt) {
                super.onServiceChanged(gatt);
                Log.i(TAG, "onServiceChanged");
            }
        };
    }

    private void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
 
Last edited:
Top