我想通过蓝牙在两台iOS设备之间转移NSString
。任何人都可以帮助如何通过蓝牙转移NSString
?我搜索了具体的答案和示例代码,但找不到它。请指导我。
先谢谢。!!
答案 0 :(得分:13)
我将更广泛地评论如何使用MCSession
来处理这种简单的案例,因为当我第一次熟悉MCSession
时,我很惊讶于可用的信息很少关于如何使用简单的MCSession
而不添加MCBrowserViewController
的额外图层。
在.h中,添加以下代理:MCSessionDelegate
,MCNearbyServiceAdvertiserDelegate
和MCNearbyServiceBrowserDelegate
。同时为MCPeerID *devicePeerID
,MCSession *session
,MCNearbyServiceAdvertiser *serviceAdvertiser
和MCNearbyServiceBrowser *nearbyServiceBrowser
声明类实例变量。
在您的.m中,在viewDidLoad期间或您希望开始MCSession
之前的任何其他时间,请初始化您的MCPeerID
:
devicePeerId = [[MCPeerID alloc] initWithDisplayName:DISPLAY_NAME];
然后使用MCPeerID
初始化MCSession
:
session = [[MCSession alloc] initWithPeer:devicePeerId securityIdentity:nil encryptionPreference:MCEncryptionNone];
session.delegate = self;
现在,为了避免使用MCBrowserViewController
,您必须初始化自己的MCNearbyServiceAdvertiser
以允许您的设备宣传会话MCNearbyServiceBrowser
以允许您的设备查找会话,或者您甚至可以在同一台设备上同时启用,以便同时进行广告和浏览:
serviceAdvertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:myDevicePeerId discoveryInfo:nil serviceType:SERVICE_TYPE];
serviceAdvertiser.delegate = self;
// (I've set discoveryInfo to nil here, but it can also contain an NSDictionary of data to pass along to browsers who find this advertiser via the browser:foundPeer:withDiscoveryInfo method)
nearbyServiceBrowser = [[MCNearbyServiceBrowser alloc] initWithPeer:myDevicePeerId serviceType:SERVICE_TYPE];
nearbyServiceBrowser.delegate = self;
接下来,如果您将设备设置为广告客户,则需要实施MCNearbyServiceAdvertiserDelegate
方法。
要向浏览对等方发送邀请:
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void (^)(BOOL, MCSession *))invitationHandler {
NSLog(@"invitation received");
if (want_to_accept_invitation)
invitationHandler(YES, session);
else
invitationHandler(NO, session);
}
如果设备由于某种原因尚未开始投放,则会收到错误消息:
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didNotStartAdvertisingPeer:(NSError *)error {
NSLog(@"Did not start advertising error: %@", error);
}
同样,如果您将设备设置为浏览器,则需要实施MCNearbyServiceBrowserDelegate
方法:
// Peer found
- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info {
NSLog(@"Session Manager found peer: %@", peerID);
if (want_to_connect)
[serviceBrowser invitePeer:peerID toSession:session withContext:nil timeout:CONNECTION_TIMEOUT];
}
// Peer lost, ex. out of range
- (void)browser:(MCNearbyServiceBrowser *)browser lostPeer:(MCPeerID *)peerID {
NSLog(@"Session Manager lost peer: %@", peerID);
}
- (void)browser:(MCNearbyServiceBrowser *)browser didNotStartBrowsingForPeers:(NSError *)error {
NSLog(@"Did not start browsing for peers: %@", error);
}
然后,您需要MCSessionDelegate
方法来帮助通知用户更改连接状态并方便发送和接收数据:
- (void)session:(MCSession *)session didReceiveCertificate:(NSArray *)certificate fromPeer:(MCPeerID *)peerID certificateHandler:(void (^)(BOOL accept))certificateHandler {
NSLog(@"Did receive certificate");
certificateHandler(true);
}
// To detect changes in the state of your connections with your peers….
- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state {
switch (state) {
case MCSessionStateConnected: {
NSLog(@"Connected to %@", peerID);
// If you'd like to send your text string as soon as you're connected...
NSError *error;
[session sendData:[@"text" dataUsingEncoding:NSUTF8StringEncoding] toPeers:[NSArray arrayWithObject:peerID] withMode:MCSessionSendDataReliable error:&error];
break;
} case MCSessionStateConnecting: {
NSLog(@"Connecting to %@", peerID);
break;
} case MCSessionStateNotConnected: {
break;
}
}
}
- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID {
NSLog(@"Did receive data.");
/// Receive the string here.
NSString *message = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
请注意,为了发送数据,我使用了:
[session sendData:[@"text" dataUsingEncoding:NSUTF8StringEncoding] toPeers:[NSArray arrayWithObject:peerID] withMode:MCSessionSendDataReliable error:&error];
在用户与同伴联系后立即传输数据。但是这一行可以用来在代码中的其他地方发送数据,例如:
- (void)sendMessageToAllPeers:(NSString *)message {
[session sendData:[message dataUsingEncoding:NSUTF8StringEncoding] toPeers:session.connectedPeers withMode:MCSessionSendDataReliable error:&error];
}
- (void)sendMessage:(NSString *)message toPeerIDs:(NSArray *)peerIDs {
[session sendData:[message dataUsingEncoding:NSUTF8StringEncoding] toPeers:peerIDs withMode:MCSessionSendDataReliable error:&error];
}
最后,要开始/停止宣传您的广告客户和/或浏览器,您可以致电[_serviceAdvertiser start/stopAdvertisingPeer]
,[_nearbyServiceBrowser start/stopBrowsingForPeers]
:
- (void)start {
[serviceAdvertiser startAdvertisingPeer];
[nearbyServiceBrowser startBrowsingForPeers];
}
- (void)stop {
[serviceAdvertiser stopAdvertisingPeer];
[nearbyServiceBrowser stopBrowsingForPeers];
}
还有其他方法,但这些是基础知识。尽快写出来,所以任何人都可以随意修改!
答案 1 :(得分:1)
查看Apple开发者论坛的源代码BTLE Transfer
我希望这可以帮助您使用蓝牙将字符串或任何数据从一个设备传输到另一个设备。