- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
GameKit
框架有问题。在我的应用程序中,我想发送文本或图像。现在,当我从 UIImagePicker
发送图像时,效果很好。当我发送文本时,它不会为图像显示相同的 UIAlertView
,没有图像。这是 .h
:
#import <UIKit/UIKit.h>
#import <GameKit/GameKit.h>
@interface ViewController : UIViewController <GKPeerPickerControllerDelegate, GKSessionDelegate, UIActionSheetDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
GKSession *currentSession;
IBOutlet UITextField *txtMessage;
IBOutlet UIButton *connect;
IBOutlet UIButton *disconnect;
GKPeerPickerController *picker;
UIImagePickerController *imgPicker;
BOOL isText;
UIImage *_image;
}
@property (nonatomic, retain) GKSession *currentSession;
@property (nonatomic, retain) UITextField *txtMessage;
@property (nonatomic, retain) UIButton *connect;
@property (nonatomic, retain) UIButton *disconnect;
-(IBAction) btnSend:(id) sender;
-(IBAction) btnConnect:(id) sender;
-(IBAction) btnDisconnect:(id) sender;
-(IBAction)chooseImageData:(id)sender;
@end
和 .m
:
#import "ViewController.h"
#import <GameKit/GameKit.h>
@implementation ViewController
@synthesize currentSession;
@synthesize txtMessage;
@synthesize connect;
@synthesize disconnect;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad {
[connect setHidden:NO];
[disconnect setHidden:YES];
[super viewDidLoad];
}
- (void)peerPickerController:(GKPeerPickerController *)picker
didConnectPeer:(NSString *)peerID
toSession:(GKSession *) session {
self.currentSession = session;
session.delegate = self;
[session setDataReceiveHandler:self withContext:nil];
picker.delegate = nil;
[picker dismiss];
}
-(IBAction) btnConnect:(id) sender {
picker = [[GKPeerPickerController alloc] init];
picker.delegate = self;
picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
[connect setHidden:YES];
[disconnect setHidden:NO];
[picker show];
}
- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker
{
picker.delegate = nil;
[connect setHidden:NO];
[disconnect setHidden:YES];
}
-(IBAction) btnDisconnect:(id) sender {
[self.currentSession disconnectFromAllPeers];
currentSession = nil;
[connect setHidden:NO];
[disconnect setHidden:YES];
}
- (void)session:(GKSession *)session
peer:(NSString *)peerID
didChangeState:(GKPeerConnectionState)state {
switch (state)
{
case GKPeerStateConnected:
NSLog(@"connected");
break;
case GKPeerStateDisconnected:
NSLog(@"disconnected");
currentSession = nil;
[connect setHidden:NO];
[disconnect setHidden:YES];
break;
}
}
- (void) mySendDataToPeers:(NSData *) data
{
if (currentSession)
[self.currentSession sendDataToAllPeers:data
withDataMode:GKSendDataReliable
error:nil];
}
- (void) mySendImageToPeers:(NSData *) data
{
if (currentSession)
[self.currentSession sendDataToAllPeers:data
withDataMode:GKSendDataReliable
error:nil];
}
-(IBAction) btnSend:(id) sender
{
//---convert an NSString object to NSData---
NSData* data;
NSString *str = [NSString stringWithString:txtMessage.text];
data = [str dataUsingEncoding: NSASCIIStringEncoding];
[self mySendDataToPeers:data];
}
- (void) receiveData:(NSData *)data
fromPeer:(NSString *)peer
inSession:(GKSession *)session
context:(void *)context {
//---convert the NSData to NSString---
if (isText) {
isText = YES;
NSString* str;
str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Messaggio"
message:str
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
else
{
UIImage *tempSentImg = [UIImage imageWithData:data];
_image = tempSentImg;
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Immagine"
message:@"\n\n\n\n\n"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
UIImageView *imageView = [[UIImageView alloc] initWithImage:_image];
CGFloat imageHeight = 100;
CGFloat imageWidth = imageHeight * _image.size.width / _image.size.height;
imageView.frame = CGRectMake(floor((284 - imageWidth)/2), 47, imageWidth, imageHeight);
[alert addSubview:imageView];
[alert show];
return;
}
}
-(IBAction)chooseImageData:(id)sender {
[self performSelector: @selector(dialogOtherAction)];
}
- (void)dialogOtherAction {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:NSLocalizedString(@"Annulla", @"")
destructiveButtonTitle:nil
otherButtonTitles:NSLocalizedString(@"Scegli dalla libreria", @""),
NSLocalizedString(@"Scatta una foto", @""),nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
imgPicker = [[UIImagePickerController alloc] init];
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imgPicker.allowsEditing = NO;
imgPicker.delegate = self;
[self presentModalViewController:imgPicker animated:YES];
}
else if (buttonIndex == 1)
{
imgPicker = [[UIImagePickerController alloc] init];
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imgPicker.delegate = self;
imgPicker.allowsEditing = NO;
[self presentModalViewController:imgPicker animated:YES];
}
}
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)imgPicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissModalViewControllerAnimated:YES];
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[self performSelector: @selector(btnImageSend:) withObject: image afterDelay: 1.0];
}
-(IBAction) btnImageSend:(UIImage *)image
{
//---convert an NSString object to NSData---
NSData* data;
data = UIImagePNGRepresentation(image);
[self mySendImageToPeers:data];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
最佳答案
在我看来你无法知道接收端发送了什么。最初“isText”变量为 NO。每次接收数据时(在 receiveData:fromPeer:inSession:context: 中),if (isText) 总是失败并且接收到的数据总是被解释为图像数据。
解决此问题的一种方法是,您可以发送字典,而不是简单地发送文本或图像数据。对 mySendDataToPeers:、mySendImageToPeers: 和 receiveData:fromPeer:inSession:context: 进行以下更改:
- (void) mySendDataToPeers:(NSData *) data
{
NSDictionary *dict = @{@"data type" : @"text",
@"data" : data};
NSData *dictData = [NSPropertyListSerialization dataWithPropertyList:dict format:NSPropertyListBinaryFormat_v1_0 options:0 error:nil];
if (currentSession)
[self.currentSession sendDataToAllPeers:dictData
withDataMode:GKSendDataReliable
error:nil];
}
- (void) mySendImageToPeers:(NSData *) data
{
NSDictionary *dict = @{@"data type" : @"image",
@"data" : data};
NSData *dictData = [NSPropertyListSerialization dataWithPropertyList:dict format:NSPropertyListBinaryFormat_v1_0 options:0 error:nil];
if (currentSession)
[self.currentSession sendDataToAllPeers:dictData
withDataMode:GKSendDataReliable
error:nil];
}
- (void) receiveData:(NSData *)dictdata
fromPeer:(NSString *)peer
inSession:(GKSession *)session
context:(void *)context
{
NSDictionary *dict = (NSDictionary *) [NSPropertyListSerialization propertyListWithData:dictdata options:0 format:0 error:nil];
isText = [@"text" isEqualToString:[dict objectForKey:@"data type"]];
NSData *data = [dict objectForKey:@"data"];
//---convert the NSData to NSString---
if (isText)
{
NSString* str;
str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Messaggio"
message:str
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
else
{
UIImage *tempSentImg = [UIImage imageWithData:data];
_image = tempSentImg;
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Immagine"
message:@"\n\n\n\n\n"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
UIImageView *imageView = [[UIImageView alloc] initWithImage:_image];
CGFloat imageHeight = 100;
CGFloat imageWidth = imageHeight * _image.size.width / _image.size.height;
imageView.frame = CGRectMake(floor((284 - imageWidth)/2), 47, imageWidth, imageHeight);
[alert addSubview:imageView];
[alert show];
return;
}
}
关于iPhone - 通过蓝牙发送图像和文本的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10432378/
是否可以识别我周围启用了蓝牙的设备?我不需要与他们交流,只知道他们就在那里。 我正在寻找类似于 android 的 BluetouthDevice.startDiscovery() 的东西 这样的事情
蓝牙的 HTTP 代理服务是否允许我将 BLE 设备视为 HTTP 服务器,例如以便与设备对话的应用可以向其发送 GET/POST/PUT 请求? 或者这个操作是相反的方向,BLE 设备通过应用程序向
1、蓝牙常识点 1、常见英文缩写 缩写 英文全称 释义 BLE Bluetooth Low Energy 低功耗
我正在与BlueZ库一起在Linux下管理蓝牙堆栈。我正在尝试打开一个套接字,该套接字应与已知UUID的特定服务连接。我已成功尝试按照以下示例在服务器和客户端之间打开套接字: http://peopl
有谁知道蓝牙设备如何获取范围内可发现设备的设备 ID? 理想情况下,我正在寻找涉及蓝牙协议(protocol)最小实现的最简单解决方案。 一个起点会很好,我只是想创建一个设备,它可以以最小的功耗存储附
蓝牙双模设备是否可以在与 BT LE 设备配对的同时被经典蓝牙发现?如果设备不能同时运行这两种模式也没关系,但我真的应该在这些模式之间切换芯片吗?我只是在 BT 4 Core 规范中找不到答案 最佳答
我目前正在开展一个涉及乐高 Mindstorms 套件的项目。砖 block 是 NXT,我对蓝牙 ping 速率很好奇。 我对其进行了 100 次 ping 测试,得到了一些有趣的结果。延迟似乎分为
我正在启动一个通过蓝牙进行无线 MIDI 连接的项目。据我所知,BT规范中没有定义MIDI配置文件。 我想知道你们中的一些人是否有兴趣分享有关通过 BT 使用 MIDI 的最佳方式的经验,特别是关于延
Closed. This question is off-topic。它当前不接受答案。
我想通过蓝牙将我的摩托罗拉机器人连接到 OBDKey。我以 BluetoothChat 为例连接蓝牙,使用 KWP 作为协议(protocol) 然后我写byte[]命令 command[0]=ra
几个月前,我用 C# 编写了一个 Messenger 程序,可以让许多客户端连接到服务器并进行聊天。 现在,我想为 android 编写相同的程序。在阅读了 Android Developers 中的
我目前正在制作一个与蓝牙相关的 Android 实用程序,我需要更改我的设备的设备发现范围.. 我有办法这样做吗?我目前正在考虑使用 TPL 来执行此操作,但我不太确定.. Android 应用程序或
我正在为两个玩家构建 tic tac,需要蓝牙连接来交换一些数据,我可以启用蓝牙,启用发现能力,但我不知道“BluetoothServerSocket”和客户端“BluetoothSocket”中的问
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visi
我正在 Microsoft visual studio express 2012,C++ 中制作一个程序,以便与具有此 mac 地址的设备建立简单的蓝牙连接:“00:12:08:24:15:50”,
我正在为 python import bluetooth 使用蓝牙模块,我相信它是 PyBluez 包。我能够从 bluetooth.BluetoothSocket 类进行连接、发送和接收,但我的应用
我正在为 python import bluetooth 使用蓝牙模块,我相信它是 PyBluez 包。我能够从 bluetooth.BluetoothSocket 类进行连接、发送和接收,但我的应用
我尝试通过以下命令来做到这一点: ./configure -developer-build -opensource -nomake examples -nomake tests make module
我有一个服务,理论上可以在没有关联 Activity 的情况下工作(因为“服务”适用于 Android 平台)。 此服务使用蓝牙,特别是注册一个具有给定名称的蓝牙服务来监听通信。当然,它必须启用蓝牙才
谁知道是否可以制作一个应用程序通过蓝牙模拟触摸屏鼠标或触控板? 如何让 PC(或 MAC)知道我是鼠标设备? 问候, 胡安 最佳答案 您应该看看蓝牙 HID 规范。这可能是可能的,具体取决于您用来模拟
我是一名优秀的程序员,十分优秀!