【问题标题】:Android Bluetooth Low energy Gatt Service implementation errorAndroid 蓝牙低功耗 Gatt 服务实现错误
【发布时间】:2017-10-04 08:23:26
【问题描述】:

我在我的项目中使用了 android.bluetooth 包,但我尝试实现 IBluetoothGatt 以实现读写特性。但我有一些问题,如下所示

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;

在 IBluetoothGatt 界面中写入特征红色高亮

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

【问题讨论】:

  • 你到底想做什么
  • 我想用 writecharacteristic 方法与 ble 设备通信。但是 Ibluetoothgatt 接口(在 android.bluetooth 中)中的 writeCharacteristic 方法“无法解析”,我不能这样做。
  • 编辑您的代码并编写完整的活动和服务代码。在ble中写入数据之前,您需要扫描并连接到设备。
  • 感谢您的回答。如果我在与写入特性相关的某些行上放置断点,并逐步调试,我会编写特性。与设备连接有问题吗?我该如何编辑?
  • 确保您正在连接设备并同时写入设备。 BLE 设备同时接受多个请求的速度很慢。所以先连接设备,在设备连接成功回调中写入数据。

标签: android bluetooth-lowenergy gatt characteristics bluetooth-gatt


【解决方案1】:

使用这篇文章非常有用。 BLE

有什么问题可以留言

这是连接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;
}

【讨论】:

  • 我试过你的建议设备已连接,但我在 BluetothGatt.java 中也有同样的错误,就像定义 IBluetoothGatt(red),clientConnect 在 IBluetoothGattCallback 方法中是红色的,我无法向设备写入任何内容。
  • IBluetoothGatt 是 Android 内部的,不是公共 API 的一部分。因此,您不应在 IDE 中打开此文件。 BluetoothGatt 也不是您应该在 IDE 中打开的文件。如果你不能编译你的项目,你应该再看看你的 SDK 设置。
猜你喜欢
  • 2015-02-17
  • 2014-02-25
  • 1970-01-01
  • 1970-01-01
  • 2014-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多