質問編集履歴

1

ソースコード掲載

2017/07/05 15:12

投稿

DJ1984
DJ1984

スコア19

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,203 @@
1
1
  今AndroidとPCとをBluetoothで接続しファイルの送受信を行うプログラムを書いています。
2
2
 
3
3
  しかし、送信するデータが大きすぎるとうまく受信できないようです。質問なのですがBluetoothでは一度にデータを送受信できる限界値があるのでしょうか?
4
+
5
+
6
+
7
+ 送信側(PC)プログラム
8
+
9
+ ```Java
10
+
11
+ //選択したファイルの情報をまとめる
12
+
13
+ StringBuilder builder = new StringBuilder();
14
+
15
+ for (File file : fileList) {
16
+
17
+ builder.append(file.getName());
18
+
19
+ builder.append(FILE_SEPARATOR);
20
+
21
+ builder.append(file.length());
22
+
23
+ builder.append(DATA_SEPARATOR);
24
+
25
+ }
26
+
27
+
28
+
29
+ sessionTools.getController().appendToConsole("Send file info:" + builder.toString());
30
+
31
+
32
+
33
+ String encodedMessage = URLEncoder.encode(builder.toString(), "UTF-8");
34
+
35
+ BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(sessionTools.getOutputStream()));
36
+
37
+ bufferedWriter.write(encodedMessage);
38
+
39
+ bufferedWriter.newLine();
40
+
41
+ bufferedWriter.flush();
42
+
43
+
44
+
45
+ BufferedOutputStream outputStream = new BufferedOutputStream(sessionTools.getOutputStream());
46
+
47
+ for (File file : fileList) {
48
+
49
+ sessionTools.getController().initProgressBar(String.format("%sを送信", file.getName()), 0);
50
+
51
+
52
+
53
+ byte[] data = readFile(file);
54
+
55
+ int sum = data.length;
56
+
57
+ int divide = 1024;
58
+
59
+ int divisionCount = sum / divide;
60
+
61
+ int remainder = sum % divide;
62
+
63
+
64
+
65
+ if (divisionCount < 1) {
66
+
67
+ outputStream.write(data);
68
+
69
+ outputStream.flush();
70
+
71
+ } else {
72
+
73
+ for (int index = 0; index < divisionCount; index++) {
74
+
75
+ outputStream.write(data, index * divide, divide);
76
+
77
+ outputStream.flush();
78
+
79
+
80
+
81
+ double percentage = (double) (index * divide) / sum;
82
+
83
+ sessionTools.getController().updateProgress(String.format("%sを送信中 %d %%", file.getName(), (int) (percentage * 100)), percentage);
84
+
85
+ }
86
+
87
+
88
+
89
+ if (remainder > 0) {
90
+
91
+ outputStream.write(data, sum - remainder, remainder);
92
+
93
+ outputStream.flush();
94
+
95
+ }
96
+
97
+
98
+
99
+ sessionTools.getController().finishedProgress(String.format("%s送信完了", file.getName()));
100
+
101
+ sessionTools.getController().appendToConsole("Send " + file.getName());
102
+
103
+ }
104
+
105
+ }
106
+
107
+ ```
108
+
109
+
110
+
111
+ 受信側(Android)プログラム
112
+
113
+ ```Java
114
+
115
+ //ファイル情報をもとにデータを受信
116
+
117
+
118
+
119
+ //ファイル情報を取得
120
+
121
+ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(sessionTools.getInputStream()));
122
+
123
+ String msg = bufferedReader.readLine();
124
+
125
+ msg = URLDecoder.decode(msg, "UTF-8");
126
+
127
+
128
+
129
+ if(msg.equals(CANCEL_MESSAGE)){
130
+
131
+ exception = new Exception("サーバがファイル送信コマンドをキャンセルしました");
132
+
133
+ return -1;
134
+
135
+ }
136
+
137
+
138
+
139
+ //ファイル情報を解析してMapに入れる
140
+
141
+ String[] fileDatas = msg.split(DATA_SEPARATOR);
142
+
143
+ for(String fileData : fileDatas){
144
+
145
+ String fileName = fileData.split(FILE_SEPARATOR)[0];
146
+
147
+ int size = Integer.parseInt(fileData.split(FILE_SEPARATOR)[1]);
148
+
149
+
150
+
151
+ fileMap.put(fileName, size);
152
+
153
+ }
154
+
155
+ byte[] dataBuffer = new byte[BUFFER_SIZE];
156
+
157
+ BufferedInputStream bufferedInputStream = new BufferedInputStream(sessionTools.getInputStream());
158
+
159
+ for(String fileName: fileMap.keySet()){
160
+
161
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
162
+
163
+ int dataSize = fileMap.get(fileName);
164
+
165
+ int current = 0;
166
+
167
+ while ((len = bufferedInputStream.read(dataBuffer)) != -1) {
168
+
169
+ if (dataSize - current < BUFFER_SIZE) {
170
+
171
+ byte[] tempBuffer = new byte[dataSize - current];
172
+
173
+ len = bufferedInputStream.read(tempBuffer);
174
+
175
+ byteArrayOutputStream.write(tempBuffer, 0, len);
176
+
177
+ break;
178
+
179
+ } else {
180
+
181
+ byteArrayOutputStream.write(dataBuffer, 0, len);
182
+
183
+ current += len;
184
+
185
+ }
186
+
187
+ double percentage = ((double)current / dataSize) * 100;
188
+
189
+ publishProgress((int)percentage);
190
+
191
+ }
192
+
193
+ byteArrayOutputStream.flush();
194
+
195
+ saveFile(fileName, byteArrayOutputStream.toByteArray());
196
+
197
+
198
+
199
+ publishProgress(100);
200
+
201
+ }
202
+
203
+ ```