简体   繁体   English

Android蓝牙低功耗Gatt服务实现错误

[英]Android Bluetooth Low energy Gatt Service implementation error

I used android.bluetooth package in my project but i try to implement IBluetoothGatt for read and write characteristics. 我在项目中使用了android.bluetooth包,但是尝试实现IBluetoothGatt的读写特性。 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 IBluetoothGatt界面中的writeCharacteristic红色突出显示

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);
                }
            }

use this article by it is very useful . 通过这篇文章来使用它非常有用。 BLE 蓝牙

Comment if you have any issue 有任何问题请发表评论

Here is the code to connect to ble device. 这是连接到ble设备的代码。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM