Comments (11)
Hi @Boodums
I've written a branch for reading and writing this weekend, I'm still testing it out, but you can check it it works for you in the feature/read-and-write
branch.
from unity-android-bluetooth-low-energy.
Thank you! I'll check it out this evening.
from unity-android-bluetooth-low-energy.
I apologize, my request was unclear. My Bluetooth knowledge is rusty. The Characteristic that I need to gather data on is a Notify Characteristic, not a Read Characteristic. It occurs every second. Your code on the main branch logs the characteristic data every second. The logs from Logcat are below. I need to subscribe to an event to collect the data.
2023/01/24 13:46:16.397 10030 10046 Info UnityAndroidBLE [3, -44, 0, 0, 0, 14, -84, -44, 0, 112, -69]
2023/01/24 13:46:17.435 10030 10046 Info UnityAndroidBLE [3, -44, 0, 0, 0, 14, -84, -44, 0, 112, -69]
2023/01/24 13:46:18.432 10030 10046 Info UnityAndroidBLE [3, -43, 0, 0, 0, 112, -83, -43, 0, -42, -63]
2023/01/24 13:46:19.410 10030 10046 Info UnityAndroidBLE [3, -42, 0, 0, 0, 127, -82, -42, 0, -68, -58]
2023/01/24 13:46:20.427 10030 10046 Info UnityAndroidBLE [3, -41, 0, 0, 0, 123, -81, -41, 0, 76, -53]
2023/01/24 13:46:21.425 10030 10046 Info UnityAndroidBLE [3, -41, 0, 0, 0, 123, -81, -41, 0, 76, -53]
2023/01/24 13:46:22.423 10030 10046 Info UnityAndroidBLE [3, -40, 0, 0, 0, 126, -80, -40, 0, -7, -49]
2023/01/24 13:46:23.420 10030 10046 Info UnityAndroidBLE [3, -39, 0, 0, 0, -112, -79, -39, 0, -19, -44]
2023/01/24 13:46:24.418 10030 10046 Info UnityAndroidBLE [3, -38, 0, 0, 0, -103, -78, -38, 0, -71, -39]
2023/01/24 13:46:25.415 10030 10046 Info UnityAndroidBLE [3, -37, 0, 0, 0, -73, -77, -37, 0, -29, -34]
2023/01/24 13:46:26.414 10030 10046 Info UnityAndroidBLE [3, -37, 0, 0, 0, -73, -77, -37, 0, -29, -34]
2023/01/24 13:46:27.411 10030 10046 Info UnityAndroidBLE [3, -37, 0, 0, 0, -73, -77, -37, 0, -29, -34]
from unity-android-bluetooth-low-energy.
Ah, if that's the case then you're looking for the SubscribeToCharacteristic
command. This has a similar setup as the ReadFromCharacteristic
command, but will stay running in the background after receiving a value. You'd want to set it up as follows:
// Replace "1101" and "2101" with your device's Service and Characteristic
_subscribeToCharacteristic = new SubscribeToCharacteristic(_deviceUuid, "1101", "2101", (byte[] data) => {
//Use the byte[] data
Debug.Log(data.Length);
});
BleManager.Instance.QueueCommand(_subscribeToCharacteristic);
Every command has different constructors, though undocumented, so I didn't touch on this one in my example code. Here's a link to the constructor that I'm using in the example above.
from unity-android-bluetooth-low-energy.
This is exactly what I need. I feel foolish for not figuring this out on my own.
However when I run the code I get the following error.
FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
at System.Convert.FromBase64CharPtr (System.Char* inputPtr, System.Int32 inputLength) [0x0004c] in <05089c1b6989455fb1452c627044bac4>:0
at System.Convert.FromBase64String (System.String s) [0x00025] in <05089c1b6989455fb1452c627044bac4>:0
at Android.BLE.BleObject.GetByteMessage () [0x00000] in <6952539eeeb640668626a3e8f723d3c4>:0
at Android.BLE.Commands.SubscribeToCharacteristic.CommandReceived (Android.BLE.BleObject obj) [0x000c2] in <6952539eeeb640668626a3e8f723d3c4>:0
at Android.BLE.BleManager.OnBleMessageReceived (Android.BLE.BleObject obj) [0x00095] in <6952539eeeb640668626a3e8f723d3c4>:0
at Android.BLE.BleAdapter.OnBleMessage (System.String jsonMessage) [0x00047] in <6952539eeeb640668626a3e8f723d3c4>:0
I'm using a newer version of the Unity Editor (2021.3.16f1). I wonder if this is the issue.
from unity-android-bluetooth-low-energy.
For reference the string that it's trying to convert from base64 is "CharacteristicValueChanged".
It's not a multiple of 4.
from unity-android-bluetooth-low-energy.
Ah, I'm not sure what branch you're on, but that was indeed a mistake on my part and was also fixed in the feature/read-and-write
branch.
I migrated the code from my thesis project wrong, so it's trying to convert the command to the byte[]
that's passed to the callback. This instead should be the base64Message
that's passed from the library to the Unity plugin.
If you're interested in the difference, this is the old implementation (main
and development
)
public byte[] GetByteMessage() => Convert.FromBase64String(command);
While it should have been (feature/read-and-write
)
public string Base64Message { get => base64Message; }
[SerializeField]
private string base64Message = string.Empty;
public byte[] GetByteMessage() => Convert.FromBase64String(base64Message);
from unity-android-bluetooth-low-energy.
I'll try it out.
BTW, in OnDeviceFound(string name, string device), the variables passed in are swapped. In other words, the name variable contains the device and vice versa.
from unity-android-bluetooth-low-energy.
Yeah I noticed that as well, should be fixed in the same branch that I mentioned above.
from unity-android-bluetooth-low-energy.
It works well. Thanks again!
from unity-android-bluetooth-low-energy.
Glad that the branch worked for you!
I'll merge it with development
and main
tomorrow.
from unity-android-bluetooth-low-energy.
Related Issues (20)
- 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
- Updating to newer Unity editor
- No devices found in one project but same scene works in another project HOT 12
- Unable to re-scan after device is found HOT 4
- Broken path to unity.jar HOT 1
- BluetoothDevice hasn't been discovered yet error when attempting to connect to a device HOT 2
- Can't properly connect or read characteristic from BLE sensor HOT 2
- Wrong command name in BleDevice.cs
- Feature: Include Android Library within Project/Package for 2022.2 and greater.
- Cannot scan or try out the plugin in Unity Version 2022.3.9 HOT 5
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.