Comments (15)
Good that you were able to connect. After that you need to find the Characteristic that you want to write, read or subscribe to. If you have both the Service and Characteristic UUID, you can start to read or write data to your device.
Respectively, you'd use:
var command = new SubscribeToCharacterstic(device, service, characteristic, (byte[] data) => {
Debug.WriteLine(data.length);
});
To subscribe
var command = new ReadFromCharacteristic(device, service, characteristic, (byte[] data) => {
Debug.WriteLine(data.length);
});
To read
var command = new WriteToCharacteristic(device, service, characteristic, base64Message);
To write.
You'd then queue them up to the BleManager
, like this:
BleManager.Instance.QueueCommand(command);
Which will execute the command. Do note, that the normal BleCommands take a short-hand UUID, which is the differentiating part in this UUID: 0000[short-hand-uuid]-0000-1000-8000-00805f9b34fb
. If you're using complete custom UUID's, pass a true
boolean
to the commands as well.
from unity-android-bluetooth-low-energy.
Hi. What does subscribing exactly mean in this context? Is it needed? Thank you so much for the response by the way!
from unity-android-bluetooth-low-energy.
So within BLE you have a certain amount of actions you can do to get data or write data. These are Subscribing / Notifying, Writing and Reading. For now, these can only be done on Characteristic level. If you download a third party app like LightBlue, you can check what kind of Characteristics are available on the device that you're connecting to.
From there you can check on a Characteristic level what kind of action you can do on it. You'd then take that information and at the point where you've connected to the device, you'd queue up one of the commands that I mentioned in the previous comment.
from unity-android-bluetooth-low-energy.
Hello. What do all the parameters in the write command mean? (for example, the device from which the request is sent?) And is it necessary to use the abbreviated uuid? How can I shorten it?
from unity-android-bluetooth-low-energy.
The WriteToCharacteristic
command has two constructors, which only differ in the data
parameter. The data you send will always be converted to Base64
and read from the .jar
file as a byte array
to the device.
The parameters are explained in the summaries on the constructors themselves, but I'll give a brief rundown:
deviceAddress
: The MAC address of the device that you used to connect to the device withserviceAddress
: Either the shortened or full length UUID of the Service that you want to write to. Basically the service holds individual Characteristics and is used to label them together. If you're using a full-length then you'd passtrue
to thecustomGatt
parameter.characteristicAddress
: Either the shortened or full length UUID of the Characteristic you want to connect to. If you're using a full-length then you'd passtrue
to thecustomGatt
parameter.data
: either astring
or abyte[]
. In both cases, it'll be turned in toBase64
and send to the Android Java library.customGatt
: by defaultfalse
, but indicates if both theserviceAddress
andcharacteristicAddress
are full length UUID's or short hands.true
for full length,false
(default) for short.
If your UUID's adhere to the standard 0000[short-hand-uuid]-0000-1000-8000-00805f9b34fb
UUID template for BLE, then you can use the small part in the labeled [short-hand-uuid]
part for the parameters of WriteToCharacteristic
.
from unity-android-bluetooth-low-energy.
Can u give an example of using write command with customGate?(My arduino bluetooth ble module is HM-10)
from unity-android-bluetooth-low-energy.
Do you have an overview of the Characteristics available on your HM-10? That makes it easier to write such examples.
For now, I'll pick a generic address.
// 0x180F -> Battery Service
// 0x2A19 -> Battery Level [under Battery Service]
// Source: https://www.bluetooth.com/specifications/assigned-numbers/
var write = new WriteToCharacteristic(
_deviceUUID,
// Service UUID
"0000180f-0000-1000-8000-00805f9b34fb",
// Characteristic UUID
"00002A19-0000-1000-8000-00805f9b34fb",
// Data
new byte[] { 0x01 },
// Indicates that these are custom GATT UUID (though the example UUID's could be used with short-hand 16-bit UUID's)
customGatt: true
);
BleManager.Instance.QueueCommand(write);
from unity-android-bluetooth-low-energy.
Hi,thanks for answer.
I checked my HM-10 UUID:
In the Internet, I found which uuid corresponds to device name, service name and other. But when I entered these uuids, I got this code:
var write = new WriteToCharacteristic(
//Device UUID
"00002A00-0000-1000-8000-00805F9B34FB",
// Service UUID
"0000FFE0-0000-1000-8000-00805F9B34FB",
// Characteristic UUID
"0000FFE1-0000-1000-8000-00805F9B34FB",
// Data
new byte[] { 0x01},
// Indicates that these are custom GATT UUID (though the example UUID's could be used with short-hand 16-bit UUID's)
customGatt: true
//false-short,true-long
);
BleManager.Instance.QueueCommand(write);
(This code is'nt working ,it connect with scan button,but dont send commands)
Internet give me this:
ORANGE-Device UUID
GREEN-Service UUID
YELLOW-Characteristic UUID
Is this correct, or do these UUID belong to others?
from unity-android-bluetooth-low-energy.
Close, but not quite. Your Device isn't a UUID, it's a MAC address. My library will return a the found devices with their MAC address and name if it has one.
Also first the device has to be scanned, so the Android Java library has it cached. Then you have to connect to your device using it's MAC address (done through a button press in the example).
When the connection is successful, you can queue your commands to the BleManager
.
Also as you can see for your Service and Characteristic addresses, they follow the same pattern as the standard UUID: 0000[short-hand-UUID]-0000-1000-8000-00805f9b34fb
. So you could use FFE0
for the Service and FFE1
for the Characteristic, then leave customGatt
as false.
from unity-android-bluetooth-low-energy.
Could you please match my data with the Write To Characteristic command? (I'm beginner in unity)
from unity-android-bluetooth-low-energy.
I'm not blaming the fact that I have to write this out on the original issue at hand (the lack of documentation). I recommend picking up some tutorials on C# / .NET and Unity to get familiar with it, but overall, keep practicing!
I'll write out the WriteToCharacteristic
for you, but I don't know two parameters: the deviceAddress
and the data
. I don't know what you're going to send and I don't know the MAC address of your device.
var command = new WriteToCharacteristic(devieAddress, "FFE0", "FFE1", data);
BleManager.Instance.QueueCommand(command);
Again: this is only possible after you scanned for devices and connected to the one you want to send this command to.
from unity-android-bluetooth-low-energy.
Final code:
//(i put this code in my void(in "BleManager script"),he activates on "onClick")
var command = new WriteToCharacteristic("E0:62:34:D3:7A:64", "FFE0", "FFE1", "MESS"); BleManager.Instance.QueueCommand(command);
for other people:
1.
E0:62:34:D3:7A:64-MAC address of the device you want to connect to(u can find it in this app: https://play.google.com/store/apps/details?id=com.argonremote.bluetoothcontroller)
FFE0-service uuid-green square(check my photo in upper message)(app:
https://play.google.com/store/apps/details?id=com.macdom.ble.blescanner)
FFE0-characteristic uuid-yellow square(check my photo in upper message)(app;blescanner too)
(after connect u will see same,as my upper photo with squares)
"MESS"-your message
(i hope this messages will help other people)
from unity-android-bluetooth-low-energy.
good luck
from unity-android-bluetooth-low-energy.
@takeraparterer
Is your original question answered / solved? Else I'm gonna close the issue and you can re-open it if it's not.
from unity-android-bluetooth-low-energy.
@Velorexe go ahead and close it 👍
from unity-android-bluetooth-low-energy.
Related Issues (20)
- How to Read Data From a Characteristic HOT 11
- Scanning for Devices with Specific Gatt Services HOT 11
- Handling BLE with multiple unity scenes HOT 3
- 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
- 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
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.