通过蓝牙IPhone SDK发送字符串 [英] Send string by Bluetooth IPhone SDK

查看:219
本文介绍了通过蓝牙IPhone SDK发送字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能发送从IPhone一个字符串到另一个设备(机器人,PC等)?

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

推荐答案

EDITED

继code超过蓝牙邮件传输
viewcontroller.h文件

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

#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文件

viewcontroller.m file

#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

然后包括GameKit.framework和运行项目。

then include GameKit.framework and run project.

这是工作完美地融入我的设备。

It's work perfectly into my devices.

希望这code帮你不少。

Hope this code help you lot

这篇关于通过蓝牙IPhone SDK发送字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆