【发布时间】:2014-01-08 01:21:47
【问题描述】:
目标
我正在开发一个运行 Linux 的简单设备。它支持 BLE,我目前使用的是 bluez 5.8。
我想使用 iPhone 触发此设备上的操作。
已经有效的方法:
- 我可以让 iPhone “看到”设备。
- iPhone 也连接到设备。
我在linux上这样设置蓝牙设备(感谢this question):
# activate bluetooth
hciconfig hci0 up
# set advertise data: "hello world"
hcitool -i hci0 cmd 0x08 0x0008 48 45 4c 4c 4f 57 4f 52 4c 44
# start advertising as connectable
hciconfig hci0 leadv 0
iOS 代码很简单:
- (int) scanForPeripherals
{
if (self->centralManager.state != CBCentralManagerStatePoweredOn) {
return -1;
}
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[self.centralManager scanForPeripheralsWithServices:nil options:options];
return 0;
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
if (central.state == CBCentralManagerStatePoweredOn) {
NSLog(@"Starting scan");
[self scanForPeripherals];
}
}
- (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"didDiscoverPeripheral");
/*
* Retain the peripheral to avoid the error:
* CoreBluetooth[WARNING]: state = connecting> is being dealloc'ed while connecting
*/
self.activePeripheral = peripheral;
[centralManager connectPeripheral:peripheral options:nil];
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"Connected to peripheral");
/* discover all services */
[peripheral discoverServices:nil];
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
NSLog(@"Discovered services");
for (CBService *service in peripheral.services) {
NSLog(@"Discovered service %@", service);
}
}
在 iPhone 上运行此代码时,我得到以下日志:
2013-12-19 12:53:22.609 Test2[18518:60b] Starting scan
2013-12-19 12:53:29.945 Test2[18518:60b] didDiscoverPeripheral
2013-12-19 12:53:31.230 Test2[18518:60b] Connected to peripheral
所以看来 iPhone 连接正常,但没有看到任何服务。
我错过了什么
- 我需要宣传一个简单的 BLE 服务,但我在 bluez 中找不到任何关于如何做到这一点的文档。
- 我认为我需要类似 gatt-server 的东西来接收我要宣传的服务的读/写特征。我在 bluez 中看到了 plugins/gatt-example.c 文件,但我完全不知道如何使用它:没有文档。
我应该提到我看到了这个问题:Creating a gatt server,但答案提出了太多问题(例如,bluez的GATT api在哪里?如何设置GATT数据库?如何注册读/写事件?)
编辑: 我使用的命令仅设置 BLE 设备以通告一些数据,但 iOS 报告连接已接受。 bluez 的哪一部分接受传入连接?
【问题讨论】:
-
你是对的,你需要一个 GATT 服务器。您运行的命令只是将硬件设置为广播广告数据包,但不会启动任何要连接的东西。不幸的是,我还没有弄清楚如何自己设置 GATT 服务器,所以我无法为您提供帮助......
-
@TimTisdall 我知道这些命令不会设置服务器。这就提出了一个问题:谁在接受蓝牙连接?我编辑了我的帖子以明确这个问题。
-
我的猜测是内核中有什么东西在建立连接,但这只是一个猜测。
标签: linux bluetooth bluetooth-lowenergy core-bluetooth bluez