19

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?

4

4 回答 4

17

据我了解,Android 实现只能充当中央设备,不能充当外围设备。在低功耗蓝牙中,只有外围设备可以做广告。中央设备可以扫描来自外围设备的广告,并发送连接请求作为对(某些类型的)广告的回复,以创建到外围设备的连接。

在 BLE 中, Central/PeripheralServer/Client的概念是有区别的:

  • Central/Peripheral 与网络架构有关,其中 Central 是星形中的集线器,有一个或多个外围设备连接到它。它通常是手机、平板电脑或计算机。外围设备一次只能连接到一个中心。

  • 服务器/客户端(GATT 服务器/客户端)是一个更高级别的概念,与保存在设备中并可能通过连接进行通信的数据有关。中央设备和外围设备都可以实现 GATT 服务器和 GATT 客户端,但不需要两者兼有。

所以回答你的问题:你不能调用广告。您必须开始扫描外围设备才能连接到其中的一个或多个。

希望这可以帮助。

于 2013-08-08T17:10:03.560 回答
12

您将需要 API 级别 21。

import android.bluetooth.le;
...
...
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter myBluetoothAdapter = bluetoothManager.getAdapter();
BluetoothLeAdvertiser myBluetoothLeAdvertiser =  myBluetoothAdapter.getBluetoothLeAdvertiser ();
myBluetoothLeAdvertiser.startAdvertising (AdvertiseSettings settings, AdvertiseData advertiseData, AdvertiseCallback callback);

有用的链接是:https ://developer.android.com/about/versions/android-5.0.html

于 2014-12-15T19:31:26.687 回答
2

似乎getProfileProxy没有响应GATTorGATT_SERVER请求。API 建议广告支持,但后面还没有实现代码。(Android 问题跟踪器)

引入 NFC 时,最初发布了相同的中途实现的 API,Google 通过后续版本迭代添加了更全面的功能。

于 2013-10-06T01:30:39.823 回答
1

正如我所看到的,BLE 广告功能(又名外围模式)将在即将推出的 Kitkat 4.4.3 版本中添加到 Android。应该是下周发布的,但是changelog已经不小心泄露了,见谷歌缓存或者here in line 2554: peripheral mode (3/4): Add peripheral mode API

我想我们很快就会知道更多。

于 2014-05-16T21:38:26.093 回答