在iOS中通过蓝牙在两台设备之间传输NSString

我想通过蓝牙在两个iOS设备之间传输NSString 。 任何人都可以请帮助如何通过蓝牙传输NSString ? 我search了具体的答案和示例代码,但无法find它。 请指导我

提前致谢。!!

我将更广泛地评论如何使用MCSession来处理这种简单的情况,因为当我第一次熟悉MCSession ,我很惊讶于如何在不添加额外的情况下使用简单的MCSession MCBrowserViewController

在您的.h,添加以下代表: MCSessionDelegateMCNearbyServiceAdvertiserDelegateMCNearbyServiceBrowserDelegate 。 还为MCPeerID *devicePeerIDMCSession *sessionMCNearbyServiceAdvertiser *serviceAdvertiserMCNearbyServiceBrowser *nearbyServiceBrowser声明类实例variables。

在你的.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允许您的设备查找会话,或者甚至可以在同一设备上启动BOTH以允许同时广告和浏览:

 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]; } 

还有其他方法,但这些是基础知识。 尽pipe写得很快,所以任何人都可以随意修改!

检查苹果开发者论坛BTLE转移的源代码

我希望这可以帮助您使用蓝牙将string或任何数据从一台设备传输到另一台设备。