본문 바로가기
Android

Bluetooth file & text transfer #2 server

by 캡틴노랑이 2021. 3. 17.
반응형

test device : note 4

android os : Marshmallow 6.0

 

server source

 

Source

activity_bluetooth1.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Bluetooth1Activity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/tbToolBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme"
            app:title="Bluetooth Server" />
 
        <TextView
            android:id="@+id/tvLog"
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:text="server"
            />
 
        <Button
            android:id="@+id/btnSendText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Send Text"
            android:onClick="btnSendTextOnClick"
            />
 
        <Button
            android:id="@+id/btnSendFile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Send File"
            android:onClick="btnSendFileOnClick"
            />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

 

Bluetooth1Activity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<code>package com.example.sampleproject;
 
 
import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
 
import com.example.sampleproject.Data.BlueToothData;
import com.example.sampleproject.Data.SettingValue;
import com.example.sampleproject.Helper.DataTypeHelper;
import com.example.sampleproject.Norang.NorangActivity;
import com.example.sampleproject.Norang.SettingData;
import com.example.sampleproject.Data.BlueToothData;
 
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
 
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
 
import android.bluetooth.BluetoothServerSocket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.nio.charset.Charset;
 
 
public class Bluetooth1Activity extends NorangActivity {   
    private TextView tvLog;
 
    private BluetoothAdapter bluetoothAdapter;
    private BluetoothServerSocket serverSocket;
    private BluetoothSocket socket;
 
    private ServerThread serverThread;
    private ListenerThread listenerThread;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bluetooth1);
 
        InitializeControl();
        InitializeCommonDataBind();
    }
 
    protected void InitializeControl() {
        toolbar = findViewById(R.id.tbToolBar);
        setToolbar("");
 
        tvLog=findViewById(R.id.tvLog);
 
         
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_EXTERNAL_STORAGE)) {
            } else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
            }
        }
 
         
        if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
    }
 
    protected void InitializeCommonDataBind() {
         
        bluetoothAdapter= BluetoothAdapter.getDefaultAdapter();
        if(bluetoothAdapter==null){
            Toast.makeText(this, "This machine doesn't have bluetooth.", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
         
        if(bluetoothAdapter.isEnabled()){
            //서버소켓 생성 작업 실행
            CreateBluetoothSocket();
 
        }else{
             
            Intent intent= new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(intent, SettingValue.BT_REQ_ENABLE);
        }
    }
 
    private Handler listenerHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            BlueToothData data = (BlueToothData)msg.obj;
 
            switch(data.getMessage_type())
            {
                case TEXT:
                    tvLog.append("new " + new String(data.getData(), 0, data.getData().length));
                    break;
                case FILE:
                    Log.d("listenerHandler", "receive file");
 
                    try{
                        Log.d("listenerHandler", "Save");
                        File file = new File("/storage/emulated/0/1234/00001.jpg"); //new File(data.getFileName());
                        FileOutputStream fos = new FileOutputStream(file);
 
                        fos.write(data.getData());
                        fos.close();
                        Log.d("listenerHandler", "End");
                        tvLog.append("wrote the file.");
                    }catch(Throwable e){
                        Log.e("file", "listenerHandler: Error writing to file. " + e.getMessage() );
                    }
                    break;
            }
 
        }
    };
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
 
        switch (requestCode){
            case SettingValue.BT_REQ_ENABLE:
                if(resultCode == RESULT_CANCELED){                   
                    Toast.makeText(this, "not allow bluetooth.", Toast.LENGTH_SHORT).show();
                    finish();
                }else{                   
                    CreateBluetoothSocket();
                }
                break;
            case SettingValue.BT_REQ_DISCOVERYABLE:
                if(resultCode == RESULT_CANCELED)
                    Toast.makeText(this, "can't find bluetooth.", Toast.LENGTH_SHORT).show();
                break;
        }
 
    }// onActivityResult
     
    protected void CreateBluetoothSocket(){
        serverThread= new ServerThread();
        serverThread.start();
         
        AllowDiscovery();
    }
     
    protected void AllowDiscovery(){
     
        Intent intent= new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);    
        intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300);// 300초
        startActivityForResult(intent, SettingValue.BT_REQ_DISCOVERYABLE);
    }
 
    public void btnSendTextOnClick(View view)
    {
        listenerThread.write(SettingValue.BT_MESSAGE_TYPE.TEXT, "Server sent test message.\n");
        //listenerThread.write("Server sent test message.\n".getBytes());
    }
 
    public void btnSendFileOnClick(View view)
    {
        ///storage/self/primary/DCIM/Camera/20190616_233731.jpg //1m
        //listenerThread.write(SettingValue.BT_MESSAGE_TYPE.FILE, "/storage/emulated/0/Download/m_1548925438_9398_i16252312250.gif");
        //listenerThread.write(SettingValue.BT_MESSAGE_TYPE.FILE, "/storage/emulated/0/DCIM/Camera/20190616_233731.jpg");  //1m
        listenerThread.write(SettingValue.BT_MESSAGE_TYPE.FILE, "/storage/emulated/0/PantaSCAN/XXXX/Original/00001.jpg"); //500k
    }
    @Override
    protected void onDestroy() {
        try {
            serverThread.interrupt();
            listenerThread.interrupt();
            listenerThread.interrupt();
            socket.close();
        } catch(Exception e) { }
 
        super.onDestroy();
    }
 
    //inner class////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public class ServerThread extends Thread{
        @Override
        public void run() {
            try {               
                serverSocket = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("SERVER",SettingValue.BT_UUID);
                showText("\nCreated sever socket.\nWaiting client.\n");
                 
                socket = serverSocket.accept();
                showText("Client connected.\n");
 
                listenerThread = new ListenerThread(socket);
                listenerThread.start();
 
            } catch (IOException e) {
                Log.e("ServerThread", e.getMessage());
            }
        }//run() ..
 
        public void showText(final String msg){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvLog.append(msg);
                }
            });
        }
 
    }//ServerThread
 
    public class ListenerThread extends Thread {
 
        protected BluetoothSocket mSocket;
        protected InputStream inStream;
        protected OutputStream outStream;
 
        private int nSendingType = 0; //0 length, 1 data
        private int nDataSize = 0;
 
        public ListenerThread(BluetoothSocket socket) {
            showText("ListenerThread will run.\n");
            mSocket = socket;
 
            InputStream tmpIn = null;
            OutputStream tmpOut = null;
 
            try {
                tmpIn = mSocket.getInputStream();
                tmpOut = mSocket.getOutputStream();
            } catch (IOException e) {
                Log.e("ListenerThread", e.getMessage());
            }
 
            inStream = tmpIn;
            outStream = tmpOut;
        }
 
        public void run() {
            byte[] buffer;  // buffer store for the stream
 
            showText("ListenerThread is waiting.\n");
 
            // Keep listening to the InputStream until an exception occurs
            while (true) {
                // Read from the InputStream
                buffer = new byte[SettingValue.BT_SEND_DATABUFFER_SIZE];
 
                try {
                    if(nSendingType == 0)
                    {
                        //Log.e("nSendingType", Integer.toString(nSendingType));
                        inStream.read(buffer);
                        ByteArrayInputStream byteIn = new ByteArrayInputStream(buffer);
                        nDataSize = (int)(new ObjectInputStream(byteIn)).readObject();
                        nSendingType = 1;
 
                        //Log.e("nDataSize", Integer.toString(nDataSize));
                    }
                    else
                    {
                        int byteNo = 0;
                        int writeByte = 0;
                        int bufferSize = nDataSize;
 
                        Log.e("elseFileDataSize", Integer.toString(nDataSize));
                        while (nDataSize != writeByte)
                        {
                            byteNo = inStream.read(buffer, writeByte, bufferSize);
                            writeByte += byteNo;
                            bufferSize = bufferSize - byteNo;
 
                            Log.e("writeByte", Integer.toString(writeByte));
                            Log.e("byteNo", Integer.toString(byteNo));
                        }
 
                        ByteArrayInputStream byteIn = new ByteArrayInputStream(buffer);
                        BlueToothData data = (BlueToothData)(new ObjectInputStream(byteIn)).readObject();
 
                        //Message send
                        Message msg = new Message();
                        msg.what = 1;
                        msg.obj = (BlueToothData)data;
                        listenerHandler.sendMessage(msg);
 
                        nSendingType = 0;
                        nDataSize = 0;
                    }
 
                } catch (Exception e) {
                    Log.e("ListenerThread run", e.getMessage());
                    break;
                }
            }
        }       
        public void write(SettingValue.BT_MESSAGE_TYPE message_type, String text) {
 
            try {
                BlueToothData data  = new BlueToothData();
                byte[] buffer = new byte[10240000]; //10M
 
                switch (message_type)
                {
                    case TEXT : //SettingValue.BT_MESSAGE_TYPE.TEXT  enum은 값만 쓰면됨.
                        Log.d("write", "write: Writing to outputstream: " + text);
 
                        data.setMessage_type(message_type);
                        data.setData(text.getBytes());
 
                        break;
                    case FILE:                       
                        try{
                            FileInputStream fis = new FileInputStream(text);
                            BufferedInputStream bis = new BufferedInputStream(fis);
 
                            int length = bis.read(buffer);
 
                            Log.d("file", "start to send file.");
 
                            data = new BlueToothData();
                            data.setMessage_type(SettingValue.BT_MESSAGE_TYPE.FILE);
                            data.setFileName(text);
                            data.setData(buffer);
 
                        } catch (Exception e) {
                            Log.e("write", "file read Error. " + e.getMessage() );
                        }
                        break;
                }
                
                try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
                    try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
                        oos.writeObject(data);
                        showText(Integer.toString(baos.toByteArray().length)+ "\n");
 
                        //////////////////////////////////
                        //send byte length.
                        ByteArrayOutputStream baosL = new ByteArrayOutputStream();
                        ObjectOutputStream oosL = new ObjectOutputStream(baosL);
                        oosL.writeObject(baos.toByteArray().length);
 
                        showText(String.valueOf(baosL.toByteArray().length) + "\n");
                        outStream.write(baosL.toByteArray());
                        outStream.flush();
                        //send byte length.
                        //////////////////////////////////
 
 
                        //send main data.
                        outStream.write(baos.toByteArray());
                        outStream.flush();
 
                    } catch (Exception e) {
                        Log.e("write", "ObjectOutputStream Error. " + e.getMessage());
                    }
                }
                catch(Exception e)
                {
                    Log.e("write", "ByteArrayOutputStream Error. " + e.getMessage() );
                }
                //data send
            } catch (Exception e) {
                Log.e("write", "write: Error writing to output stream. " + e.getMessage() );
            }
        }
 
        public void showText(final String msg){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvLog.append(msg);
                }
            });
        }
 
    }//ListenerThread
 
    //inner class////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
}
 
</code>

*NorangActivity는 기본 Activity사용하면 됨. 

이것으로 인한 듣보잡 메서드는 삭제 하면 잘 돌아 갈 것임...

 

Marshmallow 6.0에서만 테스트 되었음. 그 이외의 버전은... 안될 수 있음... 

반응형

'Android' 카테고리의 다른 글

Android Barcode Create  (555) 2021.03.17
Bluetooth file & text transfer #3 client  (4) 2021.03.17
Bluetooth file & text transfer #1 etc  (6) 2021.03.17
Android ListView Sorting  (6) 2021.01.03
Android Error Message  (2) 2021.01.01

댓글