Logic Machine Forum
Why my gateway can only connect to the Xiaomi band 2, - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Gateway (https://forum.logicmachine.net/forumdisplay.php?fid=10)
+--- Thread: Why my gateway can only connect to the Xiaomi band 2, (/showthread.php?tid=2003)



Why my gateway can only connect to the Xiaomi band 2, - 1114752670@qq.com - 03.04.2019

Why my gateway can only connect to the Xiaomi band 2, but not to other Bluetooth devices


RE: Why my gateway can only connect to the Xiaomi band 2, - admin - 03.04.2019

Which other devices?


RE: Why my gateway can only connect to the Xiaomi band 2, - 1114752670@qq.com - 03.04.2019

The Bluetooth adapter I use is CSR 4.0. I want to use it to connect other devices, such as CC2541 and CC2640. I can search for the Bluetooth of CC2541 and CC2640 in the BLE interface, but it just can't connect. The code I use is as follows. Hope to come to a big man to point me out, I have been troubled by this problem for a long time.

require('ble')
ble.up()
sock =ble.sock()
log(sock)
ble.settimeout(sock, 10)
res =ble.connect(sock,"78:04:73:BF:C4:E5",true)
log(res)
rbattery = ble.sockreadhnd(sock,0x47) or ''
battery = rbattery:byte(2)
rtime = ble.sockreadhnd(sock,45) or ''
time = {hour=rtime:byte(5), minute=rtime:byte(6), second=rtime:byte(7)}
log(rtime)
log(time)
log(rbattery)
log(battery)
rssi = ble.getrssi("78:04:73:BF:C4:E5")
log(rssi)
ble.close(sock)


RE: Why my gateway can only connect to the Xiaomi band 2, - admin - 03.04.2019

Try connecting without using a random mac:
Code:
res, err = ble.connect(sock,"78:04:73:BF:C4:E5")
log(res, err)



RE: Why my gateway can only connect to the Xiaomi band 2, - 1114752670@qq.com - 03.04.2019

Log it out as follows
* arg: 1
* nil
* arg: 2
* string: connect failed

Thank you very much. My problem has been solved.

But why does printing show that the connection failed。


RE: Why my gateway can only connect to the Xiaomi band 2, - admin - 03.04.2019

Maybe you have too many connections open at once?
For more info you can also log error number:
Code:
res, err, errno = ble.connect(sock,"78:04:73:BF:C4:E5")
log(res, err, errno)



RE: Why my gateway can only connect to the Xiaomi band 2, - 1114752670@qq.com - 03.04.2019

I've learned that the first time I made a successful connection, and later when I reconnected, because the connection was successful, I would print out the connection failure.
* arg: 1
* bool: true
* arg: 2
* nil
* arg: 3
* nil

Thank you very much. I didn't expect it was because of such a simple question.


RE: Why my gateway can only connect to the Xiaomi band 2, - 1114752670@qq.com - 09.04.2019

rbattery = ble.sockreadhnd(sock,0x47) or '' 

I want to ask specifically how this function is implemented when extracting the data I want. For example (sock, 0x47) in why 0x47 must be this, what basis.

I want to figure out a function                x = ble. sockreadhnd (sock, 22) or''                         about extracting data from Bluetooth.


RE: Why my gateway can only connect to the Xiaomi band 2, - admin - 09.04.2019

0x47 is two byte attribute handle, which is specific to a device to which you are connecting


RE: Why my gateway can only connect to the Xiaomi band 2, - 1114752670@qq.com - 10.04.2019

(09.04.2019, 14:19)admin Wrote: 0x47 is two byte attribute handle, which is specific to a device to which you are connecting
How do I get the two byte attribute handle。For example, I use an BLE Scanner APP,How do I get what the two byte attribute handle I want in this App。


RE: Why my gateway can only connect to the Xiaomi band 2, - 1114752670@qq.com - 10.04.2019

(09.04.2019, 12:39)1114752670@qq.com Wrote: rbattery = ble.sockreadhnd(sock,0x47) or '' 

I want to ask specifically how this function is implemented when extracting the data I want. For example (sock, 0x47) in why 0x47 must be this, what basis.

I want to figure out a function                x = ble. sockreadhnd (sock, 22) or''                         about extracting data from Bluetooth.

How do I get the two byte attribute handle。For example, I use an BLE Scanner APP,How do I get what the two byte attribute handle I want in this App。


RE: Why my gateway can only connect to the Xiaomi band 2, - admin - 10.04.2019

BLE Scanner does not provide handle numbers. You can use Linux with any USB BLE dongle: https://elinux.org/RPi_Bluetooth_LE


RE: Why my gateway can only connect to the Xiaomi band 2, - 1114752670@qq.com - 11.04.2019

(10.04.2019, 13:12)admin Wrote: BLE Scanner does not provide handle numbers. You can use Linux with any USB BLE dongle: https://elinux.org/RPi_Bluetooth_LE


Okay, thank you. But I found that if the handle pointed to a notification class of information, as if the data could not be obtained on the gateway, why did this happen? I can get all the other readable data. Is there any special statement about Bluetooth notification in LUA code?


RE: Why my gateway can only connect to the Xiaomi band 2, - admin - 11.04.2019

Notification is bit different. First you need to send a subscribe message and then read from device using sockreadnotify. Have a look at ti-sensortag and mio-alpha profiles:
https://forum.logicmachine.net/showthread.php?tid=1199


RE: Why my gateway can only connect to the Xiaomi band 2, - 1114752670@qq.com - 18.04.2019

(11.04.2019, 06:11)admin Wrote: Notification is bit different. First you need to send a subscribe message and then read from device using sockreadnotify. Have a look at ti-sensortag and mio-alpha profiles:
https://forum.logicmachine.net/showthread.php?tid=1199


require('ble')
ble.up()
sock =ble.sock()               
ble.settimeout(sock, 10)   
res=ble.connect(sock,"00:81:F9:21:B7:AE")  
err = ble.sockwritereq(sock,39,0x0001)
x = ble.sockreadhnd(sock,39) or ''  
a = x:byte(1)
b = x:byte(2)
c = x:byte(3)
d = x:byte(4)
e = x:byte(5)
f = x:byte(6)
g = x:byte(7)
h = x:byte(8)
i = x:byte(9)
j = x:byte(10)
k = x:byte(11)
m = x:byte(12)
n = x:byte(13)
k = x:byte(14)
o = x:byte(15)
p = x:byte(16)
q = x:byte(17)
log(sock,err,x,a,b,c,d,e,f,g,h,i,j,k,m,n,o,p,q)
ble.close(sock)



I now know that to enable notifications, I can get data, such as err = ble. sockwritereq (sock, 39,0x0001). But now there's a question, why do I write and read functions at the same time, print out all the data is nil. I still have data when I only have a read function.


RE: Why my gateway can only connect to the Xiaomi band 2, - admin - 18.04.2019

You need to use sockreadnotify instead of sockreadhnd after you've enabled notifications.


RE: Why my gateway can only connect to the Xiaomi band 2, - 1114752670@qq.com - 28.04.2020

(18.04.2019, 09:53)admin Wrote: You need to use sockreadnotify instead of sockreadhnd after you've enabled notifications.
After a long time I came back to this problem. I failed to use ble to read the notification information. Can you provide me with a similar code for reference? I can't thank you enough!


RE: Why my gateway can only connect to the Xiaomi band 2, - admin - 30.04.2020

As already mentioned, check ti-sensortag and mio-alpha profiles that use notifications: https://forum.logicmachine.net/showthread.php?tid=1199