Comments (11)
Hi @Boodums
Interesting question, I haven't looked into getting services and characteristics ahead of connecting. My plugin does pass on the known services and characteristics when you connect to a device, but not ahead of time.
I'll look into it if it's possible and I'll get back to you soon.
I also saw that you added me on Discord, but I think I removed you by accident, you can send another request if you want a faster / direct response.
from unity-android-bluetooth-low-energy.
Thanks for the quick response! This capability was available on a Bluetooth interface that I used for an IOS app a few years ago but I don't know if Android supports it. I'm new to the Android platform. I sent another request through discord just in case, but I'll use GitHub for most correspondences. That way others who have the same question can benefit. Your Bluetooth interface seems pretty solid.
from unity-android-bluetooth-low-energy.
Hi @Boodums
It is possible to discover Services, but from what I'm seeing, Descriptors and Characteristics are only available if you have an existing connection. I made a branch called feature/scan-service-callback, which you can test out to see if it works for your case as well. The ExampleBleInteractor
and DeviceButton
in the Example/Scripts
show how it's used.
from unity-android-bluetooth-low-energy.
Thank you for adding this! I'll test it out later on today.
from unity-android-bluetooth-low-energy.
Thanks again for the time you put into this. I really do appreciate it. However this is not what I had in mind.
I'm looking for a way to only add buttons to the BlePanel for which a specified Service exists in the BLE device.
In practice, you could add a variable to the DeviceButton class (or ExampleBleInteractor class) called _preferredServiceUUID.
If this variable is null then the program would behave just like it does now. If the variable contains the UUID of a Service then the java code would only pass back devices that have that service.
It seems that you could accomplish this by passing a ScanFilter object to the startScan function in your java code.
You would use the ScanFilter.Builder.SetServiceData() function to populate the ScanFilter with the Service UUID.
More information can be found at the link below.
https://developer.android.com/guide/topics/connectivity/bluetooth/find-ble-devices
from unity-android-bluetooth-low-energy.
While I'd love to support such a wide range of functionalities from the Java environment into Unity, I'd rather keep it as generic as possible. Since all devices are passed, you can keep them in a list instead of having them instantly instantiated onto the panel and wait if the service you're looking for is passed along. From what I'm seeing there's two ways you could handle this:
- Since the plugin is pretty generic and passes along every device it finds without a filter, you can just put them on the panel if the service UUID matches the one you're looking for.
private string _prefferedServiceUUID = "3698-0000-82c9-4adb-90cd-792b53207775";
public void ScanForDevices()
{
if (!_isScanning)
{
_isScanning = true;
BleManager.Instance.QueueCommand(new DiscoverDevices(OnDeviceFound, OnDeviceServiceFound, _scanTime * 1000));
}}
private void OnDeviceFound(string device, string name)
{
if (device != null)
{
}
}
private void OnDeviceServiceFound(string device, string service)
{
if (device != null && service != null && string.Equals(service, _prefferedServiceUUID))
{
DeviceButton button = Instantiate(_deviceButton, _deviceList).GetComponent<DeviceButton>();
button.Show(device, name);
}
}
- Create a new command (
DiscoverSpecificDevices
) and let it inherit fromDiscoverDevices
, override theOnCommandReceived
and keep the same functionalities as the original, but have an extra check on if the service matches the one you're looking for (or passed along with the constructor) and only callback the devices that do.
public class DiscoverSpecificDevices : DiscoverDevices
{
private string _preferredServiceUUID = string.Empty;
public DiscoverSpecificDevices(
Action<string, string> onDeviceDiscovered,
Action<string, string> onServiceDiscovered,
string filterService,
int discoverTime = StandardDiscoverTime) : base(onDeviceDiscovered, onServiceDiscovered, discoverTime)
{
_prefferedServiceUUID = filterService;
}
public override bool CommandReceived(BleObject obj)
{
switch (obj.Command)
{
case "DiscoveredDevice":
// Do nothing, since we're filtering
break;
case "DiscoveredDeviceService":
if(string.Equals(_prefferedServiceUUID, obj.Service)
{
OnDeviceDiscovered?.Invoke(obj.Device, obj.Name);
}
break;
}
return string.Equals(obj.Command, "FinishedDiscovering");
}
}
(all of the code above is written from memory, so they could contain syntax errors and should be considered as pseudo code)
It could be that I'm mis-understanding your question, but I believe either one of these solutions would with as the answer if I read things right. If I did misunderstand your question, then I will take a look at using the ScanFilter
instead with a class described in solution 2, so that users can filter out which values they do and do not want.
from unity-android-bluetooth-low-energy.
The first solution works well!
The only issue is that the name of the device is not available at the time the button is created. I can figure that part out.
Thank You!
from unity-android-bluetooth-low-energy.
Awesome, glad that this solution worked for you. I'll merge it with development
branch when I feel like it's ready.
from unity-android-bluetooth-low-energy.
Sounds great. Since you're going to merge it in to the main branch you may want to pass the device name to OnDeviceServiceFound so the button can be properly labeled.
Thanks again!
from unity-android-bluetooth-low-energy.
Sounds good, I'll add that later this week.
from unity-android-bluetooth-low-energy.
Have you had any problems with scanning devices? I have the timeout set to 30 seconds and sometimes it still cannot find my device?
from unity-android-bluetooth-low-energy.
Related Issues (20)
- The uuid becomes invalid after the reconnection HOT 12
- gatt Descriptor is null HOT 2
- Handling Connection errors HOT 1
- license and android source code HOT 6
- How to Read Data From a Characteristic HOT 11
- Handling BLE with multiple unity scenes HOT 3
- Documentation needed HOT 15
- Null result for GetInstance method call on librarySingleton HOT 3
- Subscribe problem HOT 6
- Subscribing to multiple characteristics HOT 1
- Error (and maybe workaround) connecting to BLE device in Unity and Oculus Quest 2 HOT 12
- BLUETOOTH_SCAN permission issue (android 11+) HOT 2
- Make a new release that includes the latest fix for convenience HOT 1
- Permission missing HOT 1
- Error on write characteristic HOT 8
- Write characteristic issue with control characters? HOT 2
- No devices appearing on Windows 10 HOT 6
- BLE device not appear in Android 14 while scanning HOT 1
- Option to initiate disconnect from device in refactored code? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from unity-android-bluetooth-low-energy.