diff options
author | Alex Blasche <alexander.blasche@digia.com> | 2014-10-29 13:04:49 +0100 |
---|---|---|
committer | Alex Blasche <alexander.blasche@digia.com> | 2014-11-04 09:47:24 +0100 |
commit | 7c12e73031c7dde3d422f9a2d0baeaa25b75fabb (patch) | |
tree | fcc6a333174cb741bc459855778ae0917e8f2cdc /src/android/bluetooth | |
parent | cb3054e2373dec8e800c1b5208555936ef3ae63b (diff) |
Android: Add ability to (dis)connect to BTLE devices on Android
and keep tracking the connection state
Change-Id: If4d05fa18c78802ae06096884fba78eed123e77f
Reviewed-by: Timur Pocheptsov <Timur.Pocheptsov@digia.com>
Reviewed-by: Alex Blasche <alexander.blasche@digia.com>
Diffstat (limited to 'src/android/bluetooth')
-rw-r--r-- | src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java | 73 |
1 files changed, 72 insertions, 1 deletions
diff --git a/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java b/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java index 8e36916a..6e760012 100644 --- a/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java +++ b/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java @@ -33,21 +33,38 @@ package org.qtproject.qt5.android.bluetooth; +import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; +import android.bluetooth.BluetoothGatt; +import android.bluetooth.BluetoothGattCallback; +import android.bluetooth.BluetoothProfile; +import android.util.Log; public class QtBluetoothLE { - + private static final String TAG = "QtBluetoothGatt"; private BluetoothAdapter mBluetoothAdapter; private boolean mLeScanRunning = false; + private BluetoothGatt mBluetoothGatt = null; + private String mRemoteGattAddress; + private BluetoothDevice mRemoteGattDevice = null; + + /* Pointer to the Qt object that "owns" the Java object */ long qtObject = 0; + Activity qtactivity = null; public QtBluetoothLE() { mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); } + public QtBluetoothLE(final String remoteAddress, Activity activity) { + this(); + qtactivity = activity; + mRemoteGattAddress = remoteAddress; + } + /* Returns true, if request was successfully completed */ @@ -79,5 +96,59 @@ public class QtBluetoothLE { }; public native void leScanResult(long qtObject, BluetoothDevice device, int rssi); + + + private BluetoothGattCallback gattCallback = new BluetoothGattCallback() { + + public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { + if (qtObject == 0) + return; + + int qLowEnergyController_State = 0; + //This must be in sync with QLowEnergyController::ControllerState + switch (newState) { + case BluetoothProfile.STATE_DISCONNECTED: + qLowEnergyController_State = 0; break; + case BluetoothProfile.STATE_CONNECTED: + qLowEnergyController_State = 2; + } + + //This must be in sync with QLowEnergyController::Error + int errorCode = 0; + switch (status) { + case BluetoothGatt.GATT_SUCCESS: + errorCode = 0; break; //QLowEnergyController::NoError + default: + Log.w(TAG, "Unhandled error code on connectionStateChanged: " + status); + errorCode = status; break; //TODO deal with all errors + } + leConnectionStateChange(qtObject, errorCode, qLowEnergyController_State); + } + }; + + public native void leConnectionStateChange(long qtObject, int wasErrorTransition, int newState); + + public boolean connect() { + if (mBluetoothGatt != null) + return mBluetoothGatt.connect(); + + mRemoteGattDevice = mBluetoothAdapter.getRemoteDevice(mRemoteGattAddress); + if (mRemoteGattDevice == null) + return false; + + mBluetoothGatt = mRemoteGattDevice.connectGatt(qtactivity, false, gattCallback); + if (mBluetoothGatt == null) + return false; + + return true; + } + + public void disconnect() { + if (mBluetoothGatt == null) + return; + + mBluetoothGatt.disconnect(); + } + } |