본문 바로가기
혼자 코딩(안드로이드 feat.JAVA)

[안드로이드 스튜디오 - 자바] 블루투스 연결 _ 002

by 서하란 2023. 2. 14.
반응형
SMALL
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
<?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=".MainActivity">
 
    <TextView
        android:id="@+id/tvBluetoothStatus"
        android:layout_width="170dp"
        android:layout_height="20dp"
        android:layout_marginStart="30dp"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="30dp"
        android:text="status"
        tools:layout_editor_absoluteX="126dp"
        tools:layout_editor_absoluteY="711dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
 
 
 
 
    <Button
        android:id="@+id/btnConnect"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="@+id/tvBluetoothStatus"
        app:layout_constraintTop_toBottomOf="@+id/tvBluetoothStatus"
        android:text="연결하기" />
 
 
    <Button
        android:id="@+id/btnDisConnect"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginLeft="10dp"
        android:text="종료"
        app:layout_constraintBottom_toBottomOf="@+id/btnConnect"
        app:layout_constraintStart_toEndOf="@+id/btnConnect"
        app:layout_constraintTop_toTopOf="@+id/btnConnect" />
 
    <TextView
        android:id="@+id/tvReceiveData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Receive"
        app:layout_constraintStart_toStartOf="@+id/btnSendData"
        app:layout_constraintTop_toBottomOf="@+id/btnSendData" />
 
 
    <TextView
        android:id="@+id/tvReceiveDataView"
        android:layout_width="304dp"
        android:layout_height="527dp"
        android:layout_marginBottom="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/tvReceiveData" />
 
    <Button
        android:id="@+id/btnSendData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="전송"
        app:layout_constraintStart_toStartOf="@+id/btnConnect"
        app:layout_constraintTop_toBottomOf="@+id/btnDisConnect" />
 
    <EditText
        android:id="@+id/tvSendData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Send Data"
        app:layout_constraintStart_toEndOf="@+id/btnSendData"
        app:layout_constraintTop_toTopOf="@+id/btnSendData" />
 
    <ListView
        android:id="@+id/lv_paired_device"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvSendData" />
 
</androidx.constraintlayout.widget.ConstraintLayout>
cs

 

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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package com.example.mybluetooth001_002;
 
import android.Manifest;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
 
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
 
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
 
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
 
 
public class MainActivity extends AppCompatActivity {
    TextView mTvBluetoothStatus;        // 현재 블루투스 상대
    TextView mTvReceiveData;            // 수신된 데이터
    TextView mTvSendData;               // 보낼 데이터 입력란
    Button mBtnConnect;                 // 장치 연결 버튼
    Button mBtnDisConnect;              // 장치 해제 버튼
    Button mBtnSendData;                // 데이터 전송 버튼
    ListView mMessageList;              // 받은 메시지 목록
 
    private BluetoothAdapter mBluetoothAdapter;
    Set<BluetoothDevice> mPairedDevices;            // 페어링 된 적 있는 목록
    List<String> mListPairedDevices;                // 페어링 된 장치 목록
 
    Handler mBluetoothHandler;                      // 스레드간 통신에 사용
 
    final static int BT_MESSAGE_READ = 2;           //데이터 수신했음
    final static UUID BT_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    //final static UUID BT_UUID = UUID.fromString("b101bb80-3338-4b94-a775-b3844f8f2aa8");
    //UUID serviceUUID = UUID.fromString("0000180d-0000-1000-8000-00805f9b34fb");
    //UUID characteristicUUID = UUID.fromString("00002a37-0000-1000-8000-00805f9b34fb");
 
    //tag를 사용하기 위한 선언
    private static final String TAG = "MainActivity";
    ConnectedBluetoothThread mConnectedBluetoothThread;
    private BluetoothGatt mBluetoothGatt;
 
    private ArrayList<String> mMessages;
    private ArrayAdapter<String> mMessageAdapter;
    private ArrayAdapter<String> mAdapter;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
 
        //블루투스가 되는지 설정
        if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            Toast.makeText(this"블루투스 미지원 기기입니다.", Toast.LENGTH_SHORT).show();
            finish();
        }
 
        //권한 설정
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            requestPermissions(
                    new String[]{
                            Manifest.permission.BLUETOOTH,
                            Manifest.permission.BLUETOOTH_SCAN,
                            Manifest.permission.BLUETOOTH_ADVERTISE,
                            Manifest.permission.BLUETOOTH_CONNECT,
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.ACCESS_COARSE_LOCATION
                    },
                    1);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(
                    new String[]{
                            Manifest.permission.BLUETOOTH
 
                    },
                    1);
        }
 
        // 관련 초기화
        mTvBluetoothStatus = (TextView)findViewById(R.id.tvBluetoothStatus);
        mTvReceiveData = (TextView)findViewById(R.id.tvReceiveData);
        mTvSendData =  (EditText) findViewById(R.id.tvSendData);
        mBtnConnect = (Button)findViewById(R.id.btnConnect);
        mBtnDisConnect = (Button)findViewById(R.id.btnDisConnect);
        mBtnSendData = (Button)findViewById(R.id.btnSendData);
        mMessageList = (ListView)findViewById(R.id.message_list_view);
        mMessages = new ArrayList<>();
        mMessageAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mMessages);
        mMessageList.setAdapter(mMessageAdapter);
 
 
        // 블루투스 기능을 지원하는지 알아오는 메서드
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        final BluetoothManager bluetoothManager =
                (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();              // null로 반환하지 않기 위해 사용
 
 
        // 버튼 눌렀을 때 동작하는 기능들
        mBtnConnect.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View view) {
                listPairedDevices();
            }
        });
 
        mBtnDisConnect.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View view) {
                disconnectSelectedDevice();
            }
        });
 
        mBtnSendData.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View view) {
                String message = mTvSendData.getText().toString();
                if (!message.isEmpty()) {
                    mConnectedBluetoothThread = new ConnectedBluetoothThread(mBluetoothGatt);
                    mConnectedBluetoothThread.start();
                    mConnectedBluetoothThread.write(message);
                    //mTvSendData.setText("");
                }
            }
        });
 
        // 메시지를 저장할 목록
        List<String> messages = new ArrayList<String>();
        // ListView에 메시지를 표시하기 위해
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, messages);
        // 어댑터 설정
        mMessageList.setAdapter(adapter);
 
        // 블루투스 핸들러로 연결 뒤, 수신된 데이터를 읽어오는 부분
        mBluetoothHandler = new Handler(Looper.getMainLooper()){
            public void handleMessage(android.os.Message msg){
                if(msg.what == BT_MESSAGE_READ){
                    String readMessage = null;
                    try {
                        readMessage = new String((byte[]) msg.obj, "UTF-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    // mTvReceiveData.setText(readMessage);
                    // 메시지를 목록에 추가
                    messages.add(readMessage);
                    // 데이터가 변경
                    adapter.notifyDataSetChanged();
                }
            }
        };
 
        mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
        mMessageList.setAdapter(mAdapter);
 
 
    }
 
    void listPairedDevices() {
 
        // 블루투스 활성화 상태 확인
        try{
            if (mBluetoothAdapter.isEnabled()) {
                // 이미 Paired된 Device목록을 얻어옴
                mPairedDevices = mBluetoothAdapter.getBondedDevices();
 
                //페어링 된 장치가 존재한다면 새로운 알림창을 생성
                if (mPairedDevices.size() > 0) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("장치 선택");
 
                    mListPairedDevices = new ArrayList<String>();
                    for (BluetoothDevice device : mPairedDevices) {
                        mListPairedDevices.add(device.getName());
                        //mListPairedDevices.add(device.getName() + "\n" + device.getAddress());
                    }
                    final CharSequence[] items = mListPairedDevices.toArray(new CharSequence[mListPairedDevices.size()]);
                    mListPairedDevices.toArray(new CharSequence[mListPairedDevices.size()]);
 
                    //장치를 누르면 connectSelectedDevice로 전달
                    builder.setItems(items, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int item) {
                            for (BluetoothDevice device : mPairedDevices) {
                                if (device.getName().equals(items[item].toString())) {
                                    connectSelectedDevice(String.valueOf(device));
                                    mTvBluetoothStatus.setText("connected to " + device.getName());
                                    break;
                                }
                            }
                        }
                    });
                    // 알림창을 뛰어줌
                    AlertDialog alert = builder.create();
                    alert.show();
                } else {
                    Toast.makeText(getApplicationContext(), "페어링된 장치가 없습니다.", Toast.LENGTH_LONG).show();
                }
            }
            else {
                Toast.makeText(getApplicationContext(), "블루투스가 비활성화 되어 있습니다.", Toast.LENGTH_SHORT).show();
            }
 
        }catch (SecurityException e)
        {
            e.printStackTrace();
        }
    }
 
    // 지정된 주소를 사용하여 장치에 연결
    @SuppressWarnings({"MissingPermission"})
    private void connectSelectedDevice(String address) {
 
        // 블루투스 장치에 대한 정보를 가져옴
        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
 
        // 연결 상태 변경 수신
        //mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
        if (mBluetoothGatt == null) {
            mBluetoothGatt = device.connectGatt(thisfalse, mGattCallback);
        } else {
            mBluetoothGatt.connect();
        }
        //mTvBluetoothStatus.setText("connected to "+ address);
    }
 
    // BluetoothGattCallback를 상속하는 mGattCallback객체를 생성
    // bluetooth연결과 관련된 이벤트 처리
    private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        //연결 상태 변경시 호출, 연결 성공시 gatt서버 검색
        @SuppressLint("MissingPermission")
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            super.onConnectionStateChange(gatt, status, newState);
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                Log.i(TAG, "Connected to GATT server.");
                // 서비스 검색
                gatt.discoverServices();
            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                // GATT 서버와 연결이 끊어졌을 때의 처리를 수행
                Log.i(TAG, "Disconnected from GATT server.");
                mBluetoothGatt = null;
                gatt.close();
            }
        }
 
        // gatt서버 검색 후 호출
        @SuppressLint("MissingPermission")
        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                // 서비스 검색이 완료되면, 필요한 서비스나 특성에 대한 작업을 수행
                List<BluetoothGattService> services = gatt.getServices();
                for (BluetoothGattService service : services) {
                    // Perform operations on each service, if needed.
                    UUID serviceUuid = service.getUuid();
                    Log.d(TAG, "Discovered service: " + serviceUuid);
 
                    List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
                    for (BluetoothGattCharacteristic characteristic : characteristics) {
                        // Perform operations on each characteristic, if needed.
                        UUID characteristicUuid = characteristic.getUuid();
                        Log.d(TAG, "Discovered characteristic: " + characteristicUuid);
 
                    }
                /**/
                }
            }else {
                Log.e(TAG, "onServicesDiscovered error: " + status);
            }
        }
 
        //characteristicUUID가리키는 곳에서 데이터를 읽은 후 호출
        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //어댑터 갱신
                        byte[] data = characteristic.getValue();
                        String message = new String(data);
                        mAdapter.add(message);
                        mAdapter.notifyDataSetChanged();
                    }
                });
            }
        }
 
 
    };
 
    //원격 장치에 연결하여 생성된 BluetoothGatt 개체를 반환
    @SuppressLint("MissingPermission")
    private BluetoothGatt connectGatt(BluetoothDevice device) {
        return device.connectGatt(thisfalse, mGattCallback);
    }
 
    // 블루투스 연결 후 데이터 수신 및 전송을 관리
    @SuppressWarnings({"MissingPermission"})
    private class ConnectedBluetoothThread extends Thread {
        private final BluetoothGatt mmGatt;
        private BluetoothGattCharacteristic mCharacteristic;
 
        public ConnectedBluetoothThread(BluetoothGatt gatt) {
            mmGatt = gatt;
 
            //지정된 UUID와 관련된 서비스
 
            // BluetoothGattService와 BluetoothGattCharacteristic을 검색
            BluetoothGattService service = gatt.getService(BT_UUID);
 
            if (service != null) {
                //지정된 UUID와 관련된 특성
                mCharacteristic = service.getCharacteristic(BT_UUID);
 
                if (mCharacteristic == null) {
                    // 못찾으면 찾기를 반복
                    List<BluetoothGattService> gattServices = gatt.getServices();
                    for (BluetoothGattService gattService : gattServices) {
                        if (gattService.getUuid().equals(BT_UUID)) {
                            List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
                            for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                                if (gattCharacteristic.getUuid().equals(BT_UUID)) {
                                    mCharacteristic = gattCharacteristic;
                                    break;
                                }
                            }
                        }
                    }
                }
            } else {
                Log.e(TAG, "service is null");
                mCharacteristic = null;
                //return;
            }
        }
 
        public void run() {
            //지정된 UUID와 관련된 서비스
            BluetoothGattService service = mmGatt.getService(BT_UUID);
 
            if (service == null) {
                Log.e(TAG, "service is null");
                mCharacteristic = null;
                return;
            }else{
                mCharacteristic = service.getCharacteristic(BT_UUID);
            }
            //UUID와 관련된 특성
            mCharacteristic = service.getCharacteristic(BT_UUID);
 
            if (mCharacteristic == null) {
                Log.e(TAG, "mCharacteristic is null");
                //찾을 때까지 반복
                List<BluetoothGattService> gattServices = mmGatt.getServices();
                for (BluetoothGattService gattService : gattServices) {
                    if (gattService.getUuid().equals(BT_UUID)) {
                        List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
                        for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                            if (gattCharacteristic.getUuid().equals(BT_UUID)) {
                                mCharacteristic = gattCharacteristic;
                                break;
                            }
                        }
                    }
                }
                return;
            }
            // 데이터 수신 알림 설정
            mmGatt.setCharacteristicNotification(mCharacteristic, true);
            BluetoothGattDescriptor descriptor = mCharacteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            mmGatt.writeDescriptor(descriptor);
 
            // 데이터 수신
            while (true) {
                // 데이터를 수신하여 bytes 변수에 저장
                byte[] bytes = mCharacteristic.getValue();
 
                // bytes 변수를 원하는 형태로 바꿔 message 변수에 저장
                String message = new String(bytes);// bytes를 문자열로 변환
 
                // mMessageAdapter에 message를 추가합니다.
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mMessages.add(message);
                        // ListView의 Adapter에 새 메시지를 추가
                        mMessageAdapter.notifyDataSetChanged();
                        // ListView를 맨 아래로 스크롤
                        mMessageList.setSelection(mMessages.size() - 1);
                    }
                });
            }
        }
 
        public void write(String str) {
            if (mBluetoothGatt == null || mCharacteristic == null) {
                return;
            }
 
            byte[] bytes = str.getBytes();
            mCharacteristic.setValue(bytes);
            mBluetoothGatt.writeCharacteristic(mCharacteristic);
            // ListView에 메시지 추가
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mMessages.add(str);
                    mMessageAdapter.notifyDataSetChanged();
                    mMessageList.setSelection(mMessages.size() - 1);
                }
            });
        }
 
 
        public void cancel() {
            mmGatt.close();
        }
    }
 
 
    @SuppressLint("MissingPermission")
    public void disconnectSelectedDevice() {
        if (mBluetoothGatt != null) {
            mBluetoothGatt.disconnect();
            mBluetoothGatt.close();
            mBluetoothGatt = null;
        }
        if (mBluetoothAdapter != null) {
            mBluetoothAdapter.cancelDiscovery();
        }
        if((mBluetoothGatt == null&& (mBluetoothAdapter == null)){
            mTvBluetoothStatus.setText("연결 종료");
        }else{
            mTvBluetoothStatus.setText("연결 종료 실패");
        }
    }
 
 
}
cs

라즈베리파이에 연결이 된다고 뜨는데

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from bluetooth import *
 
server_socket= BluetoothSocket(RFCOMM)
 
port = 1
server_socket.bind(("", port))
server_socket.listen(1)
 
client_socket, address = server_socket.accept()
print("Accepted connection from ", address)
 
client_socket.send("bluetooth connected!")
 
while True:
    data = client_socket.recv(1024)
    print("Received: %s" %data)
    if(data=="q"):
        print("Quit")
        break
 
client_socket.close()
server_socket.close()
 
cs

정작 이 파이썬 코드를 돌렸을 때는 연결이 되지 않는다.... 뭐가 문제일까...ㅠㅠㅠ

반응형
LIST

댓글