xcode 通过蓝牙IPhone SDK发送字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14575706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:39:06  来源:igfitidea点击:

Send string by Bluetooth IPhone SDK

iphonexcodebluetooth

提问by Eduardo Font

how can I send a string from an IPhone to another device (android, pc, etc)?

如何将字符串从 iPhone 发送到另一台设备(android、pc 等)?

回答by Nimit Parekh

EDITED

已编辑

Following code for the message transfer over the bluetooth viewcontroller.h file

以下代码用于通过蓝牙 viewcontroller.h 文件传输消息

#import <UIKit/UIKit.h>
#import <GameKit/GameKit.h>

@interface ViewController : UIViewController <GKSessionDelegate, GKPeerPickerControllerDelegate>

@property (strong, nonatomic) UILabel           *messageReceivedLabel;
@property (strong, nonatomic) UITextField       *messageToSendTextField;
@property (strong, nonatomic) GKSession         *session;
@property (strong, nonatomic) UIButton          *sendButton; 

@end

viewcontroller.m file

视图控制器.m 文件

#import "ViewController.h"

@interface ViewController ()
- (void)sendMessage:(id)sender;
- (void)connectToDevice:(id)sender;

@end

@implementation ViewController

@synthesize messageReceivedLabel    = _messageReceivedLabel;
@synthesize messageToSendTextField  = _messageToSendTextField;
@synthesize session                 = _session;
@synthesize sendButton              = _sendButton;

- (void)connectToDevice:(id)sender
{
    if (self.session == nil) {
        //create peer picker and show picker of connections
        GKPeerPickerController *peerPicker = [[GKPeerPickerController alloc] init];
        peerPicker.delegate = self;
        peerPicker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
        [peerPicker show];
    }

}

- (void)sendMessage:(id)sender 
{   
    //package text field text as NSData object 
    NSData *textData = [self.messageToSendTextField.text dataUsingEncoding:NSASCIIStringEncoding];
    //send data to all connected devices
    [self.session sendDataToAllPeers:textData withDataMode:GKSendDataReliable error:nil];

}

#pragma mark - 
#pragma mark - GKPeerPickerControllerDelegate
- (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type
{
    //create ID for session
    NSString *sessionIDString = @"MTBluetoothSessionID";
    //create GKSession object
    GKSession *session = [[GKSession alloc] initWithSessionID:sessionIDString displayName:nil sessionMode:GKSessionModePeer];
    return session;
}

- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session
{
    //set session delegate and dismiss the picker
    session.delegate = self;
    self.session = session; 
    picker.delegate = nil;
    [picker dismiss];
}

#pragma mark - 
#pragma mark - GKSessionDelegate
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state
{
    if (state == GKPeerStateConnected){
        [session setDataReceiveHandler:self withContext:nil]; //set ViewController to receive data
        self.sendButton.enabled = YES; //enable send button when session is connected
    }
    else {
        self.sendButton.enabled = NO; //disable send button if session is disconnected
        self.session.delegate = nil; 
        self.session = nil; //allow session to reconnect if it gets disconnected
    }
}

- (void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context
{   
    //unpackage NSData to NSString and set incoming text as label's text
    NSString *receivedString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    self.messageReceivedLabel.text = receivedString;

}

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Button to connect to other device
    UIButton *connectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    connectButton.frame = CGRectMake(20.0f, 20.0f, 80.0f, 40.0f);
    [connectButton setTitle:@"Connect" forState:UIControlStateNormal];
    connectButton.tintColor = [UIColor darkGrayColor];
    [connectButton addTarget:self action:@selector(connectToDevice:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:connectButton];

    //Button to send message to other device
    UIButton *sendButton_ = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    sendButton_.frame = CGRectMake(220.0f, 20.0f, 80.0f, 40.0f);
    [sendButton_ setTitle:@"Send" forState:UIControlStateNormal];
    sendButton_.tintColor = [UIColor darkGrayColor];
    sendButton_.enabled = NO; //set button as disabled until connection is made
    [sendButton_ addTarget:self action:@selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside];
    self.sendButton = sendButton_;
    [self.view addSubview:self.sendButton];

    //Label for message that is received
    self.messageReceivedLabel = nil;
    CGRect messageReceivedLabel_Frame = CGRectMake(20.0f, 80.0f, 280.0f, 44.0f);
    UILabel *messageReceivedLabel_ = [[UILabel alloc] initWithFrame:messageReceivedLabel_Frame];
    messageReceivedLabel_.textAlignment = UITextAlignmentCenter;
    messageReceivedLabel_.font = [UIFont boldSystemFontOfSize:20.0f];
    self.messageReceivedLabel = messageReceivedLabel_;
    [self.view addSubview:self.messageReceivedLabel];

    //Text field to input message to send
    CGRect messageToSendTextField_Frame = CGRectMake(20.0f, 144.0f, 280.0f, 44.0f);
    UITextField *messageToSendTextField_ = [[UITextField alloc] initWithFrame:messageToSendTextField_Frame];
    messageToSendTextField_.font = [UIFont systemFontOfSize:20.0f];
    messageToSendTextField_.backgroundColor = [UIColor whiteColor];
    messageToSendTextField_.clearButtonMode = UITextFieldViewModeAlways;
    messageToSendTextField_.placeholder = @"Enter a message to send";
    messageToSendTextField_.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.messageToSendTextField = messageToSendTextField_;
    [self.view addSubview:self.messageToSendTextField];

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


@end

then include GameKit.framework and run project.

然后包含 GameKit.framework 并运行项目。

It's work perfectly into my devices.

它完美地适用于我的设备。

Hope this code help you lot

希望这段代码对你有很大帮助