This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Why my gateway can only connect to the Xiaomi band 2,
#1
Why my gateway can only connect to the Xiaomi band 2, but not to other Bluetooth devices
Reply
#2
Which other devices?
Reply
#3
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)
Reply
#4
Try connecting without using a random mac:
Code:
res, err = ble.connect(sock,"78:04:73:BF:C4:E5")
log(res, err)
Reply
#5
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。
Reply
#6
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)
Reply
#7
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.
Reply
#8
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.
Reply
#9
0x47 is two byte attribute handle, which is specific to a device to which you are connecting
Reply
#10
(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。
Reply
#11
(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。
Reply
#12
BLE Scanner does not provide handle numbers. You can use Linux with any USB BLE dongle: https://elinux.org/RPi_Bluetooth_LE
Reply
#13
(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?
Reply
#14
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
Reply
#15
(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.
Reply
#16
You need to use sockreadnotify instead of sockreadhnd after you've enabled notifications.
Reply
#17
(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!
Reply
#18
As already mentioned, check ti-sensortag and mio-alpha profiles that use notifications: https://forum.logicmachine.net/showthread.php?tid=1199
Reply


Forum Jump: