Android蓝牙低能耗Gatt服务实施错误

时间:2017-10-04 08:23:27

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

我在我的项目中使用了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界面中的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);
                }
            }

1 个答案:

答案 0 :(得分:0)

使用这篇文章非常有用。 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;
}