从Apple Watch应用程序到其他设备的本地网络连接可在模拟器上运行,但不在Watch上运行

时间:2015-07-25 20:27:27

标签: ios objective-c apple-watch

我正试图为Apple Watch构建一个应用程序来远程控制Raspberry Pi上的媒体播放器(omxplayer)。

我在Xcode的模拟器中使用它,但它并不适用于实际的手表。

在Raspberry Pi上,我有一个简单的python脚本,可以启动TCP服务器并监听命令。

在WatchKit Extension projet的InterfaceController.m文件中,我有以下代码:

- (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的网络连接并发送字符串。

正如我所说,它适用于Watch模拟器,但不适用于真实设备。

请注意,相同的代码适用于真实设备上的iOS应用。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:3)

好的我工作了。与此同时,我升级到了WatchKit 2.0 SDK。如果有人感兴趣,这是我的代码(仍然需要工作):

观看扩展程序的ExtensionDelegate.m:

- (void)applicationDidFinishLaunching
{
    // Perform any final initialization of your application.

    if ([WCSession isSupported]) {
        WCSession *session = [WCSession defaultSession];
        session.delegate = self;
        [session activateSession];
    }
}

手表扩展程序的InterfaceController.m:

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

现在我可以通过手机和手表控制omxplayer了;)