Jimmy
Jimmy

Reputation: 147

Android BLE how to send message with GATT?

I'm trying to communicate with JDY-18 Bluetooth module using Android BLE (Java). Currently, I can connect with JDY-18 but I can't send message to it. I get problem while use this: mBluetoothGatt.getService this is some snippets of my program:
Firstly, I get UUIDList of JDY-18:

public void startScanning() {
    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
                ArrayList<String> permissions = new ArrayList<>();
                permissions.add(Manifest.permission.BLUETOOTH_SCAN);
                requestPermissionLauncher.launch(permissions.toArray(new String[0]));
                return;
            }
            btScanner.startScan(leScanCallback);
        }
    });
}


// Device scan callback.


private ScanCallback leScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                ArrayList<String> permissions = new ArrayList<>();
                permissions.add(Manifest.permission.BLUETOOTH_CONNECT);
                requestPermissionLauncher.launch(permissions.toArray(new String[0]));
                return;
            }
    

            if (result.getDevice().getName() != null) {

                if (result.getDevice().getName().equals("JDY-18")) {
                    targetDevAddr = result.getDevice().getAddress();
                    tvAppend(tv_read, "\r\nSet targetDevAddr to " + targetDevAddr);

//                    Add this to get UUIDList
                    serviceUUIDsList = getServiceUUIDsList(result);
                    stopScanning();
                }
            }
        }
    };

private List<UUID> getServiceUUIDsList(ScanResult scanResult)
    {
        List<ParcelUuid> parcelUuids = scanResult.getScanRecord().getServiceUuids();

        List<UUID> serviceList = new ArrayList<>();

        for (int i = 0; i < parcelUuids.size(); i++)
        {
            UUID serviceUUID = parcelUuids.get(i).getUuid();

            if (!serviceList.contains(serviceUUID))
                serviceList.add(serviceUUID);
        }

        return serviceList;
    }

I get 2 UUIDs:

0000ffe1-0000-1000-8000-00805f9b34fb

0000fee7-0000-1000-8000-00805f9b34fb

Then, I get service:

BluetoothGattService service = mBluetoothGatt.getService(UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb"));
if (service == null) {
        tvAppend(tv_read,"\r\nService not found!");
    }
service = mBluetoothGatt.getService(UUID.fromString("0000fee7-0000-1000-8000-00805f9b34fb"));
if (service == null) {
        tvAppend(tv_read,"\r\nService not found!");
    }

And output are:

Service not found!

Service not found!

I also set permission in Manifest file:

<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />

<uses-feature android:name="android.hardware.bluetooth" android:required="false"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="false"/>

Did I missing something ? Please help me to figure out the problem.

Thank you so much !

Upvotes: 0

Views: 2160

Answers (1)

Michael Kotzjan
Michael Kotzjan

Reputation: 2663

Before accessing a BLE device you need to follow a few steps:

  1. Find the device by calling bluetoothLeScanner.startScan(leScanCallback);. The found devices are reported through the callback leScanCallback.
  2. Connect to the device using bluetoothService.connect(deviceAddress);
  3. After you connected successfully call bluetoothGatt.discoverServices(); to find all available services the devices provides.

These steps are always necessary before reading or writing a characteristic. You are currently only doing step 1. Please refer to the official Bluetooth Low Energy Guide for more information and sample code snippets.

Upvotes: 1

Related Questions