Goal
I am developping a simple device running Linux. It is BLE capable, and I am currently using bluez 5.8.
I want to trigger an action on this device using an iPhone.
What already works:
- I can make the iPhone "see" the device.
- The iPhone also connects to the device.
I setup the bluetooth device like this on linux (thanks to 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
The iOS code is straightforward:
- (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);
}
}
When running this code on the iPhone, I get this log:
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
So it seems that the iPhone connects fine, but does not see any service.
What I am missing
- I need to advertise a simple BLE service, but I can't find any documentation on how to do this in bluez .
- I think I need something like a gatt-server to receive read/write characteristics for the service I would advertise. I saw the plugins/gatt-example.c file in bluez, but I have absolutely no idea how to use it: there is no documentation.
I should probably mention that I saw this question: Creating a gatt server, but the answers raise too much questions (for example, where is the GATT api for bluez? how to set the GATT database? How to register for read/write events?)
EDIT: The commands I use only set-up the BLE device to advertise some data, but iOS reports that the connection is accepted. What part of bluez is accepting incoming connections?
Eventually, I discovered the answers to all the questions I had.
I will start by answering the last question:
The commands I use only set-up the BLE device to advertise some data, but iOS reports that the connection is accepted. What part of bluez is accepting incoming connections?
This one was answered on the bluez mailing-list, in response to me.
Summary: the BLE connection is accepted at the HCI level by the kernel. If you want to use that connection from user space you need to use an l2cap socket with the ATT channel ID (which is 4).
Bleno has a good example of using an L2CAP socket.
How an L2CAP socket works is basically like this:
How to advertise a service?
I realized I needed an answer to the previous question to answer that one.
Once you can read the data over L2CAP socket, everything makes more sense, for example, if your Android phone does
gatt.discoverServices()
, then the little program above will read (i.e. receive):Which basically means:
This request is the way any BLE peripheral will request the list of services.
Then, you can answer this request with the list of services your device provides, formatted according to the GATT protocol.
Again, see the implementation of this in Bleno.
You're really close.
To see the services on a device using your iOS code, try adding
to your
didConnectPeripheral
, before thediscoverServices
call. I got that from the Apple documentation and it fixed things for me (just don't forget to addCBPeripheralDelegate
to the interface declaration in the header file). Without it,didDiscoverServices
will never be called.I was able to get the
gatt-example
service plugin to run by compiling BlueZ from source with the./configure --enable-maintainer-mode
. Then if you launchbluetoothd -nd
you'll see something likenear the top of the output, and then
At that point, my iOS app was able to see the BlueZ
peripheral
, connect, and discover its services (after ahciconfig hci0 leadv
).