My favorites | Sign in
Logo
             
Search
for
Updated Nov 18 (5 days ago) by matthias.ringwald
Labels: Featured
GettingStarted  
How to get a grip on the BTstack

Introduction

Although in an early stage, the BTstack can already used to create connections to other devices using the L2CAP protocol. This allows for example to receive keypress events from an Bluetooth keyboard or movement data from a WiiMote.

On this page, we provide a quick walk-through to run example/test.c on an iPhone 3G(S) or iPod touch 2G/3G and connect to a WiiMote, while displaying acceleration and button reports.

Requirements

Steps

  1. get the BTstack project from the Google code SVN:
  2. svn checkout http://btstack.googlecode.com/svn/trunk/ btstack
  3. cd into the btstack folder
  4. cd btstack
  5. If you have pkg-config from MacPorts
  6. in bootstrap.sh: change aclocal to aclocal -I /opt/local/share/aclocal
  7. If you don't have iPhone SDK 2.0 installed
  8. in configure.in: change the SDK_VERSION variable to the SDK you want to use/have. BTstack works with all versions.
  9. run the bootstrap helper
  10. ./bootstrap.sh
  11. configure BTstack for the iPhone
  12. ./configure --target=iphone
  13. If you get an error that mentions "PKG_CHECK_MODULES", then aclocal did not pick up pkg-config installation. Try to fix it and run configure again.
  14. in example/test.c, find the line with the hard-coded value for the MAC address of the WiiMote and enter yours. Mine has the address 00:19:1d:90:44:68
  15. build it
  16. make
  17. copy the BTdaemon, the client client library and the example test to your iPhone. Replace 192.168.3.102 with the IP address of your device
  18. scp src/BTdaemon src/libBTstack.dylib example/test mobile@192.168.3.102:.
  19. ssh into your device
  20. ssh mobile@192.168.3.102
  21. sign the binaries with ldid
  22.  ldid -S BTdaemon
     ldid -S test
     ldid -S libBTstack.dylib
  23. copy the client library into /usr/local/lib
  24.  su
     cp libBTstack.dylib /usr/local/lib
     exit
  25. make sure Bluetooth is turned OFF in the Settings.app
  26. run the BTdaemon
  27. ./BTdaemon
  28. ssh into your device again
  29. ssh mobile@192.168.3.102
  30. run the test
  31. ./test
  32. check the output in both sesssions. If everything works, the app will prompt you to make your WiiMote discoverable by pressing the buttons 1 and 2 together. Shortly after that you should get continuous accelerometer readings. You can press the Home button to quit the test app.

Important: My WLAN often breaks down when using Bluetooth. When logged in via SSH this looks like the app would not work or hang, when in reality the WLAN is just not working properly. Check the WLAN icon in the status bar. After the introduction of Internet tethering, I highly recommend to connect the iPhone via USB and use this connection for SSH. When using USB Tethering, my machine receives the IP address 192.168.20.3, and the iPhone can be accessed at 192.168.20.1.

Details

In main, the BTstack is opened and packet handlers are registered before the BTstack is asked to boot with the btstack_set_power_mode command. All commands to control the BTstack are send asynchronously with the bt_send_cmd. Finally the run_loop is executed. From there on, the rest of the test app runs as a finite state machine which processes events received from the BTstack. Be aware that error handling is barely performed. :)

Anyway, the different steps of the program are visible in the event handler. First, it reacts to the successful startup of the BTstack and set the local Bluetooth name. Then, pairing is enabled/disabled with the hci_write_authentication_enable HCI command, which is caused by the command complete event of the hci_hci_write_local_name. Again, a command complete event will trigger the next action. Now, the BTstack is asked to open an L2CAP channel to the hard-coded WiiMote at PSM 0x13 (HID Interrupt) where reports from the HID device are received. Upon success, a second channel for PSM 0x11 (HID Control) is opened. This channel is necessary to enable the reporting of acceleration readings and send the command to set LEDs which also stops the LEDs from blinking. Finally, we're ready to receive HID status reports. See the HID and the WiiMote documentation for more details on this.

As this event handler shows, everything is event-triggered. After sending a command to the BTdaemon, which in turn sends it to the Bluetooth chipset, we wait for the corresponding event to continue with our application. Instead of writing this single even handler, it is much better to follow the concept of finite state machines. Based on our current state, an event such as HCI events, triggers the next action and the transition to another state. For more insight inton finite state machine, I highly recommend the book by Miro Samek.

References

Help

If you really want to try the example app on your iPhone, but didn't get it to work although you've followed the instructions here, or, if you'd like to use Bluetooth in your own application (whatever OS/HW), please join the BTstack Developer Forum.


Comment by monsieur.goonie, Nov 05, 2009

hello mathias, AS i understood, btstack 0.1 is out right?

I don't own a wiimote but well, I'd still like to dl it from cydia if possible, what's the source to add in order to install it?

regards G.

Comment by matthias.ringwald, Nov 08, 2009

Cydia packages are out: iPhoneInstallationViaCydia

Comment by robmack228, Nov 10, 2009

I'd like to connect with a ps3 using the iPhones keyboard for input within pshome... What would it take to achieve something like that?

Comment by matthias.ringwald, Nov 11, 2009

I don't know what's used for the PS3. If you can use a regular Bluetooth keyboard, it should be possible BTstack on the iPhone too. Please join the BTstack Developer Group for further discussion.

Comment by monsieur.goonie, Nov 11, 2009

great mathias, alright, I'll install it up as soon as I get back home, thank you

Comment by robmack228, Nov 16, 2009

U can use a bluetooth keyboard on ps3, It would be the same as turning the iPhone into a bluetooth keyboard for pc/mac... I'll join

Comment by Alexandre.Bourget, Nov 18 (4 days ago)

Is there a way that I can install btstack on my jailbroken Ipod Touch and use it to pair a headset for skype or whatever voip app? From what I understand, Itouch can't pair natively to headsets, only headphones.... was wondering if btstack was supposed to work on its own... I installed btstack and tried to enable bluetooth using the normal way, no pairing possible, and couldn't find any btstack icons or whatever...

thx

Comment by matthias.ringwald, Nov 19 (4 days ago)

BTstack is an alternative to Apple's on the iPhone. No support for the Headphone or the A2DP protocol yet. Even then, integrating it into the OS will be tricky.

Comment by Alexandre.Bourget, Nov 19 (4 days ago)

The thing is, I know it works for the iphone, Apple just didn't want to include the profiles into the Ipod touch. So Apple's BT stack would work for me. I'm just somehow surprised no little cracker managed (or bothered to try) to rip those profiles from the IPhone to include them in a jailbroken ipod touch yet... maybe it's harder than I think... thx for your reply...

Comment by monsieur.goonie, Yesterday (26 hours ago)

Ok, I could see my keyboard, without crash but nothing more so far... there will be a switch to disable it soon, right? regards.

So when's "next";) ?

Comment by matthias.ringwald, Yesterday (26 hours ago)

@goonie. BTstack is not integrated into Apple's. Keyboard will be provided by a separate app that allows to connect and to configure your keyboard. I've got cursor support working now (no keyboard -> no cursor support in the OS). And it won't take much longer. I'll promise to release something before Christmas...

Comment by laptophomework2007, Today (15 hours ago)

I tried to look around before posting so as not to sound like an idiot, but I'm afraid that I've run into a dead end. The intro says that BTstack for the iPhone works with keyboards, but I can't find any data/instructions on the topic. Is it possible to connect a bluetooth keyboard to an iPhone with Btstack at this time? And if so, is it similar to the process outlined here? Thanks for your time and keep up the good work.

Comment by monsieur.goonie, Today (3 hours ago)

thank you matthias, You mean we won't have to run a vnc app ;)))

I'll love christmas this year.


Sign in to add a comment
Hosted by Google Code