google BTstack地址:http://code.google.com/p/btstack/
使用方法:http://code.google.com/p/btstack/wiki/GettingStarted
步驟說明:
- 在IOS裝置中通過Cydia來安裝BTstack,直接搜尋即可安裝;
- 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
- 進入剛才下載的BTstack檔案夾
- 運行 bootstrap helper
- 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;
}
注意,該工程一定要在裝置上調試,在模擬器上調試不通過!