How to use go-ble to connect from android to linux

huangapple go评论86阅读模式
英文:

How to use go-ble to connect from android to linux

问题

我正在尝试在两个设备之间实现BLE连接:

  • 一个作为服务器的Linux应用程序,必须广播服务和特征
  • 一个作为客户端的Android智能手机,必须连接到服务器并读取特征

问题出在我的Linux应用程序上。当我使用我的Android应用程序和bluetoothctl将智能手机连接到笔记本电脑时,一切正常。然而,当我使用我的Linux应用程序而不是bluetoothctl时,它不起作用(实际上很少起作用)。

由于我的Linux应用程序是用Go语言编写的,我使用go-ble作为蓝牙包。我在他们的最小代码示例中遇到了同样的问题,这就是为什么我在这里没有写更多代码的原因,但我无法弄清楚原因。

我认为我只是不知道如何正确使用他们的库,但是文档并不是很明确,所以如果有人能帮忙就好了...


编辑

在我的智能手机上,当使用我的应用程序时,我可以看到扫描到笔记本电脑,所以HCI设备是正常的。但是当尝试使用connectGatt()连接时,我得到了一个133状态码,即GATT_ERROR

一个可能的问题是我没有关闭连接,参考这个线程,但是为什么使用bluetoothctl时它可以工作呢?

英文:

I'm trying to implement a BLE connection between two devices:

  • A Linux app, as the server, that must advertise services and characteristics
  • An Android smartphone, as the client, that must connect to the server and read characteristics

The issue comes from my Linux app. When I connect my smartphone to my laptop using my android app and bluetoothctl, it works like a charm. However, when I do the same with my Linux app instead of bluetoothctl, it doesn't works (it actually does, but very rarely).

As my Linux app is written in Go, I use go-ble as the bluetooth package. I have the same issue with their minimal code sample, this is the reason I don't write more code here, but I can't figure out why.

I think I just don't know how to use their library correctly, but the documentation isn't very explicit, so if anyone could help...


EDIT

On my smartphone, when using my apps, I see the laptop when scanning, so The HCI device is up. But when attempting to connect with connectGatt(), I get a 133 status code, which is GATT_ERROR.

One probable issue, is I'm not closing the connection, see this thread, but then how could it works with bluetoothctl?

答案1

得分: 1

Bluetoothctl是一个可以与BLE和BR/EDR蓝牙一起工作的蓝牙客户端。它之前可以工作是因为我的安卓应用程序通过“经典蓝牙”连接了gattConnect

我通过指定要使用的传输协议来解决了这个问题,将以下代码:

device.connectGatt(context, false, gattCallback)

替换为:

device.connectGatt(context, false, gattCallback, BluetoothDevice.TRANSPORT_LE)
英文:

Bluetoothctl is a bluetooth client working either with BLE and BR\EDR bluetooth. It was working because my android app was connecting through "Classic Bluetooth" with gattConnect.

I solved it by specifying the transport protocol to use, replacing:

device.connectGatt(context, false, gattCallback)

with:

device.connectGatt(context, false, gattCallback, BluetoothDevice.TRANSPORT_LE)

huangapple
  • 本文由 发表于 2022年4月4日 20:04:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/71736964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定