0

I used android.bluetooth package in my project but i try to implement IBluetoothGatt for read and write characteristics. But i have some problems like below

public final class BluetoothGatt implements BluetoothProfile {
    private static final String TAG = "BluetoothGatt";
    private static final boolean DBG = true;
    private static final boolean VDBG = false;

    private IBluetoothGatt mService;  // IBluetoothGatt red highlights. Some functions in IBluetoothGatt interface just work by put breakpoints.

    private BluetoothGattCallback mCallback;
    private int mClientIf;
    private boolean mAuthRetry = false;
    private BluetoothDevice mDevice;
    private boolean mAutoConnect;
    private int mConnState;
    private final Object mStateLock = new Object();
    private Boolean mDeviceBusy = false;
    private int mTransport;

    private static final int CONN_STATE_IDLE = 0;
    private static final int CONN_STATE_CONNECTING = 1;
    private static final int CONN_STATE_CONNECTED = 2;
    private static final int CONN_STATE_DISCONNECTING = 3;
    private static final int CONN_STATE_CLOSED = 4;

    private List<BluetoothGattService> mServices;

writeCharacteristic red highlights in IBluetoothGatt interface

public void onCharacteristicWrite(String address, int status, int handle) {
            if (VDBG) Log.d(TAG, "onCharacteristicWrite() - Device=" + address
                        + " handle=" + handle + " Status=" + status);

            if (!address.equals(mDevice.getAddress())) {
                return;
            }

            synchronized(mDeviceBusy) {
                mDeviceBusy = false;
            }

            BluetoothGattCharacteristic characteristic = getCharacteristicById(mDevice, handle);
            if (characteristic == null) return;

            if ((status == GATT_INSUFFICIENT_AUTHENTICATION
              || status == GATT_INSUFFICIENT_ENCRYPTION)
              && mAuthRetry == false) {
                try {
                    mAuthRetry = true;
                    mService.writeCharacteristic(mClientIf, address, handle,
                        characteristic.getWriteType(), AUTHENTICATION_MITM,
                        characteristic.getValue());
                    return;
                } catch (RemoteException e) {
                    Log.e(TAG,"",e);
                }
            }
Hilal
  • 74
  • 10
  • 1
    what you exactly want to do – Prashant Jaiswal Oct 04 '17 at 08:27
  • I want to communicate with ble device with writecharacteristic method. But writeCharacteristic method in Ibluetoothgatt interface(in android.bluetooth) "cannot resolve" and I can not do this. – Hilal Oct 04 '17 at 08:32
  • edit your code and write whole code of activity and service. You need to scan and connect to device before writing data in ble. – Prashant Jaiswal Oct 04 '17 at 08:38
  • Thanks for your answer. If i put breakpoint on some lines related to write characteristics, and debug step by step I write characteristics. Is problem in connecting with device? How can i edit? – Hilal Oct 04 '17 at 08:48
  • make sure you are connecting to device and writing to device at the same time. BLE devices are slow to take multiple requests at the same time. so first connect to device and write the data in device connection success callback. – Prashant Jaiswal Oct 04 '17 at 08:54
  • look at the below code – Prashant Jaiswal Oct 04 '17 at 09:03

1 Answers1

0

use this article by it is very useful . BLE

Comment if you have any issue

Here is the code to connect to ble device.

public boolean connect(final String address)
{

    if (mBluetoothAdapter == null || address == null)
    {
        Log.e(TAG, "BluetoothAdapter not initialized or unspecified address.");
        return false;
    }
    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if (device == null)
    {
        Log.e(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);

    return true;
}
  • I tried your suggestion device is connected but I have same error in BluetothGatt.java just like definition IBluetoothGatt(red), clientConnect is red in IBluetoothGattCallback method and I can not write anything to device. – Hilal Oct 04 '17 at 10:21
  • IBluetoothGatt is internal in Android and not part of the public API. You should therefore not open this file in your IDE. BluetoothGatt is also not a file you should open in your IDE. If you can't compile your project you should take another look at your SDK setup. – Emil Oct 04 '17 at 11:31