I am using Bluez 5.50; 5.48 was the version acquire-write became officially (non-experimentally) supported. Am I using the acquire-write / write feature properly?
On my Raspberry Pi Zero W:
pi#raspberrypi:~ $ bluetoothctl
[bluetooth]# power on
Changing power on succeeded
[bluetooth]# scan on
Discovery started
[bluetooth]# connect B8:27:E8:72:33:7A
Attempting to connect to B8:27:E8:72:33:7A
Connection successful
[DSD TECH]# menu gatt
[DSD TECH]# select-attribute 0000ffe1-0000-1000-8000-00805f9b34fb
[DSD TECH:/service0010/char0011]# acquire-write
[CHG] Attribute /org/bluez/hci0/dev_B8_27_E8_72_33_7A/service0010/char0011 WriteAcquired: yes
AcquireWrite success: fd 7 MTU 23
[DSD TECH:/service0010/char0011]# write("h")
[DSD TECH:/service0010/char0011]# write('h')
[DSD TECH:/service0010/char0011]# write(h)
[DSD TECH:/service0010/char0011]# write(0x68)
On my Arduino Nano connected to HM-10:
void setup()
{
Serial.begin(9600);
}
void loop() {
delay(10);
if (Serial.available())
{
char c = Serial.read();
Serial.println("Read value");
}
}
I use the iOS app named "Serial" which connects to my HM-10. I send the letter h over and read "Read value" on the arduino serial monitor. I do this directly before and after using the raspberry pi, on the same arduino serial monitor. The raspberry pi notifications never come thru. Yes, I am certain it is the same HM-10. It is the only HM-10 I have powered on in BLE range, and I cannot connect to the UUID while my phone is connected. (Actually, I submit connect B8:27:E8:72:33:7A to the raspberry pi, and keeps trying, and then disconnect from phone, and rpi automatically connects).
Thanks in advance!
When using gatt services with bluetoothctl, leave off the parentheses and just use:
write 0x68
acquire-write returns a file descriptor that can be used for writing to the GATT characteristic, usage of write will be locked. so either use acquire-write (which requires a write-without-response flag) or use write.
void WriteValue(array{byte} value, dict options)
Issues a request to write the value of the
characteristic.
Possible options: "offset": Start offset
"device": Device path (Server only)
"link": Link type (Server only)
"prepare-authorize": boolean Is prepare
authorization
request
Possible Errors: org.bluez.Error.Failed
org.bluez.Error.InProgress
org.bluez.Error.NotPermitted
org.bluez.Error.InvalidValueLength
org.bluez.Error.NotAuthorized
org.bluez.Error.NotSupported
fd, uint16 AcquireWrite(dict options) [optional]
Acquire file descriptor and MTU for writing. Usage of
WriteValue will be locked causing it to return
NotPermitted error.
For server the MTU returned shall be equal or smaller
than the negotiated MTU.
For client it only works with characteristic that has
WriteAcquired property which relies on
write-without-response Flag.
To release the lock the client shall close the file
descriptor, a HUP is generated in case the device
is disconnected.
Note: the MTU can only be negotiated once and is
symmetric therefore this method may be delayed in
order to have the exchange MTU completed, because of
that the file descriptor is closed during
reconnections as the MTU has to be renegotiated.
Possible options: "device": Object Device (Server only)
"MTU": Exchanged MTU (Server only)
"link": Link type (Server only)
Possible Errors: org.bluez.Error.Failed
org.bluez.Error.NotSupported
Related
I've connected my sim808 module to a HC05 bluetooth module with SPP profile.
Hc05 UART config is 9600 , no parity,1 stop.
i'm connecting with these at commands:
AT+BTPOWER=1
AT+BTCONNECT=1,4 // I have already paired two devices
when i send message printf("0adch=0&temp=27&hu=14") via HC05 to SIM808 , it breaks message to multiple parts like below (I've connected SIM808 to PC via a usb-ttl convertor,so i can see messages on sim RX port) :
+BTSPPDATA: 1,1,0
+BTSPPDATA: 1,10,adch=0&tem
+BTSPPDATA: 1,10,p=27&hu=14
I noticed that the method onConnectionStateChange() is not invariably invoked (or just not invoked on time) when a peripheral is non-manually disconnected like for example when the peripheral is powered off. Is there a way to get the connection state of a connected BLE peripheral manually versus just waiting for the onConnectionStateChange() to fire? I tried using BluetoothManager#getConnectionState but this method seems to be accessing the connection state updated by whatever thread is calling onConnectionStateChange(), and does not actually ask the device if it's connected. In other words, BluetoothManager#getConnectionState just returns false if onConnectionStateChange() hasn't been called yet.
Here is my isConnected method
public boolean isConnected(){
// If the device has never connected, it's gatt service is null.
if(mGatt != null){
BluetoothManager btm =
(BluetoothManager)
MainActivity.mMainActivity.getSystemService(Context.BLUETOOTH_SERVICE);
int state = btm.getConnectionState(mGatt.getDevice(), BluetoothProfile.GATT);
return state == 2;
}
// The gat service is null, the device is not connected, return false.
return false;
}
onConnectionStateChange is being called as soon as the Bluetooth controller reports to the host Bluetooth stack that the link has been terminated. If you suddenly power off the peripheral without gracefully disconnecting, the connection will remain until the supervision timeout triggers. The default value of that was just changed from 20 seconds to 5 seconds in the latest Android versions because people have complained it was too long. The default on iOS is 0.72 seconds. On Android you can manually lower it by doing a connection parameter update request from the peripheral.
How to set scan parameters and disable the filter duplicates, so that I can receive all advertisement from the controller ?
StartDiscovery API only notifies, whenever new device is identified but I want to set filter duplicate to disabled, so that I can receive each and every advertisement from the controller?.
But I didn't find any DBus API to set the scan parameters nor set scan enable API.
My Goal is simple I need get notify each advertisement received on controller to my client app, How can I do that with Bluez?
Note : I might missing some basic points because new to bluetooth and bluez.
In the below output you can see that the Device "Parthiban" is already present under /org/bluez/hci0/dev_44_D8_84_02_A3_17. I have started the scan which in turn triggers "StartDiscovery()" method and the same device is updated with RSSI value.
root#mx6q:~# bluetoothctl
[NEW] Controller 5C:F3:70:6E:26:02 Adapter [default]
**[NEW] Device 44:D8:84:02:A3:17 Parthiban**
[NEW] Device 2C:F0:A2:26:D7:F5 iPhone tteam
[NEW] Device 78:F8:82:10:E7:0C Nexus 5X
[bluetooth]# scan on
Discovery started
[CHG] Controller 5C:F3:70:6E:26:02 Discovering: yes
**[CHG] Device 44:D8:84:02:A3:17 RSSI: -52**
So you can use "g_dbus_connection_signal_subscribe" on the interface "org.freedesktop.DBus.Properties" for "PropertiesChanged" signal.
while(g_variant_iter_loop(&iter, "{sv}", &property, &value)) {
if(strcasecmp(property, "RSSI") == 0) {
/*
* - Extract the device address from object path
* /org/bluez/hci0/dev_44_D8_84_02_A3_17
* - Play with the device
*/
}
I have previously paired with a Bluetooth device that supports RFCOMM.
When my app is opened I continuously try to connect to the device by opening the RFCOMM. This way my app automatically connects when the device comes in range.
deviceInfoCollection = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
LogData(String.Format("Number of mldp devices is {0}", deviceInfoCollection.Count));
foreach (DeviceInformation deviceInfo in deviceInfoCollection)
{
LogData(String.Format("ID:{0}, NAME:{1}", deviceInfo.Id, deviceInfo.Name));
}
Then run this on a timer:
try
{
// The first time this method is invoked by a store app, it should be called
// from a UI thread in order to display the consent prompt
// https://msdn.microsoft.com/en-us/windows.devices.bluetooth.rfcomm.rfcommdeviceservice.fromidasync
RfcommDeviceService rfcommService = await RfcommDeviceService.FromIdAsync(deviceInfo.Id);
LogData(String.Format("ID:{0}, NAME:{1}", deviceInfo.Id, deviceInfo.Name));
}
catch (Exception)
{
LogData(String.Format("Can not request rfcomm service from device ID:{0}, NAME:{1}", deviceInfo.Id, deviceInfo.Name));
}
Is there any way to query when the device is in range , rather than trying to connect? I would prefer to only attempt connection when the device is in range.
For RFCOMM (BT2.0, BT2.1) you can run a device enumeration periodically, see also Get bluetooth devices in range
However your actual implementation with a connection attempt may work a little better.
For Bluetooth 4.0, you can listen to the advertisements of the BT module, see also https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BluetoothAdvertisement
If you're talking to an embedded device (e.g. some robot, or homebrew appliances using RFCOMM) I am afraid there is no better solution than what you're doing.
If you're taking to a phone (which supports both BT4.0 and BT2.1) you can use the BT4 advertisements to signal the proximity of the device, then connect via RFCOMM.
Environment: Bluez 5.14, Linux 3.1, USB Plugable BLE radio, TI BLE keyfob (CC2541 dev kit)
Linux Device <---hci----> USB BLE Radio
We enabled key press events on TI keyfob using gatttool and started to listen for events
gatttool -b [hardware ID] --char-write-req -a [handle] -n [value] --listen
(gatttool -b 90:59:AF:09:E1:5D --char-write-req -a 0x0048 -n 0100 --listen)
Pressing buttons on the keyfob and see these events
Notification handle = 0x0047 value: 02
Notification handle = 0x0047 value: 00
Notification handle = 0x0047 value: 02
Hence we can receive the key press events from the Keyfob through the Bluez stack
Objective:
We need to catch the GATT Disconnect Event i.e. When we remove the battery from the keyfob sooner or later the GATT connection is broken. We would like to receive a disconnect event from Bluez stack. Bluez has this capability since Android supports GATT disconnect event which is built over Bluez.
Question:
How do we receive the GATT Disconnect event using Bluez command line hcitool/gatttool or Bluez API.
Watch for G_IO_HUP and shutdown gracefully.
chan = gatt_connect(opt_src, opt_dst, opt_dst_type, opt_sec_level,
opt_psm, opt_mtu, connect_cb, &gerr);
if (chan == NULL) {
log_printf(LOG_LEVEL_ERROR,"%s: chan is NULL\n",__func__);
log_printf(LOG_LEVEL_ERROR,"%s\n", gerr->message);
g_error_free(gerr);
g_main_loop_quit(event_loop);
} else {
log_printf(LOG_LEVEL_INFO,"Connected to %s\n",opt_dst);
g_io_add_watch(chan, G_IO_HUP, channel_watcher, NULL);
}