在Android上我这样做是用一个简单的attacched地图创建一个服务广播作为信息
HashMap<String, String> record = new HashMap<>();
record.put("info, "my android info");
WifiP2pDnsSdServiceInfo serviceInfo = WifiP2pDnsSdServiceInfo
.newInstance("_myservice", "_tcp", record);
WifiP2pManager manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
Channel channel = manager.initialize(this, this.getMainLooper(), null);
manager.addLocalService(channel, serviceInfo, null);
检索另一个信号
manager.setDnsSdResponseListeners(channel, this, this);
@Override
public void onDnsSdTxtRecordAvailable(String fullDomainName, Map<String, String> txtRecordMap, WifiP2pDevice srcDevice)
在iOS上,它是相同的服务名称和信息地图
MCPeerID* _myPeerId=[[MCPeerID alloc] initWithDisplayName:@"my ios peer"];
NSDictionary* _serviceInfo=@{@"info": @"my ios info"};
MCNearbyServiceAdvertiser* _serviceAdvertiser = [[MCNearbyServiceAdvertiser alloc]
initWithPeer:_myPeerId discoveryInfo:_serviceInfo serviceType:@"myservice"];
_serviceAdvertiser.delegate=advertiseDelegate;
MCNearbyServiceBrowser* _serviceBrowser=[[MCNearbyServiceBrowser alloc]
initWithPeer:_myPeerId serviceType:@"myservice"];
_serviceBrowser.delegate=browserDelegate;
[_serviceAdvertiser startAdvertisingPeer];
[_serviceBrowser startBrowsingForPeers];
和代表
-(void)browser:(MCNearbyServiceBrowser *)browser lostPeer:(MCPeerID *)peerID
-(void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary<NSString *,NSString *> *)info
Android设备只能在iOS设备之间看到另一个Android设备。但是,这两个家庭看不到对方。 无论操作系统如何,我都想让其他设备看到它们。