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!