使用BTstack實現IOS裝置與非IOS裝置之間的通訊

來源:互聯網
上載者:User

google BTstack地址:http://code.google.com/p/btstack/

使用方法:http://code.google.com/p/btstack/wiki/GettingStarted

步驟說明:

    1. 在IOS裝置中通過Cydia來安裝BTstack,直接搜尋即可安裝;
    2. get the BTstack project from the Google code SVN:從Google的SVN代碼伺服器中獲得工程,SVN Location為http://btstack.googlecode.com/svn/trunk/,關於怎麼在Xcode4中使用SVN,請參考Xcode4使用SVN管理代碼。svn checkout http://btstack.googlecode.com/svn/trunk/ btstack
    3. 進入剛才下載的BTstack檔案夾
    4. 運行 bootstrap helper
    5. configure BTstack for the iPhone if you have SDK 2.0 installed, or...為iPhone配置BTstack,注意這裡的版本,我的是IOS5.0,大家應根據自己的實際情況來更改version的值。

      配置好之後,就可以直接運行工程了。進入BTstack/CocoaTouch/,運行工程即可,搜尋到周圍的非IOS裝置。

      BTStack工程解讀,工程架構:

      BTDevice類主要代表藍牙裝置,包含藍芽的常用資訊(名字、地址、Class等);

      BTDiscoveryView類主要提供搜尋介面;

      BTstackManager類主要用於管理藍牙裝置。

      BTTeststackManager用於測試,BTTeststackManager.m代碼及解讀如下:

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


      @implementation TestBTstackManager

      /*
      *啟用藍芽協議棧管理器(BTstackManager)
      */
      -(void) activatedBTstackManager:(BTstackManager*) manager {
      NSLog(@"activated!");
      [bt startDiscovery];
      }

      /*
      *啟用失敗
      */
      -(void) btstackManager:(BTstackManager*)manager activationFailed:(BTstackError)error {
      NSLog(@"activationFailed error 0x%02x!", error);
      };

      /*
      *搜尋可疑的藍芽協議棧
      */
      -(void) discoveryInquiryBTstackManager:(BTstackManager*) manager {
      NSLog(@"discoveryInquiry!");
      }

      /*
      *停止搜尋可疑的藍芽協議棧
      */
      -(void) discoveryStoppedBTstackManager:(BTstackManager*) manager {
      NSLog(@"discoveryStopped!");
      }

      /*
      *搜尋到藍牙裝置的名稱
      */
      -(void) btstackManager:(BTstackManager*)manager discoveryQueryRemoteName:(int)deviceIndex {
      NSLog(@"discoveryQueryRemoteName %u/%u!", deviceIndex+1, [bt numberOfDevicesFound]);
      }

      /*
      *搜尋到的藍牙裝置的資訊
      */
      -(void) btstackManager:(BTstackManager*)manager deviceInfo:(BTDevice*)device {
      NSLog(@"Device Info: addr %@ name %@ COD 0x%06x", [device addressString], [device name], [device classOfDevice] );
      }

      /*
      *藍牙裝置列表中被選中的裝置時,停止收縮附近的藍芽
      */
      -(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;
      }
      /*
      *重新開始搜尋裝置
      */
      -(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[])
      {
      NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
      int retVal = UIApplicationMain(argc, argv, nil, @"TestBTstackManager");
      [pool release];
      return retVal;
      }

      注意,該工程一定要在裝置上調試,在模擬器上調試不通過!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.