Use BTstack to implement communication between IOS devices and non-IOS devices

Source: Internet
Author: User

Google BTstack address: http://code.google.com/p/btstack/

Usage: http://code.google.com/p/btstack/wiki/GettingStarted

Steps:

    1. Use Cydia to install BTstack on IOS devices and search for it to install it;
    2. Get the BTstack project from the Google code SVN: get the project from Google's SVN code server, SVN Location is Repository. Svn checkout http://btstack.googlecode.com/svn/trunk/ btstack
    3. Go to the downloaded BTstack folder.
    4. Run bootstrap helper
    5. Configure BTstack for the iPhone if you have sdks 2.0 installed, or... configure BTstack for the iPhone. Note that the version here is IOS5.0. You should change the version value based on your actual situation.

      After configuration, you can directly run the project. Go to BTstack/CocoaTouch/, run the project, and find the surrounding non-IOS devices.

      BTStack engineering interpretation, engineering framework:

      The BTDevice Class mainly represents a bluetooth device and contains common Bluetooth information (such as name, address, and Class );

      The BTDiscoveryView class mainly provides a search interface;

      The BTstackManager class is mainly used to manage Bluetooth devices.

      BTTeststackManager is used for testing. The BTTeststackManager. m code and interpretation are as follows:

      # Import "TestBTstackManager. h"
      # Import <BTstack/BTDevice. h>


      @ Implementation TestBTstackManager

      /*
      * Activate the Bluetooth protocol stack Manager (BTstackManager)
      */
      -(Void) activatedBTstackManager :( BTstackManager *) manager {
      NSLog (@ "activated! ");
      [Bt startDiscovery];
      }

      /*
      * Activation failed
      */
      -(Void) btstackManager :( BTstackManager *) manager activationFailed :( BTstackError) error {
      NSLog (@ "activationFailed error 0x % 02x! ", Error );
      };

      /*
      * Search for suspicious Bluetooth protocol stacks
      */
      -(Void) discoveryInquiryBTstackManager :( BTstackManager *) manager {
      NSLog (@ "discoveryInquiry! ");
      }

      /*
      * Stop searching for suspicious Bluetooth protocol stacks
      */
      -(Void) discoveryStoppedBTstackManager :( BTstackManager *) manager {
      NSLog (@ "discoveryStopped! ");
      }

      /*
      * The Name Of The Bluetooth device is found.
      */
      -(Void) btstackManager :( BTstackManager *) manager discoveryQueryRemoteName :( int) deviceIndex {
      NSLog (@ "discoveryQueryRemoteName % u/% u! ", DeviceIndex + 1, [bt numberOfDevicesFound]);
      }

      /*
      * Information of the searched bluetooth device
      */
      -(Void) btstackManager :( BTstackManager *) manager deviceInfo :( BTDevice *) device {
      NSLog (@ "Device Info: addr % @ name % @ COD 0x % 06x", [device addressString], [device name], [device classOfDevice]);
      }

      /*
      * When a selected device in the bluetooth device list is stopped
      */
      -(BOOL) discoveryView :( BTDiscoveryViewController *) discoveryView willSelectDeviceAtIndex :( int) deviceIndex {
      If (selectedDevice) return NO;
      SelectedDevice = [bt deviceAtIndex: deviceIndex];
      BTDevice * device = selectedDevice;
      NSLog (@ "Device selected: addr % @ name % @ COD 0x % 06x", [device addressString], [device name], [device classOfDevice]);
      [Bt stopDiscovery];
      Return NO;
      }
      /*
      * Start searching devices again
      */
      -(Void) statusCellSelectedDiscoveryView :( BTDiscoveryViewController *) discoveryView {
      If (! [Bt isDiscoveryActive]) {
      SelectedDevice = nil;
      [Bt startDiscovery];
      }
      NSLog (@ "statusCellSelected! ");
      }
      -(Void) applicationDidFinishLaunching :( UIApplication *) application {

      SelectedDevice = nil;

      // Create discovery controller
      DiscoveryView = [[BTDiscoveryViewController alloc] init];
      [DiscoveryView setDelegate: self];
      UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController: discoveryView];
      UIWindow * window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen] bounds];
      [Window addSubview: nav. view];
      [Window makeKeyAndVisible];

      // BTstack
      Bt = [BTstackManager sharedInstance];
      [Bt setDelegate: self];
      [Bt addListener: self];
      [Bt addListener: discoveryView];

      BTstackError err = [bt activate];
      If (err) NSLog (@ "activate err 0x % 02x! ", Err );
      }
      @ End

      Int main (int argc, char * argv [])
      {
      NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init];
      Int retVal = UIApplicationMain (argc, argv, nil, @ "TestBTstackManager ");
      [Pool release];
      Return retVal;
      }

      Note: This project must be debugged on the device. It cannot be debugged on the simulator!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.