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;
}注意,该工程一定要在设备上调试,在模拟器上调试不通过!