yoon0506
yoon0506

Reputation: 33

(java, bluetooth-lowenergy, ble) how to get data from android device via BLE

enter image description here

i'm beginner of BLE(Bluetooth-LowEnergy). I wanted to get data from android device via BLE.

I've already read about characteristic in Google Document. and i've already searched on google too.

my device didn't respond to my request byte code. i think it's because i set wrong characteristics. cus i think i didn't understand about characteristics perfectly.

does anybody help me to set right characteristics please ?

Here's custom Uuid(it's better to see added Images on the top.)

CUSTOM SERVICE
0783b03e-8535-b5a0-7140-a304d2495cb7

NOTIFY Uuid : 0783B03E-8535-B5A0-7140-A304D2495CB8
Read Uuid : 00002a19-0000-1000-8000-00805f9b34fb
Write Uuid : 0783b03e-8535-b5a0-7140-a304d2495cba

and Here's Uuid I set

    public final UUID serviceUuid = UUID.fromString("0783b03e-8535-b5a0-7140-a304d2495cb7");
    public final UUID notifyUuid = UUID.fromString("0783b03e-8535-b5a0-7140-a304d2495cb8");
    public final UUID readUuid = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
    public final UUID writeUuid = UUID.fromString("0783b03e-8535-b5a0-7140-a304d2495cba");

and Here's my code

BluetoothHandler.java
 targetGattCharacteristic = targetGattService.getCharacteristic(Define.GetInstance().notifyUuid);
        BluetoothGattCharacteristic readGattCharacteristic = targetGattService.getCharacteristic(Define.GetInstance().notifyUuid);

        if (readGattCharacteristic != null) {
            mBleService.setCharacteristicNotification(readGattCharacteristic, true);

        } else {
            callInterface();
            return;
        }
(BluetoothService.java)
    public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                              boolean enabled) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }

        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

        BluetoothGattDescriptor gD = new BluetoothGattDescriptor(UUID.fromString(Define.GetInstance().readUuid.toString()),  BluetoothGattDescriptor.PERMISSION_WRITE | BluetoothGattDescriptor.PERMISSION_READ);
        characteristic.addDescriptor(gD);
        if (Define.GetInstance().notifyUuid.equals(characteristic.getUuid())) {
            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                    UUID.fromString(Define.GetInstance().readUuid.toString()));
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            mBluetoothGatt.writeDescriptor(descriptor);

        }
    }

Upvotes: 0

Views: 512

Answers (1)

yoon0506
yoon0506

Reputation: 33

i solved it by referring to this site. https://medium.com/@avigezerit/bluetooth-low-energy-on-android-22bc7310387a

Upvotes: 1

Related Questions