Ios 从Apple Watch应用程序到其他设备的本地网络连接可以在模拟器上工作,但不能在手表上工作
我正在尝试为我的Apple Watch构建一个应用程序,用于远程控制覆盆子Pi上的媒体播放器(omxplayer) 我让它在Xcode的模拟器中工作,但在实际的手表上不工作 在Raspberry Pi上,我有一个简单的python脚本,可以启动TCP服务器并侦听命令 在WatchKit扩展名projet的InterfaceController.m文件中,我有以下代码:Ios 从Apple Watch应用程序到其他设备的本地网络连接可以在模拟器上工作,但不能在手表上工作,ios,objective-c,apple-watch,Ios,Objective C,Apple Watch,我正在尝试为我的Apple Watch构建一个应用程序,用于远程控制覆盆子Pi上的媒体播放器(omxplayer) 我让它在Xcode的模拟器中工作,但在实际的手表上不工作 在Raspberry Pi上,我有一个简单的python脚本,可以启动TCP服务器并侦听命令 在WatchKit扩展名projet的InterfaceController.m文件中,我有以下代码: - (IBAction)playPauseButtonPressed { [self initNetworkCo
- (IBAction)playPauseButtonPressed {
[self initNetworkCommunication];
[self sendNetworkCommand:@"play"];
[self closeNetworkCommunication];
}
-(void)initNetworkCommunication {
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.10.116", 10010, &readStream, &writeStream);
if(!CFWriteStreamOpen(writeStream)) {
NSLog(@"Error, writeStream not open");
return;
}
inputStream = (NSInputStream *)CFBridgingRelease(readStream);
outputStream = (NSOutputStream *)CFBridgingRelease(writeStream);
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}
-(void)sendNetworkCommand:(NSString *) command {
NSData *data = [[NSData alloc] initWithData:[command dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
}
-(void)closeNetworkCommunication {
[inputStream close];
[outputStream close];
[inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream setDelegate:nil];
[outputStream setDelegate:nil];
inputStream = nil;
outputStream = nil;
}
当我按下手表应用程序上的按钮时,会调用“playPauseButtonPressed”功能。此函数启动到Raspberry Pi的网络连接并发送字符串
正如我所说,它可以在手表模拟器上工作,但不能在真正的设备上工作
请注意,在真实设备上的iOS应用程序中也可以使用相同的代码
任何帮助都将不胜感激 好的,我让它工作了。与此同时,我升级到了Watchkit2.0SDK。如果有人感兴趣,以下是我的代码(仍然需要工作): 手表扩展的extensionelegate.m:
- (void)applicationDidFinishLaunching
{
// Perform any final initialization of your application.
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
}
- (IBAction)playPauseButtonPressed
{
[self sendCommandToPhone:@"pause"];
}
- (IBAction)seekBackwardButtonPressed
{
[self sendCommandToPhone:@"seek_backward"];
}
- (IBAction)seekForwardButtonPressed
{
[self sendCommandToPhone:@"seek_forward"];
}
- (IBAction)subtitleButtonPressed
{
[self sendCommandToPhone:@"subtitle"];
}
- (void)sendCommandToPhone:(NSString *)command
{
NSLog(@"sendCommandToPhone called");
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:command, @"command", nil];
[[WCSession defaultSession] sendMessage:dict
replyHandler:^(NSDictionary *replyHandler) {
}
errorHandler:^(NSError *error) {
NSLog(@"%@", [error localizedDescription]);
}
];
}
手表分机的InterfaceController.m:
- (void)applicationDidFinishLaunching
{
// Perform any final initialization of your application.
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
}
- (IBAction)playPauseButtonPressed
{
[self sendCommandToPhone:@"pause"];
}
- (IBAction)seekBackwardButtonPressed
{
[self sendCommandToPhone:@"seek_backward"];
}
- (IBAction)seekForwardButtonPressed
{
[self sendCommandToPhone:@"seek_forward"];
}
- (IBAction)subtitleButtonPressed
{
[self sendCommandToPhone:@"subtitle"];
}
- (void)sendCommandToPhone:(NSString *)command
{
NSLog(@"sendCommandToPhone called");
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:command, @"command", nil];
[[WCSession defaultSession] sendMessage:dict
replyHandler:^(NSDictionary *replyHandler) {
}
errorHandler:^(NSError *error) {
NSLog(@"%@", [error localizedDescription]);
}
];
}
iPhone应用程序的ViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
// Init communication with watch
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
NSLog(@"WCSession initialized");
}
}
// Message received from the watch
- (void)session:(WCSession * _Nonnull)session didReceiveMessage:(NSDictionary<NSString *, id> * _Nonnull)message
{
NSLog(@"didReceiveMessage called");
[self sendCommandToPlayerAndCloseConnection:message[@"command"]];
}
- (IBAction)connectButtonPressed:(id)sender
{
[self sendCommandToPlayer:@"list_dir"];
[self performSegueWithIdentifier:@"toFileTableViewController" sender:self];
}
- (IBAction)seekBackwardButtonPressed:(id)sender
{
[self sendCommandToPlayerAndCloseConnection:@"seek_backward"];
}
- (IBAction)seekForwardButtonPressed:(id)sender
{
[self sendCommandToPlayerAndCloseConnection:@"seek_forward"];
}
- (IBAction)stopButtonPressed:(id)sender
{
[self sendCommandToPlayerAndCloseConnection:@"stop"];
}
- (IBAction)playPauseButtonPressed:(id)sender
{
[self sendCommandToPlayerAndCloseConnection:@"pause"];
}
- (void)sendCommandToPlayer:(NSString *) command
{
NSLog(@"%@", [NSString stringWithFormat:@"Sending command to player : %@", command]);
[self initNetworkCommunication];
NSData *data = [[NSData alloc] initWithData:[command dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
}
- (void)sendCommandToPlayerAndCloseConnection:(NSString *) command
{
[self sendCommandToPlayer:command];
[self closeNetworkCommunication];
}
-(void)initNetworkCommunication
{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.10.116", 10023, &readStream, &writeStream);
if(!CFWriteStreamOpen(writeStream)) {
NSLog(@"Error, writeStream not open");
return;
}
inputStream = (__bridge NSInputStream *) readStream;
outputStream = (__bridge NSOutputStream *) writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}
-(void)closeNetworkCommunication
{
[inputStream close];
[outputStream close];
[inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream setDelegate:nil];
[outputStream setDelegate:nil];
inputStream = nil;
outputStream = nil;
}
-(void)viewDidLoad
{
[超级视图下载];
//与手表的初始化通信
如果([WCSession isSupported]){
WCSession*会话=[WCSession defaultSession];
session.delegate=self;
[会话激活会话];
NSLog(@“WCSession initialized”);
}
}
//从手表收到的信息
-(void)会话:(WCSession*\u Nonnull)会话didReceiveMessage:(NSDictionary*\u Nonnull)消息
{
NSLog(@“调用didReceiveMessage”);
[self-sendCommandToPlayerAndCloseConnection:消息[@“命令”];
}
-(iAction)连接按钮按下:(id)发送方
{
[self-sendCommandToPlayer:@“列表目录”];
[self-PerformsgueWithIdentifier:@“toFileTableViewController”发送方:self];
}
-(iAction)seekbackward按钮按下:(id)发件人
{
[self-SendCommandTopLayer和CloseConnection:@“向后搜索”];
}
-(iAction)请参见按下的前进按钮:(id)发件人
{
[self-SendCommandTopLayer和CloseConnection:@“seek_forward”];
}
-(iAction)停止按钮按下:(id)发件人
{
[self-SendCommandToplayer和CloseConnection:@“停止”];
}
-(iAction)播放暂停按钮按下:(id)发送方
{
[self-SendCommandToplayer和CloseConnection:@“暂停”];
}
-(void)sendCommandToPlayer:(NSString*)命令
{
NSLog(@“%@,[NSString stringWithFormat:@“向播放器发送命令:%@”,命令]);
[自启动网络通信];
NSData*data=[[NSData alloc]initWithData:[命令数据使用编码:NSASCISTRINGENCODE]];
[outputStream写入:[数据字节]最大长度:[数据长度]];
}
-(void)sendCommandToPlayerAndCloseConnection:(NSString*)命令
{
[self-sendCommandToPlayer:command];
[自我封闭网络通信];
}
-(无效)网络通信
{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL,(CFStringRef)@“192.168.10.116”、10023、readStream和writeStream);
如果(!CFWriteStreamOpen(writeStream)){
NSLog(@“错误,writeStream未打开”);
返回;
}
inputStream=(uu桥NSInputStream*)readStream;
outputStream=(uu桥NSOutputStream*)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];
[outputStream ScheduleRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];
[输入流打开];
[输出流打开];
}
-(无效)近网通信
{
[输入流关闭];
[输出流关闭];
[inputStream removeFromRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];
[outputStream removeFromRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];
[inputStream setDelegate:nil];
[outputStream setDelegate:nil];
inputStream=nil;
outputStream=nil;
}
现在我可以通过手机和手表控制omxplayer;) TCP连接的东西在手表上不工作吗?我也有同样的问题,都是在模拟器上工作,但不是在真正的手表上。我不知道,正如我在回答中所写的,我现在使用WCSession与手机应用程序通信,所有的网络逻辑都是从应用程序中完成的