質問編集履歴
2
質問変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,25 +6,47 @@
|
|
6
6
|
|
7
7
|
###発生している問題・エラーメッセージ
|
8
8
|
|
9
|
-
javaで
|
9
|
+
javaでbluetoothを使い画像を送信したいのですがうまくいきません。
|
10
|
+
|
11
|
+
プログラムを動かした際、andoroid端末に接続されている形跡はあるのですが、画像として受信されません。どうしたらよいのでしょうか・
|
10
12
|
|
11
13
|
|
12
14
|
|
13
15
|
###該当のソースコード
|
14
16
|
|
17
|
+
|
18
|
+
|
15
19
|
package bluetooth;
|
16
20
|
|
17
21
|
|
18
22
|
|
19
|
-
import java.awt.Image;
|
20
23
|
|
24
|
+
|
21
|
-
import java.io.File;
|
25
|
+
import java.io.FileInputStream;
|
26
|
+
|
27
|
+
import java.io.FileNotFoundException;
|
28
|
+
|
29
|
+
import java.io.FileOutputStream;
|
22
30
|
|
23
31
|
import java.io.IOException;
|
24
32
|
|
25
33
|
import java.io.InputStream;
|
26
34
|
|
27
35
|
import java.io.OutputStream;
|
36
|
+
|
37
|
+
import java.io.FileInputStream;
|
38
|
+
|
39
|
+
import java.io.FileNotFoundException;
|
40
|
+
|
41
|
+
import java.io.FileOutputStream;
|
42
|
+
|
43
|
+
import java.io.IOException;
|
44
|
+
|
45
|
+
import java.io.InputStream;
|
46
|
+
|
47
|
+
import java.io.OutputStream;
|
48
|
+
|
49
|
+
import java.awt.Image;
|
28
50
|
|
29
51
|
|
30
52
|
|
@@ -36,203 +58,63 @@
|
|
36
58
|
|
37
59
|
import javax.microedition.io.OutputConnection;
|
38
60
|
|
39
|
-
import javax.microedition.io.StreamConnection;
|
40
61
|
|
41
|
-
import javax.bluetooth.BluetoothStateException;
|
42
62
|
|
43
|
-
|
63
|
+
public class file {
|
44
|
-
|
45
|
-
import javax.bluetooth.DiscoveryAgent;
|
46
|
-
|
47
|
-
import javax.bluetooth.DiscoveryListener;
|
48
|
-
|
49
|
-
import javax.bluetooth.LocalDevice;
|
50
|
-
|
51
|
-
import javax.bluetooth.RemoteDevice;
|
52
|
-
|
53
|
-
import javax.bluetooth.ServiceRecord;
|
54
64
|
|
55
65
|
|
56
66
|
|
67
|
+
public static void main(String[] args)throws Exception {
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
String connectionURL = "btspp://・・・・・・:2";
|
72
|
+
|
73
|
+
Connection connection = Connector.open(connectionURL);
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
try {
|
78
|
+
|
79
|
+
// 画像をバイトストリームで読み込む
|
80
|
+
|
81
|
+
FileInputStream in = new FileInputStream("C:/・・・・・・・aaa.jpg");
|
82
|
+
|
83
|
+
// 画像をそのままバイトストリームで出力
|
84
|
+
|
85
|
+
OutputStream pic = new FileOutputStream("connectionURL");
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
// 読み込み処理
|
92
|
+
|
93
|
+
int num=0;
|
94
|
+
|
95
|
+
byte[] buf = new byte[512];
|
96
|
+
|
97
|
+
while ((num = in.read(buf)) != -1) {
|
98
|
+
|
99
|
+
// 書き込み処理
|
100
|
+
|
57
|
-
|
101
|
+
pic.write(num);
|
102
|
+
|
103
|
+
System.out.println(num);
|
104
|
+
|
105
|
+
}
|
58
106
|
|
59
107
|
|
60
108
|
|
61
|
-
|
109
|
+
// クローズ処理
|
62
110
|
|
63
|
-
in
|
111
|
+
in.close();
|
64
112
|
|
65
|
-
|
113
|
+
pic.close();
|
66
114
|
|
67
|
-
|
115
|
+
} catch (FileNotFoundException e) {
|
68
116
|
|
69
|
-
|
70
|
-
|
71
|
-
// サーバー側EV3のBluetoothのMACアドレス、チャネル=1
|
72
|
-
|
73
|
-
String connectionURL = "btspp:..............";
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
System.out.println("Connecting...");
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
// サーバー側のデバイスからコネクションを取得する
|
82
|
-
|
83
|
-
Connection connection = Connector.open(connectionURL);
|
84
|
-
|
85
|
-
if (connection == null) {
|
86
|
-
|
87
|
-
System.out.println("Connect fail");
|
88
|
-
|
89
|
-
System.exit(1);
|
90
|
-
|
91
|
-
}
|
92
|
-
|
93
|
-
System.out.println("Connected");
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
// 読み、書きのストリームを取得する
|
100
|
-
|
101
|
-
OutputStream os = ((OutputConnection)connection).openOutputStream();
|
102
|
-
|
103
|
-
InputStream is = ((InputConnection)connection).openInputStream();
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
// メッセージを送信する(ここを画像送信にしたい)
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
try {
|
114
|
-
|
115
|
-
StreamConnection conn = (StreamConnection) Connector.open(connectionURL);
|
116
|
-
|
117
|
-
try {
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
File file = new File("C:/Users...aaaa.jpeg");
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
// 何を何バイト送受信するかはデバイスによって異なる
|
126
|
-
|
127
|
-
|
117
|
+
e.printStackTrace();
|
128
|
-
|
129
|
-
is.read();
|
130
|
-
|
131
|
-
} finally {
|
132
|
-
|
133
|
-
conn.close();
|
134
|
-
|
135
|
-
}
|
136
|
-
|
137
|
-
} catch (IOException e) {
|
138
|
-
|
139
|
-
throw new RuntimeException(e);
|
140
|
-
|
141
|
-
}
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
System.out.println("Sending...");
|
148
|
-
|
149
|
-
send(os, sendMessageNo);
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
// 通信の切断を行う
|
156
|
-
|
157
|
-
System.out.println("Closing...");
|
158
|
-
|
159
|
-
try {
|
160
|
-
|
161
|
-
if (null != connection) {
|
162
|
-
|
163
|
-
os.close();
|
164
|
-
|
165
|
-
is.close();
|
166
|
-
|
167
|
-
connection.close();
|
168
|
-
|
169
|
-
}
|
170
|
-
|
171
|
-
} catch (Exception ioe) {
|
172
|
-
|
173
|
-
}
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
System.out.println("Finished");
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
}
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
/**
|
186
|
-
|
187
|
-
* @param codeBase
|
188
|
-
|
189
|
-
* @param string
|
190
|
-
|
191
|
-
* @return
|
192
|
-
|
193
|
-
*/
|
194
|
-
|
195
|
-
private static Image getImage(Object codeBase, String string) {
|
196
|
-
|
197
|
-
// TODO Auto-generated method stub
|
198
|
-
|
199
|
-
return null;
|
200
|
-
|
201
|
-
}
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
/**
|
206
|
-
|
207
|
-
* @return
|
208
|
-
|
209
|
-
*/
|
210
|
-
|
211
|
-
private static Object getCodeBase() {
|
212
|
-
|
213
|
-
// TODO Auto-generated method stub
|
214
|
-
|
215
|
-
return null;
|
216
|
-
|
217
|
-
}
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
// 送信する
|
222
|
-
|
223
|
-
static void send(OutputStream os, int messageNo) {
|
224
|
-
|
225
|
-
byte[] sendBuff = new byte[16];
|
226
|
-
|
227
|
-
for (int i = 0; i < sendBuff.length; i++) {
|
228
|
-
|
229
|
-
sendBuff[i] = (byte) messageNo;
|
230
|
-
|
231
|
-
}
|
232
|
-
|
233
|
-
try {
|
234
|
-
|
235
|
-
os.write(sendBuff);
|
236
118
|
|
237
119
|
} catch (IOException e) {
|
238
120
|
|
@@ -242,55 +124,9 @@
|
|
242
124
|
|
243
125
|
}
|
244
126
|
|
245
|
-
|
127
|
+
}
|
246
128
|
|
247
129
|
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
}
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
###試したこと
|
256
|
-
|
257
|
-
try {
|
258
|
-
|
259
|
-
StreamConnection conn = (StreamConnection) Connector.open(connectionURL);
|
260
|
-
|
261
|
-
try {
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
File file = new File("C:/Users...aaaa.jpeg");
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
// 何を何バイト送受信するかはデバイスによって異なる
|
270
|
-
|
271
|
-
os.write(.getBytes());
|
272
|
-
|
273
|
-
is.read();
|
274
|
-
|
275
|
-
} finally {
|
276
|
-
|
277
|
-
conn.close();
|
278
|
-
|
279
|
-
}
|
280
|
-
|
281
|
-
} catch (IOException e) {
|
282
|
-
|
283
|
-
throw new RuntimeException(e);
|
284
|
-
|
285
|
-
}
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
この当たりの処理がおかしいことは分かるのですが、画像の読み込み、送信などの処理が分かりませんでした。
|
292
|
-
|
293
|
-
|
294
130
|
|
295
131
|
###補足情報(言語/FW/ツール等のバージョンなど)
|
296
132
|
|
1
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
###前提・実現したいこと
|
2
2
|
|
3
|
-
PCからbluetoothでandroid端末に画像送信
|
3
|
+
PCからbluetoothでandroid端末に画像送信したい
|
4
4
|
|
5
5
|
|
6
6
|
|
7
7
|
###発生している問題・エラーメッセージ
|
8
8
|
|
9
|
-
javaでandroid端末にペアリングをするところまではうまくいっているのすが、画像
|
9
|
+
javaでandroid端末にペアリングをするところまではうまくいっているのすが、画像送信がうまくいきません。
|
10
10
|
|
11
11
|
|
12
12
|
|