Skip to content

Commit

Permalink
Merge pull request #6 from tahaHichri/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
tahaHichri authored Feb 19, 2019
2 parents 8902272 + 254e72a commit 34b2d2c
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 32 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,25 @@ Before adding the plugin to your projects, please take these points into conside
</ol>


## Getting Started
<u><i>Make sure that your target platform/device family satisfy the Prerequisites listed above</i></u>

<img src="https://i.imgur.com/XL5pKEv.jpg" />
There is no better way to understand how to integrate the use this plugin than seeing it in action!<br />
You can go ahead and clone <a href="https://github.com/tahaHichri/UE4-Bluetooth-Support-DEMO">this demo project</a>. After opeining it on your UE4, you can naviguate to the project settings and take a look at the Blueprints to learn how to call the different functionnalities.


## Change log
Milestones on the "Release" channel of this repository.
<ul>
<li>v4.19-4: Unified Scan node with optional filters<br /><ul>
<li>Scan all, and Scan by UUID are now unified an both support Async result callback, no need to call "getDiscoveredDevices" anymore.</li><li>Scan by MAC address added</li></ul></li>
<img src="https://i.imgur.com/kgeTB85.jpg" />
<li>v4.19-3: Async scan callback for "filterless" scan.</li>
</ul>



<!--<img src="https://i.imgur.com/XL5pKEv.jpg" />-->



Expand Down
12 changes: 7 additions & 5 deletions Source/BluetoothSupport/Private/Android/AndroidGateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jmethodID FAndroidGateway::ClearDiscoveredDevicesListMethod;

IsEnabledMethod = FJavaWrapper::FindMethod(Env, FJavaWrapper::GameActivityClassID, "isBluetoothEnabled", "()Z", false);
ScanBLEdevicesMethod = FJavaWrapper::FindMethod(Env, FJavaWrapper::GameActivityClassID, "startLEScan", "(I)V", false);
ScanByCharacteristicMethod = FJavaWrapper::FindMethod(Env, FJavaWrapper::GameActivityClassID, "scanByCharacteristic", "(ILjava/lang/String;)V", false);
ScanByCharacteristicMethod = FJavaWrapper::FindMethod(Env, FJavaWrapper::GameActivityClassID, "startFilteredScan", "(ILjava/lang/String;Ljava/lang/String;)V", false);
StopScanMethod = FJavaWrapper::FindMethod(Env, FJavaWrapper::GameActivityClassID, "stopLEScan", "()V", false);


Expand Down Expand Up @@ -133,18 +133,20 @@ jmethodID FAndroidGateway::ClearDiscoveredDevicesListMethod;



bool FAndroidGateway::ScanByCharacteristic(int32 scanTimeout, FString characteristicUUID)
bool FAndroidGateway::ScanByCharacteristic(int32 scanTimeout, FString serviceUUID, FString deviceAddr)
{
if (JNIEnv* Env = FAndroidApplication::GetJavaEnv())
{
jstring jStringParam = Env->NewStringUTF( (std::string( TCHAR_TO_UTF8(*characteristicUUID) )).c_str());
jstring jStringParam = Env->NewStringUTF( (std::string( TCHAR_TO_UTF8(*serviceUUID) )).c_str());

if (!jStringParam)
jstring jStringAddrParam = Env->NewStringUTF((std::string(TCHAR_TO_UTF8(*deviceAddr))).c_str());

if (!jStringParam || !jStringAddrParam)
{
UE_LOG(LogTemp, Fatal, TEXT("Could Not generate jstring from uuid"));
}

FJavaWrapper::CallVoidMethod( Env, FJavaWrapper::GameActivityThis, FAndroidGateway::ScanByCharacteristicMethod, scanTimeout, jStringParam);
FJavaWrapper::CallVoidMethod( Env, FJavaWrapper::GameActivityThis, FAndroidGateway::ScanByCharacteristicMethod, scanTimeout, jStringParam, jStringAddrParam);

Env->DeleteLocalRef(jStringParam);
return true;
Expand Down
4 changes: 2 additions & 2 deletions Source/BluetoothSupport/Private/Android/AndroidGateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class FAndroidGateway : public IBluetoothOperationsInterface
virtual bool IsBLESupported() override;
virtual bool IsScanning() override;

virtual bool ScanBLEdevices(int32 scanTimeout) override;
virtual bool ScanByCharacteristic(int32 scanTimeout, FString characteristicUUID) override;
virtual bool ScanBLEdevices(int32 scanTimeout) override;
virtual bool ScanByCharacteristic(int32 scanTimeout, FString serviceUUID, FString deviceAddress) override;

virtual void StopScan() override;

Expand Down
17 changes: 14 additions & 3 deletions Source/BluetoothSupport/Private/ScanDevicesCallback.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
#include "ScanDevicesCallback.h"

FString searchDeviceAddr;
FString searchService;
FString searchAddress;
int32 searchTimeout;
UScanDevicesCallback* UScanDevicesCallback::ScanNearbyDevices(int32 timeout)
UScanDevicesCallback* UScanDevicesCallback::ScanNearbyDevices(int32 timeout, const FString& serviceUUID, const FString& deviceAddress)
{
UScanDevicesCallback* Proxy = NewObject<UScanDevicesCallback>();
searchTimeout = timeout;
searchService = FString(serviceUUID);
searchAddress = FString(deviceAddress);
return Proxy;
}

void UScanDevicesCallback::Activete_imp()
{
UBluetoothAdapter::ScanBLEdevices(searchTimeout);
// characteristic is optional, scan without filters if not present
if (searchService.IsEmpty() && searchAddress.IsEmpty())
{
UBluetoothAdapter::ScanBLEdevices(searchTimeout);
}
else {
UBluetoothAdapter::ScanByCharacteristic(searchTimeout, searchService, searchAddress);
}

// call scan device start
}
4 changes: 2 additions & 2 deletions Source/BluetoothSupport/Public/Blueprint/BluetoothAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ bool UBluetoothAdapter::ScanBLEdevices(int32 scanTimeout)
}


bool UBluetoothAdapter::ScanByCharacteristic(int32 scanTimeout, FString characteristicUUID)
bool UBluetoothAdapter::ScanByCharacteristic(int32 scanTimeout, FString serviceUUID, FString deviceAddress)
{
//
#if PLATFORM_ANDROID
return TaDispatcher::Get().GetAndroidGatewayInterface()->ScanByCharacteristic(scanTimeout, characteristicUUID);
return TaDispatcher::Get().GetAndroidGatewayInterface()->ScanByCharacteristic(scanTimeout, serviceUUID, deviceAddress);
#endif
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions Source/BluetoothSupport/Public/Blueprint/BluetoothAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class UBluetoothAdapter : public UObject
// UFUNCTION(BlueprintCallable, Category = "Bluetooth Support Plugin", meta = (DisplayName = "Scan all devices"))
static bool ScanBLEdevices(int32 scanTimeout);

UFUNCTION(BlueprintCallable, Category = "Bluetooth Support Plugin", meta = (DisplayName = "Scan devices having characterisitc"))
bool ScanByCharacteristic(int32 scanTimeout, FString characteristicUUID);
// UFUNCTION(BlueprintCallable, Category = "Bluetooth Support Plugin", meta = (DisplayName = "Scan devices having characterisitc"))
static bool ScanByCharacteristic(int32 scanTimeout, FString serviceUUID, FString deviceAddress);


UFUNCTION(BlueprintCallable, Category = "Bluetooth Support Plugin")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class IBluetoothOperationsInterface
virtual bool IsScanning() = 0;

virtual bool ScanBLEdevices(int32 scanTimeout) = 0;
virtual bool ScanByCharacteristic(int32 scanTimeout, FString characteristicUUID) = 0;
virtual bool ScanByCharacteristic(int32 scanTimeout, FString characteristicUUID, FString addr) = 0;
virtual void StopScan() = 0;
virtual TArray<UBluetoothDevice*> GetDiscoveredDevices() = 0;
virtual void ClearDiscoveredDevicesList() = 0;
Expand Down
6 changes: 4 additions & 2 deletions Source/BluetoothSupport/Public/ScanDevicesCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ class UScanDevicesCallback : public UScanCallbackBase

public:


/**
* service UUID and MAC addr are optional filters
*/
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Scan Nearby Bluetooth Devices", ToolTip = "Discover all nearby broadcasting devices",
BlueprintInternalUseOnly = "true", WorldContext = "WorldContextObject"), Category = "Bluetooth Support Plugin")
static UScanDevicesCallback* ScanNearbyDevices(int32 timeout);
static UScanDevicesCallback* ScanNearbyDevices(int32 timeout, const FString& serviceUUID, const FString& deviceAddress);


virtual void Activete_imp() override;
Expand Down
57 changes: 43 additions & 14 deletions Source/BluetoothSupport/TaModuleAPL.xml
Original file line number Diff line number Diff line change
Expand Up @@ -268,27 +268,56 @@ import android.os.Bundle;


/**
* Called to scan for LE devices with a provided Characteristic UUID only
* Called to scan for LE devices with a provided Service UUID only
* The filtered scan will save unneeded extra treatment especially with
* a big number of devices around.
* @param uuid of the characteristic needed.
* @param uuid of the service needed.
* @param scanTimeoutMillisecond For how many milliseconds should I keep scanning?
* The loop is better be managed BP=side
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void scanByCharacteristic (int scanTimeoutMillisecond, String uuid)
public void startFilteredScan (int scanTimeoutMillisecond, final String uuidStr, final String deviceAddr)
{
//runOnUiThread(new Runnable()
//{
// public void run() {
// Toast.makeText(getApplicationContext(), uuidStr+&quot;|&quot;+deviceAddr, Toast.LENGTH_SHORT).show();
// }
//});

// prevent user from calling scan multiple times
if( isScanning() ) stopLEScan();

// taBluetoothLeScanner would be null if bluetooth is disabled
if ( !isBluetoothEnabled() || !isBluetoothLowEnergySupported() ) return;

ScanFilter scanFilter = null;

if (!uuidStr.equals(&quot;&quot;))
{

// We only want to scan for devices advertising our custom service
ScanFilter scanFilter = new ScanFilter.Builder()
.setServiceUuid(new ParcelUuid(UUID.fromString(uuid)))
UUID uuid;
try {
uuid = UUID.fromString(uuidStr);

scanFilter = new ScanFilter.Builder()
.setServiceUuid(new ParcelUuid(uuid))
.build();
} catch (IllegalArgumentException illArgExc)
{
// ignore
}

}
if (!deviceAddr.equals(&quot;&quot;))
{
scanFilter = new ScanFilter.Builder()
.setDeviceAddress(deviceAddr)
.build();
}

if (scanFilter == null ) return;

mScanCallback = new BtleScanCallback();

Expand All @@ -300,15 +329,15 @@ import android.os.Bundle;


mHandler.postDelayed(new Runnable() {
@Override
public void run() {
new Thread() {
@Override
public void run() {
stopLEScan();
}
}.start();
}
@Override
public void run() {
new Thread() {
@Override
public void run() {
stopLEScan();
}
}.start();
}
},scanTimeoutMillisecond);


Expand Down

0 comments on commit 34b2d2c

Please sign in to comment.