安卓4.3 BTLE为服务器:如何启动广告了?服务器、广告、BTLE

2023-09-05 07:11:43 作者:社会复杂爷简单

我想实现的的Nexus 7在4.3的新BTLE API一个BTLE服务器。我遇到的几个问题。首先有与SDK没有例子。唯一的例子为客户端。其次,文件实际上告诉你做了错误的事情。它指出,必须使用 BluetoothAdapter.getProfileProxy()与BluetoothProfile.GATT_SERVER 参数,以获得 BluetoothGattServer 对象。这种方法将工作,但一会就不能一个人实施 BluetoothGattServerCallback 的链接到BLE堆栈。 (此回调是怎么响应客户端读取和写入除其他事项外的请求。)但是,磕磕绊绊发行58582后,开发人员指出,新的 BluetoothManager.openGattServer()方法这需要回调作为参数,并返回一个 BluetoothGattServer 对象。那么,一个问题解决了。

接下来的问题是比较麻烦的。该 BluetoothGattServer 文档指出可以使用这个类来创建和发布蓝牙LE服务和特​​色。创建服务等不是问题,但他们忽视说怎么做起了广告。有一个在类本身没有任何方法或其他任何我能找到的类。

有谁知道如何做到这一点?此刻所有我可以看到的是使用相同的方法,因为所使用的客户端,但是该方法涉及扫描(这不是广告)。所有的文件还表明, BluetoothAdapter.startLeScan()确实只是扫描。

那么,如何调用一次广告,我所有的服务,特点,描述到位? 解决方案

据我所知,Android的实现只能充当一个中央设备,而不是作为一个外围设备。在蓝牙低能耗,只有外围可以做广告。中央设备可以从外围的广告扫描,并发送连接请求的答复(某些类型的)广告,来创建到外设的连接。

在BLE,有区别的概念的中央/外设和服务器/客户端的:

中央/外设是与网络架构,其中中央是一个明星的枢纽,连接了一个或多个外设。它通常是一个电话,平板电脑或计算机。外围设备只能连接到一个集中的时间。

服务器/客户端(总协定服务器/客户端)是一个更高层次的概念,相关被保存在设备和可能传送通过连接的数据。中央和外围设备可以实现总协定服务器和一个总协定客户端,但不要求同时具有

因此​​,要回答你的问题:你不能调用广告。你将不得不开始扫描用于外围设备,以便能够使一个或多个它们的连接

希望这有助于。

I am trying to implement a BTLE SERVER on the Nexus 7 with the new BTLE API in 4.3. I am running into several problems. First there are no examples with the SDK. The only example is for a client. Second, the documentation actually tells you to do the wrong thing. It states that one must use the BluetoothAdapter.getProfileProxy() with a BluetoothProfile.GATT_SERVER parameter to obtain the BluetoothGattServer object. This approach will work, but one will be unable to link one's implementation of the BluetoothGattServerCallback to the BLE stack. (This callback is how one responds to client read and write requests among other things.) However, after stumbling on issue 58582 a developer pointed to the new BluetoothManager.openGattServer() method which takes your callback as a parameter and returns a BluetoothGattServer object. Well, one problem solved.

服务器数据恢复 天津数据库恢复

The next issue is more problematic. The BluetoothGattServer documentation states that one can use this class to create and advertise Bluetooth LE services and characteristics. Creating the services, etc. was not problem but they neglect to say how to start advertising. There is no method in the class itself or any other of the classes that I can find.

Does anyone know how to do this? At the moment all I can see is to use the same approach as used by the client, but that approach involves scanning (which is not advertising). All the documentation further suggests that the BluetoothAdapter.startLeScan() IS indeed JUST for scanning.

So how do I invoke advertisements once all my services, characteristics, and descriptors are in place?

解决方案

As I understand, the Android implementation can only act as a central device, and not as a peripheral device. In Bluetooth Low Energy, only the peripheral can advertise. The central device can scan for advertisements from peripherals, and send connect requests as replies to (some kinds of) advertisements, to create a connection to the peripheral.

In BLE, there is a distinction between the concepts Central/Peripheral and Server/Client:

Central/Peripheral is relating to the network architecture, where the central is the hub in a star, with one or more peripherals connected to it. It will typically be a phone, tablet or computer. A peripheral device can only connect to one central at a time.

Server/Client (GATT server/client) is a higher level concept, related to the data that are kept in the devices and possibly communicated over the connection. Both central and peripheral devices can implement a GATT server and a GATT client, but is not required to have both.

So to answer your question: You cannot invoke advertisements. You will have to start scanning for peripheral devices to be able to make a connection to one or more of them.

Hope this helps.